HAProxy or High Availability Proxy, is a well-known open-source TCP/HTTP load balancer and proxy solution which is able to handle a lot of traffic. HAProxy consumes a very low amount of memory, and it is commonly used to improve the performance of servers by distributing the workload across multiple servers to handle a large number of concurrent connections. If you have busy websites, you can install and configure HAProxy as the reverse proxy to your webservers.
In this tutorial, we will show you how to install and configure HAProxy on Ubuntu 20.04, one of the most popular operating systems in the world.
Log in to your Ubuntu 20.04 VPS as a root user or as a regular user with sudo privileges. In this tutorial, we will use a sudoer user called ‘master’.
ssh master@IP_Address -p Port_number
Do not forget to replace “master” with a user that has sudo privileges, or root. Additionally, replace “IP_Address” and “Port_Number” with your server’s IP address and SSH port.
Run this command to check whether you have the proper Ubuntu version installed on your server:
$ lsb_release -a
You should see this output:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
Now, run the following command to update all installed packages to the latest available version.
$ sudo apt update && apt upgrade
HAProxy is available on the default Ubuntu 20.04 repository. However, the available package is not the most recent stable version. Let’s check the HAProxy version if we want to install it from Ubuntu 20.04 repository.
$ sudo apt show haproxy
You can check the latest stable version at http://www.haproxy.org/#down. In this article, we are going to install HAPproxy 2.5, let’s run the following command to proceed with the installation.
$ sudo apt install software-properties-common
The software-properties-common package is probably already installed on your server, but there will be no issue if you run this command again.
$ sudo add-apt-repository ppa:vbernat/haproxy-2.5
This command puts the Personal Package Archive (PPA) into the list of apt sources. After adding the PPA to our APT source list, we can run the command below to complete the installation. You can replace the version number in the command above if you want to use another version of HAProxy.
$ sudo apt update $ sudo apt install haproxy
Once installed, we can check the version by running this command:
$ sudo haproxy -v
You should see this output:
HAProxy version 2.5.0-1ppa1~focal 2021/11/26 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2023.
Known bugs: http://www.haproxy.org/bugs/bugs-2.5.0.html
Running on: Linux 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64
By default, HAProxy is not configured to listen on a port number. In this step, since we are going to configure it as a reverse proxy and load balancer, we are going to make changes to the default HAProxy configuration.
$ sudo cp -a /etc/haproxy/haproxy.cfg{,.orig}
The command above will copy the file /etc/haproxy/haproxy.cfg
to /etc/haproxy/haproxy.cfg.orig
Now. let’s edit the file.
$ sudo nano /etc/haproxy/haproxy.cfg
And append these lines:
frontend haproxy-main bind *:80 option forwardfor default_backend apache_webservers backend apache_webservers balance roundrobin server websvr1 10.0.0.10:80 check server websvr2 10.0.0.20:80 check
Make sure to replace 10.0.0.10 and 10.0.0.20 with your actual webserver IP addresses. Save the file then exit.
Now, log in to your other two servers and install apache on the servers then create a default index file by running these commands.
$ sudo apt update; sudo apt install apache2 -y
On websvr1, run this command:
echo "Apache on backend server 1 is running
" |sudo tee /var/www/html/index.html
On websvr2, run this command:
echo "Apache on backend server 2 is running
" |sudo tee /var/www/html/index.html
Save the files then exit.
On your HAProxy server, restart the service:
$ sudo systemctl restart haproxy
The HAProxy server is now ready to accept and distribute the workload across the two apache servers. You can verify this by invoking a one-liner command in your HAProxy server.
$ while true; do curl localhost; sleep 1; done
As seen in the picture, the website is loaded both from websvr1 and websvr2.
If you want to see the statistics and see the information through the GUI, we can configure HAProxy and enable the monitoring function.
Open HAProxy configuration file.
$ sudo nano /etc/haproxy/haproxy.cfg
Then append these lines.
listen stats bind :8800 stats enable stats uri / stats hide-version stats auth rosehosting:m0d1fyth15 default_backend apache_webservers
Pay attention to the stats auth
part. This is where you specify the login name and password. Change the login name and password to a stronger password. Save the file, exit and restart HAProxy.
$ sudo systemctl restart haproxy
Now, you can navigate to http://YOUR_HAPROXY_IP_ADDRESS:8800
to see the statistics, you will be asked for the username and password you specified earlier in /etc/haproxy/haproxy.cfg
.
That’s it. You have successfully installed HAProxy on your Ubuntu VPS. For more information about HAProxy, please refer to the HAProxy website.
If you are one of our web hosting customers and use our optimized Linux Hosting, you don’t have to install HAProxy On Ubuntu 20.04 by yourself, our expert Linux admins will set up and configure HAProxy on your Ubuntu VPS, for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post, on how to install HAProxy on Ubuntu 20.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.
The post How to Install and Configure HAProxy on Ubuntu 20.04 first appeared on LinuxHostSupport.
One of the most critical gaps in traditional Large Language Models (LLMs) is that they…
Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…
What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…
Wouldn’t it be wonderful to wake up one day with a desire to explore AI…
Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…
Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…