Apache Httpd Tomcat



Apache

Introduction Using Apache httpd as a proxy to an Apache Tomcat application container is a common setup. It comes with many use cases, the most trivial is serving static content from httpd, while providing services implementing heavy business logic from an application written in Java that resides in the Tomcat container.

Apache Tomcat Error 404

  • Apache Tomcat is used to deploy your Java Servlets and JSPs. So in your Java project you can build your WAR (short for Web ARchive) file, and just drop it in the deploy directory in Tomcat. So basically Apache is an HTTP Server, serving HTTP. Tomcat is a Servlet and JSP Server serving Java technologies.
  • Apache HTTPD 2.4.6 (from Red Hat yum repository) Tomcat 8.5.23 (from Apache) Install Docker on Red Hat Enterprise Linux 7. Join Red Hat Enterprise Linux developer subscription; Then, refer to the GETTING STARTED WITH CONTAINERS to install Docker and corresponding configurations. Dockerfile -Apache HTTPD. Mkdir -p /root/httpd-project; cd /root.
Tomcat

Apache Tomcat 8.5

Technics‎ > ‎

How to run Apache Httpd and Tomcat on port 80 using mod proxy

This how to assumes you already have installed Apache Httpd and Apache Tomcat running on a Debian/Ubuntu compatible box. Sometimes is it useful to run Apache and Tomcat side by side running on the standard http port (80) in case you need to run other scripts than JSP pages or Servlets. There are several ways to do this. The tomcat project exposes them in the Connector Docs. But in this case we are going to use mod_proxy which is a general propose proxy module. The main idea is to setup a VirtualHost exclusive for tomcat on a given path of our site.

Ensure we have mod_proxy enabled


You need to enable 2 modules: proxy and proxy_http using a2enmod
root@server# a2enmod proxy
Module proxy installed; run /etc/init.d/apache2 force-reload to enable.

root@server# a2enmod proxy_http
Enabling proxy as a dependency
This module is already enabled!
Module proxy_http installed; run /etc/init.d/apache2 force-reload to enable.

Create your VirtualHost

root@server# cd /etc/apache2/sites-available
root@server# vi mysite.com

This is the content of mysite.com
<VirtualHost XXX.XXX.XXX.XXX:80>
ServerAdmin webmaster@localhost
ServerName http://www.mysite.com/
DocumentRoot /home/httpd/publics/mysite.com/
ErrorLog /var/log/apache2/mysite.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/mysite.com-access.log combined
ServerSignature Off
<Directory /home/httpd/publics/mysite.com/>
Options FollowSymLinks
AllowOverride All
Options -MultiViews
</Directory>
</VirtualHost>

Now we need to enable this VirtualHost
root@server# a2ensite mysite.com
Site mysite.com installed; run /etc/init.d/apache2 force-reload to enable.

Add your Tomcat VirtualHost


Here lets assume we want to have mysite.com and tomcat.mysite.com where all tomcat request will go

root@server# cd /etc/apache2/sites-available
root@server# vi tomcat.mysite.com

This is the content of tomcat.mysite.com
<VirtualHost XXX.XXX.XXX.XXX:80>
ServerAdmin webmaster@localhost
ServerName tomcat.mysite.com
DocumentRoot /home/httpd/publics/tomcat.mysite.com/
ErrorLog /var/log/apache2/tomcat.mysite.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/tomcat.mysite.com-access.log combined
ServerSignature Off
# lets indicate the proxy what path do we want
# to forward to tomcat
ProxyPass / http://localhost:8080/
<Directory /home/httpd/publics/tomcat.mysite.com/>
Options FollowSymLinks
AllowOverride All
Options -MultiViews
</Directory>
</VirtualHost>

Now we need to enable this VirtualHost too
root@server# a2ensite tomcat.mysite.com
Site tomcat.mysite.com installed; run /etc/init.d/apache2 force-reload to enable.

Restart Apache



The trick


The main trick of this configuration resides on the line
  • ProxyPass / http://localhost:8080/
Where we tell the proxy what path should be forwarded to Tomcat. In the same way we could modify this to our needs. Lets say we would like to have our path on /tomcat/ all we should do is:

The advanced way


We may want to run both VirtualHost in the same configuration and only forward to tomcat requests starting on a given path
<VirtualHost XXX.XXX.XXX.XXX:80>
ServerAdmin webmaster@localhost
ServerName mysite.com
DocumentRoot /home/httpd/publics/mysite.com/
ErrorLog /var/log/apache2/mysite.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/mysite.com-access.log combined
ServerSignature Off
# lets indicate the proxy what path do we want
# to forward to tomcat
ProxyPass /java/ http://localhost:8080/
<Directory /home/httpd/publics/mysite.com/>
Options FollowSymLinks
AllowOverride All
Options -MultiViews
</Directory>
</VirtualHost>

With the above scheme all requests will be served by apache unless they start with /java/
Will be served by the PHP engine. But however:
Will be forwarded to Tomcat