How to create a reverse proxy on ubuntu with apache2

Hello All,

This blog is a sister post of how to create a virtualhot on ubuntu. In this I will be showing how to use apache as a reverse proxy for another web server, we will be using tomcat as that another server. I will be using virtualbox and ubuntu 14.04 guest. Please refer to the sister post to have a fair idea of how everything would pan out as we using same settings.

Preparatory work

Our virtual machine has the same IP and will use a domain like node5-tomcat.localhost, so we will add that domain to our /etc/hosts file.

Installation of required packages

In the box used for the first part of this tutorial we had apache2 and php5 installed, we need now to install java and apache tomcat.

[bash]
$sudo apt-get install openjdk-7-jdk tomcat7
[/bash]

Let’s verify our installation by visiting 192.168.56.105:8080 in a browser,Since default port for apache tomcat is 8080.

tomcat running on port 8080
tomcat running on port 8080

Enabling mod_proxy

Before we start creating our virtualhost file, let’s do a little preparatory work. First, we need to edit the port on which we want to forward request from apache to tomcat.If we chose to use http protocol, there won’t be any need of editing any file in tomcat since tomcat listens to requests on port 8080 over http.

Mod_ajp is the guy that would take care of our bridging between apache and tomcat here.Explaining what is mod_ajp is outside the scope of this post but I encourage you to read a little more about it.It’s designed for this kind of purpose — bridging to java servers/servlet containers — and seem to support various configurations and is way faster than mod_http which can also be used. We will need to enable tomcat to listen for request on the port 8009 over ajp.

Enabling mod_proxy components

Before we can use mod_ajp, we needed to enable mod_proxy and load them in apache. In order to do that let’s follow the commands below:
[bash]
sudo a2enmod proxy
sudo a2enmod proxy_ajp
sudo a2enmod proxy_http
sudo service apache2 restart
[/bash]

After enabling the apache modules that we needed ,we need to open the ajp port in tomcat so that tomcat is ready to receive request on the ajp port. Let’s edit tomcat server.xml file .open /var/lib/tomcat7/conf/server.xml and make sure you change this:

[xml]
<!– Define an AJP 1.3 Connector on port 8009 –>
<!–
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
–>
[/xml]

to look like this:

[xml]
<!– Define an AJP 1.3 Connector on port 8009 –>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
[/xml]

After this we will need to restart tomcat for the changes to reflect.

[bash]
sudo service tomcat7 restart
[/bash]

Creation of the virtualhost

[bash]
sudo nano /etc/apache2/sites-available/node5.localhost.conf
[/bash]

Let’s make sure it looks like shown below

[bash]
#/etc/apache2/sites-available/node5-tomcat.localhost.conf

<VirtualHost *:80>

ProxyRequests off
ProxyPreserveHost On
ServerName node5-tomcat.localhost

ServerAdmin pakkun@localhost

ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

LogLevel warn

ErrorLog ${APACHE_LOG_DIR}/node5-tomcat_error.log
CustomLog ${APACHE_LOG_DIR}/node5-tomcat_access.log combined

</VirtualHost>

[/bash]

Let’s activate our virtual host with the command a2ensite. What that command does is to actually create a symbolic link in the /etc/apache2/sites-enabled folder.

[bash]
pakkun@node5:~$ sudo a2ensite node5-tomcat.localhost.conf
Enabling site node5-tomcat.localhost.
To activate the new configuration, you need to run:
service apache2 reload
[/bash]

After that a reload of apache configuration would make the virtualhost available.

[bash]sudo service apache reload[/bash]

Now let’s visit our site at http://node5-tomcat.localhost, it should be looking like below

node5 tomcat reverse proxy
node5 tomcat reverse proxy

Voila!! your are up.

2 thoughts on “How to create a reverse proxy on ubuntu with apache2

Leave a Reply

Your email address will not be published. Required fields are marked *

captcha * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top