HAProxy

Install and setup HAProxy for a simple two-server website setup

published on

This tutorial will cover the basics of how to set up HAProxy to run your site on two servers. It only covers the HAProxy part of the set up and so will be a little shorter than some of the other guides on this site.

Bear in mind that HAProxy can be configured in a number of ways and can do many complex things that are not covered in this tutorial. See https://docs.haproxy.org/ for more information.

Introduction

For the purposes of this tutorial we will need three servers. It is presumed that all three will be purchased from the same provider and added into a private network. Only one of the servers will need an external IP address.

Getting started

Once you have purchased your three servers and set up a private network (making sure that two of the servers do not have an external IP address) we can get started on installing HAProxy on the server that does have an external IP address.

This is simply done by running the following command on the HAProxy server:

sudo apt update
sudo apt install haproxy

For the purposes of this tutorial we will presume that you already have installed Apache on both of the other two servers and your site or service is working on both.

It's also worth noting that this tutorial does not cover how to replicate data (such as a database) so that both servers that your site is on can access and update the same information when necessary. This tutorial only covers getting HAProxy set up.

Configuring HAProxy

Once you have HAProxy installed you need to configure it.

First, make a copy of the original configuration file by running sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.original and then make your changes by running sudo nano /etc/haproxy/haproxy.cfg.

Find the frontend and backend sections and edit them to look like the below:

frontend example_front_end
mode http
bind *:80
bind :443 ssl cert /path/to/cert.pem
option forwardfor  
http-request redirect scheme https unless { ssl_fc }
default_backend web_servers    

backend web_servers
balance roundrobin
server server-1 10.0.0.11:8080 check
server server-2 10.0.0.12:8080 check

Make sure to update the path to your SSL certificate and the internal IP addresses of your two Apache servers.

Now find the listen section and update it to look something like the below:

listen stats
bind :32600
stats enable
stats uri /
stats hide-version
stats auth ha_USERNAME:PASSWORD

Make sure to replace USERNAME with a username of your choice and PASSWORD with a strong password.

Finally, restart HAProxy with sudo systemctl restart haproxy.

Configure Apache

Now it is time to configure the copies of Apache you have installed on the other two servers.

Edit the ports.conf file by running sudo nano /etc/apache2/ports.conf and change the line that reads

Listen 80

to

Listen 8080

Now edit the virtual host configuration files and change any instances of <VirtualHost *:80> to <VirtualHost *:8080>.

Finally, restart Apache.

Testing

Provided their is some form of content on each of the Apache servers you should now be able to access it by visiting the IP address of the HAProxy server. Presumably you will want to assign this to a domain name.

The first time you load the HAProxy IP you will be forwarded to the first backend server. If you reload the page you should now be redirected to the second backend server instead.

If you wish to view stats on how well HAProxy is performing visit the HAProxy IP address and port 32600 in your browser. You will need the username and password you entered into the configuration file earlier.

Conclusion

This guide is very brief on purpose to show you how to set up and configure HAProxy for a basic site running on two servers.

You need to work out for yourself what web server to use, where to store data and how to access or duplicate it.