Configure Reverse Proxies Using Apache2 HTTP Server On Ubuntu 18.04

This brief tutorial shows students and new users how to install and configure reverse proxies with Apache2 HTTP Server on Ubuntu 18.04 and others..

Reverse proxy is when a proxy server (in this case, Apache2 HTTP) accepts all traffic and forwards it to a specific resource, like a backend server or container.. The backend server can be either another Apache2 or open source HTTP server like Nginx…

Apache2 HTTP server is one of the most popular open source web servers that is in use today… It’s not typically used as a proxy server, but can be if you want to use it as one..

There are many reasons to install and use a proxy server… Example, reverse proxy can be used to added security, or for load balancing, restrict access to certain locations in order to prevent attacks and many more…

When you’re ready to configure Apache2 as reverse proxy, follow the steps below:

Step 1: Install Apache2

Installing Apache2 is easy.. to do that, simply run the commands below

sudo apt-get update
sudo apt-get install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 to always restart when the server boots up.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

To test Apache2 setup, open your browser and browse to the server hostname or IP address and you should see Apache2 default test page as shown below.. When you see that, then Apache2 is working as expected..

http://localhost

Apache2 Test Page

Step 2: Configure Apache2 as Reverse Proxy

Assuming that Apache2 is installed and ready, you can now configure Apache2 to act as a reverse proxy..

Apache2 proxy module’s ProxyPass and ProxyPassReverse function provide a reverse proxy… To use ProxyPass and ProxyPassReverse, you must first know where you want to direct traffic..

In a typical setup, the reverse proxy server will listen for all traffic on the default HTTP port, which is port 80..

The backend server which host the content will listen on a custom port… Most likely port 8080.

In this post, we’re going set up Apache2 to listen on port 80, then direct traffic to the backend server which listens on port 8080

Below, run the command to create a proxy VirtualHost file called Apache2Proxy.conf.

sudo nano /etc/apache2/sites-available/Apache2Proxy.conf

Then add the block of codes in the file then save.

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin webmaster@example.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyRequests Off
        <Proxy *>
          Order deny,allow
          Allow from all
        </Proxy>
        
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/

        <Location />
          Order allow,deny
          Allow from all
        </Location>

   </VirtualHost>

Save the file when done.

The file contains your server name as well as the proxy_pass where traffic are directed when received by the proxy server….

Step 3: Enable Apache2 Proxy

Now that Apache2 is installed, run the commands below to enable its proxy modules.

sudo a2enmod proxy
sudo a2enmod proxy_http

When you’re done, enable the VirtualHost site and restart Apache2 to enable reload the modules.

sudo a2ensite Apache2Proxy.conf 
sudo systemctl restart apache2.service

Launch a web browser and navigate to your server hostname (example.com)… You will now be proxied to your Apache2 server on port 8080.

apache2 proxy to nginx

Congratulations! You have successfully configure Apache2 reverse proxy on Ubuntu 18.04

You may also like the post below:

Setup FluxBB Bulletin Board Forum On Ubuntu 18.04 | 16.04 With Nginx

Leave a Reply

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

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