HAProxy is an open-source high availability proxy and load balancer. HAProxy makes it easy to balance requests between multiple web servers. It improves website efficiency and performance. It can be used as either load balancer or revers proxy, or both at the same time. In this article, we will look at how to install HAProxy in Ubuntu. You can use these steps for Ubuntu 16, 18 or 20.
How to Install HAProxy in Ubuntu
Here are the steps to install HAProxy in Ubuntu. You need to be logged in as root user, or as a user with sudo privileges for this purpose.
1. Update system packages
Open terminal and run the following commands to update system packages.
$ sudo apt-get update
Also read : How to Import CSV in Python
2. Install HAProxy
Run the following command to install HAProxy in Ubuntu.
$ sudo apt-get install haproxy
Also read : How to Install Tomcat in Ubuntu
3. Enable HAProxy
To run HAProxy on start up, open the following file in a text editor.
$ sudo vi /etc/default/haproxy
Also read : How to Configure UFW Firewall in Ubuntu
4. Configure HAProxy
The HAProxy configuration file is located at /etc/haproxy/haproxy.cfg. It controls all the functions of HAProxy and has four main sections – global, default, front-end and back-end. The global section defines settings applicable for all websites that you run on HAProxy. The default configuration is good enough in most cases.
So out of these, we need to mainly configure front-end and back-end sections. Open the configuration file using a text editor.
$ sudo /etc/haproxy/haproxy.cfg
Add the following lines to front-end section which helps you specify the IP address and port. You can keep several front end sections, one for each website but you need to differentiate them with labels.
Replace ip_address below with your website’s IP address.
frontend haproxynode bind ip_address:443 ssl crt mode http default_backend backendnodes
The first line specifies the label haproxynode for this front end section. The next line mentions the ip address and port number HAProxy is listening to. The third line specified the protocol to be followed. In the above example, we are binding HAProxy to port 443. The last line refers to the label of back end section where the request needs to be forwarded.
Also read : How to Use NGINX with Flask
Similarly, add the corresponding back end section to serve requests received from this front end section. Just like front-end section, you can have multiple back end sections, for different websites, each labelled differently.
Let us say we want HAProxy to forward incoming requests to 2 web servers running on port 80 at ip_address1 and ip_address2. In such case, add the following backend section.
Replace ip_address1 and ip_address2 with ip addresses of your 2 servers. Also specify server1 & server 2 names to differentiate both of them.
backend backendnodes balance roundrobin server <server1 name> <ip_address1>:80 check server <server2 name> <ip_address2>:80 check
The first line mentions the label backendnodes for back end section. Next line specifies the algorithm to be used to distributing requests among servers. We will use round robin algorithm.
Next couple of lines describe the server name, ip address and port number of the two servers that will handle requests forwarded by HAProxy.
Also read : How to Sort List in Python
5. Enable stats
You can enable page stats by adding the following lines to your HAProxy configuration.
listen stats bind ip_address:1935 stats enable stats hide-version stats refresh 30s stats show-node stats uri /stats
As per the above configuration, HAProxy will be available at /stats URI at port number 1935. It will refresh every 30s. All of this can be customized as per your requirements.
Also read : How to Use NGINX as Reverse Proxy for Django
6. Verify HAProxy
Run the following command to check for errors.
$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg
Restart HAProxy to apply changes
$ sudo service haproxy restart
Run the following command to verify HAProxy is running.
$ sudo service haproxy status
Open browser and go to the following URL to get HAProxy stats. Replace ip_address with the IP address at which your HAProxy is running.
That’s it. In this article, we have described the basic configuration of HAProxy. You can always customize it as per your requirements.