How To Configure Apache as a Reverse Proxy For Ubuntu/Debian

Apache Reverse Proxy allows you to run one or more back-end servers behind a proxy server. Here’s how to configure Apache Reverse Proxy on Ubuntu/Debian system.

Install and configure Apache Reverse Proxy
Install and Configure Apache as a Reverse Proxy

Overview

Apache Reverse Proxy allows all traffic and forwards it to run one or more backend servers or containers behind a proxy server, without exposing them publicly. The back-end web 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 server that is being used today.

There are many reasons to install and configure a proxy server. For Example, reverse proxy can help you to add security, for load balancing, restrict access to certain locations in order to prevent attacks and many more. This article explains how to install and configure apache reverse proxy configuration step by step for HTTPS Server on Ubuntu/Debian:

  • Installing Apache2
  • Configure Apache2
  • Enable its proxy modules
  • Enable the SSL
  • Restart Apache2
  • Conclusion

Step 1: Install Apache2

Installing Apache2 is very simple and easy to run. To do Installations, run the below commands simply:

sudo apt-get update
sudo apt-get install apache2

After installing Apache2 on your system, use the following systemctl commands on Debian Linux or Ubuntu Linux version Ubuntu to stop, start, enable and restart Apache2 to always restart when the server boots up.

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

You can view web server status using the following command:

sudo systemctl status apache2.service

To check Apache2 setup, open your browser and browse to the server hostname or your system 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

Install and Configure apache reverse proxy Proxy For Ubuntu

Step 2: Configure Apache2 as Reverse Proxy

Apache2 is successfully installed and ready to use. You can now configure Apache2 to work as a apache2 reverse proxy ubuntu. Apache2 proxy module’s ProxyPass and ProxyPassReverse function provide a reverse proxy. To use ProxyPass and ProxyPassReverse, you must first know that where you want to direct site traffic.

The apache2 reverse proxy server will listen for all traffic on the default HTTP port, which is port 80 in a simple setup. The back-end server which host the website content will listen on a custom port and it is most likely port 8080.

In this blog article, we are going set up Apache2 to listen on port 80 and then direct traffic to the back-end server which listens on port 8080. Run below command to create a proxy VirtualHost file called Apache2Proxy.conf.

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

Then add the following block of codes in the Apache2Proxy.conf file and then save it.

<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>

Apache2Proxy.conf file contains your server name as well as the proxy_pass where traffic will be directed when received by the http proxy server.

Step 3: Enable Apache2 Proxy

Apache2 web server is now installed and setup successfully. Apache has many available bundled modules bundled with in it. These modules are not enabled in fresh apache2 installation. First we’ll need to enable the modules we need to enable required apache mod_proxy module and several of its add-on modules to support different network protocols. Run the below listed commands to enable its http proxy modules.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

Now enable the VirtualHost site and restart Apache2 to enable reload the Apache2 modules configuration.

sudo a2ensite Apache2Proxy.conf 
sudo systemctl restart apache2.service

Launch a web browser of your choice and navigate to your server hostname like example.com. You will now be proxied to your Apache2 server on default port i.e 8080.

Step 4: Enable the SSL

If you want to enable the apache reverse proxy ssl module then run the below command to enable apache reverse proxy https proxy pass:

sudo a2enmod ssl

This will provide secure apache reverse proxy http to https support to backend servers.

Step 5: Restart Apache2

To make these changes into effect, restart Apache by running the below command:

sudo systemctl restart apache2.service

Congratulations! You have successfully installed and configured Apache2 reverse proxy on your Linux system.

Conclusion:

In this tutorial, we explored and discussed what is a Proxy Server and apache reverse proxy example configuration step by step. We also learned how to set up and configure apache2 as a reverse proxy on a linux system. In our upcoming tutorials, we’ll discuss more interesting topics about apache and other web server solution stacks.

Explore

You may also like the below more related articles:

Avatar

About Yasir Saeed

Open Source Enthusiast. I spend almost all my days making sure everyone around containerize is happy by growing your website traffic and ranking . Please feel free to reach us via our open source forum