[#]: subject: "How to Install and Configure HAProxy on Ubuntu 22.04" [#]: via: "https://www.linuxtechi.com/install-configure-haproxy-on-ubuntu/" [#]: author: "Pradeep Kumar https://www.linuxtechi.com/author/pradeep/" [#]: collector: "lkxed" [#]: translator: " " [#]: reviewer: " " [#]: publisher: " " [#]: url: " " How to Install and Configure HAProxy on Ubuntu 22.04 ====== In this post, we will demonstrate how to install HAProxy on Ubuntu 22.04 (Jammy Jellyfish) step by step. We will later configure it to act as a load balancer by distributing incoming requests between two web servers. ##### What is HAProxy? HaProxy, short for High Availability Proxy, is a free and open-source HTTP load balancer and reverse-proxy solution that is widely used to provide high availability to web applications and guarantee maximum possible uptime. It is a high-performance application that injects performance improvements to your web apps by distributing traffic across multiple endpoints. This way, it ensures that no webserver is overloaded with incoming HTTP requests since the workload is equitably distributed across several nodes. While free, the Enterprise Edition provides added features such as WAF ( Web Application Firewall ), application acceleration, advanced DDoS protection, advanced health checks and so much more. ##### Lab setup To demonstrate HAProxy in action, you need to have at least three Linux systems. One will act as the HAProxy load balancer, while the rest will act as web servers. ![Haproxy-Lab-Setup-Ubuntu][1] ### Step 1) Install HAProxy Load Balancer The first step is to install HAProxy on Ubuntu. Ubuntu repositories provide HAProxy by default, but it is not the latest one. To view available haproxy package version from default repositories, run ``` $ sudo apt update $ sudo apt show haproxy ``` ![default-haproxy-version-ubuntu-22-04][2] But the latest long term support release is HAProxy is 2.6, So to install HAProxy 2.6, first enable PPA repository, run following command ``` $ sudo add-apt-repository ppa:vbernat/haproxy-2.6 -y ``` Now install haproxy 2.6 by executing the following commands ``` $ sudo apt update $ sudo apt install -y haproxy=2.6.\* ``` Once installed, confirm the version of HAProxy installed as shown. ``` $ haproxy -v ``` ![Haproxy-version-ubuntu-22-04][3] Upon installation, the HAProxy service starts by default and listens to TCP port 80. To verify HAProxy is running, run the command ``` $ sudo systemctl status haproxy ``` ![Haproxy-Status-Ubuntu-Linux][4] It’s recommended to enable the service to auto-start on very system reboot as shown. ``` $ sudo systemctl enable haproxy ``` ### Step 2) Configure HAProxy The next step is to configure HAProxy to distribute traffic evenly between two web servers. The configuration file for haproxy is /etc/haproxy/haproxy.cfg. Before making any changes to the file, first, make a backup copy. ``` $ sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk ``` Then open the file using your preferred text editor. Here, we are using Nano. ``` $ sudo nano /etc/haproxy/haproxy.cfg ``` Haproxy configuration file is made up of the following sections: * global: This is the first section that you see at the very top. It contains system-wide settings that handle performance tuning and security. * defaults: As the name suggests, this section contains settings that should work well without additional customization. These settings include timeout and error reporting configurations. * frontend and backend: These are the settings that define the frontend and backend settings. For the frontend, we will define the HAProxy server as the front end which will distribute requests to the backend servers which are the webservers. We will also set HAProxy to use round robbin load balancing criteria for distributing traffic. * listen: This is an optional setting that enables you to enable monitoring of HAProxy statistics. Now define the frontend and backend settings: ``` frontend linuxtechi bind 10.128.0.25:80 stats uri /haproxy?stats default_backend web-servers backend web-servers balance roundrobin server web1 10.128.0.27:80 server web2 10.128.0.26:80 ``` Here, we have configured both the HAProxy server and the web server nodes to listed to port 80. Be sure to replace the IP address for the HAProxy and webservers with your setup. In order to enable viewing the HAProxy statistics from a browser, add the following ‘listen’ section. ``` listen stats bind *:8080 stats enable stats uri / stats refresh 5s stats realm Haproxy\ Statistics stats auth linuxtechi:[email protected] #Login User and Password for the monitoring ``` The stats auth directive specifies the username and password for the login user for viewing statistics on the browser. ![HAproxy-Config-File-Ubuntu][5] Now save all the changes and exit the configuration file. To reload the new settings, restart haproxy service. ``` $ sudo systemctl restart haproxy ``` Next edit the /etc/hosts file. Define the hostnames and IP addresses of the haproxy main server and the webservers. ``` 10.128.0.25 haproxy 10.128.0.27 web1 10.128.0.27 web2 ``` Save the changes and exit. ### Step 3) Configure Web Servers In this step, we will configure the remaining Linux systems which are the web servers. So, log in to each of the web servers and install the Apache web server package. ``` $ sudo apt update $ sudo apt install -y apache2 ``` Next, verify that Apache is running on each of the servers. ``` $ sudo systemctl status apache2 ``` Then enable Apache webserver to start on boot on both servers. ``` $ sudo systemctl enable apache2 ``` Next, modify the index.html files for each web server. For Web Server 1 Switch to the root user ``` $ sudo su ``` Then run the following command. ``` # echo "