How to Install mod_proxy in Apache in Ubuntu


how to install mod_proxy in apache

mod_proxy is a useful Apache module that allows you to setup Apache proxy server and direct traffic to other back end servers and ports. It also supports various load balancing algorithms, allowing you to reduce server load. Let us look at how to install mod_proxy in Apache on Ubuntu.

 

How to Install mod_proxy in Apache in Ubuntu

Here are the steps to install mod_proxy in Apache.

 

1. Install Apache

Open terminal window and run the following commands to update packages and then install Apache.


$ sudo apt-get update -y
$ sudo apt-get upgrade -y

 

Install Apache


$ sudo apt-get install apache2 -y

 

Start Apache service and enable it to start automatically on reboot.


$ sudo /etc/init.d/apache2 start
$ sudo update-rc.d apache2 defaults

 

2. Install mod_proxy and other related modules

Install mod_proxy and its dependencies


$ sudo apt-get install libapache2-mod-proxy-html libxml2-dev -y

 

3. Enable mod_proxy

Run the following command to get a list of installed modules.


$ sudo a2enmod

Now add mod_proxy related modules


$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
$ sudo a2enmod proxy_ajp
$ sudo a2enmod rewrite
$ sudo a2enmod deflate
$ sudo a2enmod headers
$ sudo a2enmod proxy_balancer
$ sudo a2enmod proxy_connect
$ sudo a2enmod proxy_html

 

That’s it! You have installed mod_proxy in Apache in Ubuntu

 

4. Configure mod_proxy

Now we need to configure Apache proxy server. Open Apache’s Virtual Host file in a text editor


$ sudo vim /etc/apache2/sites-available/000-default.conf

 

We look at 3 possible proxy configurations for your references

1. Reverse Proxy Configuration

In Apache’s config file, look for the <Virtual Host> block and replace it with the following:


<VirtualHost *:80>
 ProxyPreserveHost On

 ProxyPass / http://127.0.0.1:8080/
 ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

In the above code,

  • ProxyPreserveHost – Makes Apache pass the Host header in original request. It allows backend server to know the origin
  • ProxyPass – It is the main reverse proxy directive that specifies that all requests under the root (/) URL should be sent to the back end server.
  • ProxyPassReverse – It makes Apache modify response headers before sending it to the client. So, in case of location redirect headers, the client is redirected to the reverse proxy and not back end servers.

 

2. SSL Reverse Proxy Configuration

This is similar to reverse proxy, except that we also provide paths to SSL certificate, private key and chain file in <VirtualHost> block, as shown:

 


<VirtualHost *:80>
 ProxyPreserveHost On
 ProxyPass / http://127.0.0.1:8080/
 ProxyPassReverse / http://127.0.0.1:8080/
 SSLCertificateFile /path/to/certificate.crt
 SSLCertificateKeyFile /path/to/certificate.key
 SSLCertificateChainFile /path/to/chainfile.crt
</VirtualHost>

 

3. Load Balancing Proxy

In this case Apache acts as a load balancer for a cluster of back end servers.


<VirtualHost *:80>
 <Proxy balancer://cluster>
  BalancerMember http://127.0.0.1:8080
  BalancerMember http://127.0.0.1:8081
 </Proxy>
ProxyPreserveHost On
ProxyPass / balancer://cluster/
ProxyPassReverse / balancer://cluster/
</VirtualHost>

 

In the above example, we point ProxyPass to a cluster of IP’s and ports, instead of just a single IP address. We have named the cluster as “cluster”. You can call it something else if you want. It basically contains a list of IP addresses and ports where the traffic needs to be distributed. We have used 127.0.0.1:8080 and 127.0.0.1:8081 for our example.

 

5. Restart Apache Server

Restart Apache to apply changes

$ sudo systemctl restart apache2

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!