Sam - February 19, 2017

How To Utilize Apache as a Reverse Proxy with Mod_proxy on Ubuntu (16.04)

Do you know what a reverse proxy is? If you don’t, let’s take a minute to explain…

This type of proxy receives requests from HTTP(s) and then openly offers them to one or multiple servers of the back-end type. In terms of usefulness, these proxies are valued because an array of ultra-modern Web apps receive HTTP requests via app servers of the back-end variety! These servers aren’t designed to be accessible to users in a direct way and they are meant to support only the most basic features of HTTP. If you want more performance, the key is discovering the best way to turn Apache into a reverse proxy system. It’s really a lot easier than you think and we want to walk you through the process today.

You may utilize reverse proxies to stop app servers from being accessed in a direct manner. As well, you may utilize reverse proxies in order to scatter the load from new requests to an assortment of separate app servers. This will help you to boost performance “at scale” and to provide fail-safe service. Basically, when you opt for a reverse proxy, you’ll have more control over how your app servers operate. As well, if you wish to, you’ll be able to fill in gaps with your preferred features, such as SSL encryption, caching and compression. As you can see, personalization will be at your fingertips when you follow our simple steps.

Today, we’re going to share clear and detailed instructions on how to set up Apache in order to make it a reverse proxy system, by utilizing the extension known as mod_proxy. It redirects connections that come into a single back-end server (or more back-end servers) which run via the same network. Our tutorial utilizes an easy back-end which is designed via the Web framework known as Flask. However, you may feel free to utilize your preferred back-end server. Note: don’t add the quote marks outside of the commands. Just add what’s inside!

Before you get started, let’s discuss prerequisites…

You will need a 16.04 Ubuntu server which includes the first server setup instructions. These tutorial-style instructions must include a “sudo” user of the non-root type, as well as a firewall. As well, you’ll need to ensure that Apache 2 has been installed on the server.

Now, you’ll need to enable the right modules for Apache.

Find the perfect Proxy Product.

Proxyrack offers a multiple options to suit most use cases, if you are unsure our 3 Day Trial allows you to test them all.
Security

Residential proxies

Never get blocked, choose your location
View all option available
Vault

Datacenter proxies

Super fast and reliable
View all option available
Try

3 Day Trial

Test all products to find the best fit
View all option available

Step One – Enable The Correct Modules for Apache

Apache features a host of modules which are grouped together. However, although they are grouped together, they aren’t enabled. This means that you’ll need to do some enabling in order to get them up and running. The first module to enable is mod_proxy, as well as some mod_proxy module add-ons which contribute to superior functionality among a variety of network protocols. Here are the right things to enable…

Mod_proxy is the primary proxy module for Apache and it’s utilized in order to redirect connections. It gives Apache the power to function as a gateway for application servers which are underlying.

Also, you’ll need to enable mod_proxy_http, which gives support for the proxying of connections of the HTTP type.

As well, you’ll need to enable both mod_Ibmethod_byrequests, in addition to mod_proxy_balancer. Enabling them will add load-balancing elements for a group of back-end servers.

To get these modules enabled, you’ll need to execute the commands below, in the same order that they appear.

  1. sudo a2enmod proxy

  2. sudo a2enmod proxy_http

  3. sudo a2enmod proxy_balancer

  4. sudo a2enmod lbmethod_byrequests

After you put in these commands, reboot Apache. This will activate the changes.

You may restart Apache by adding this command, sudo systemctl restart apache2.

At this point, Apache is primed to function as an HTTP request reverse proxy. During the following step, we’re going to make a couple of standard servers of the back-end type. This will give us the power to see if our new setup is working the way that it should. If you have an app for back-end in place already, just skip this and move onto the following step.

Step Two – Make Testing Servers for the Back-end

Back-end servers may be run in order to see if the new setup in Apache is working well. To make a couple of servers of this type, which react to single lines of printed text, you’ll need to follow our instructions. One of these severs is going to respond with the words, “hello world!”. The other server will send out this message: “howdy world”.

They send unique messages in order to let users know that the load is balanced.

Flask functions as a micro-framework within Python and it’s used to built Web apps. We’re utilizing Flash to make test servers, as standard applications only need some code lines. You won’t have to familiar with Python to get these test servers created. There are tutorials for Python which will help you to get the job done.

To begin, update your list of packages.
The command you’ll need to enter is, sudo apt-get update
After this, perform installation of Pip, which is the package manager for Python.
The command for this is sudo apt-get -y install python3-pip
Next, utilize Pip in order to get Flask installed.
The command for this is, sudo pip3 install flask

After these commands are processed, you’ll need to make a file which contains coding for the first back-end server which is found in the directory called, “home”. The command for this is nano ~/backend1.py

Next, paste the code below into your file. After you do so, save it and then close the file.

~/backend1.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello world!

The first coupe of codes will start the framework for Flask. There is a single function, called home, which provokes the return of a text line, which is “hello world!”. The line over the function for home (@app.route(“/”) instructs the program to utilize home’s return value when it received requests of the HTTP type, which are directed towards the “/root url” part of the app.

It’s the same thing with the second back-end server. It just returns a line of text which is different from the first.

To look at the file that you just copied, type:

cp ~/backend1.py ~/backend2.py

To alter your message return from “hello world!” to “howdy world”, add this command:

nano ~/backend2.py

After you do so, save your file and close your file.
Use this commend next:

~/backend2.py
from flask import Flask
app = Flask(__name__)

Then, use this command in order to get the first server running via the port, 8080. It will additionally do a redirect of the output of Flask to a section known as /dev/null.

@app.route('/')
def home():
return 'Howdy world!'

Now, you’ll need to enter this command:

FLASK_APP=~/backend1.py flask run --port=8080 >/dev/null 2>&1 &

This sets the right variable for environment.

Test your server by adding this command:

FLASK_APP=~/backend2.py flask run --port=8081 >/dev/null 2>&1 &

and this one:

curl http://127.0.0.1:8080/

You should see, “hello world” when you do so. Test the second server the same way, but with this command:
curl http://127.0.0.1:8081/

Step Three – Change Default Configurations

You now need to change the default configurations for Apache in order to make it function as a proxy of the reverse type.

To begin, open up the configuration for default Apache setting, via a text editor or nano. Add this command:

sudo nano /etc/apache2/sites-available/000-default.conf

When you go into the file, you’ll see a line with virtualhost 80 in the text. Replace it with this upcoming command in order to alter the file for configuration purposes:

/etc/apache2/sites-available/000-default.conf

ProxyPreserveHost On

ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/

Then, restart your system. When it is restarted, everything should function perfectly. You’ll have a new reverse proxy setup with Mod_proxy on Ubuntu 16.04 and you’ll have taken care of the whole job by yourself. If you find that it doesn’t work correctly, go through the steps again. Check the codes and the order and make sure that everything is right. If you need to change something, go in and make the appropriate change or changes.

Now, you know the drill and you’ll be able to utilize Apache the reverse proxy way. Our guide is meant to be simple to follow and it will give you the ability to customize performance in just a few minutes. While there are definitely some steps to follow and some commands to enter, it’s not that laborious to get this task done and it will give you the power to enjoy a reverse proxy setup quickly.

Learning how to utilize Apache as a reverse proxy with mod_proxy on Ubuntu 16.04 is practical. If you got value from this step by step article, we hope that you’ll leave a comment and/or share it via social media. We’ve taken the guesswork out of performing this process because we want to make it simple for people to access reverse proxy setups, without paying a pro to take care of things for them.

Find the perfect Proxy Product.

Proxyrack offers a multiple options to suit most use cases, if you are unsure our 3 Day Trial allows you to test them all.
Security

Residential proxies

Never get blocked, choose your location
View all option available
Vault

Datacenter proxies

Super fast and reliable
View all option available
Try

3 Day Trial

Test all products to find the best fit
View all option available

Get Started by signing up for a Proxy Product