How to Install HAProxy on Elementary OS Latest

HAProxy is a popular open source software that provides high availability, load balancing, and proxying for TCP and HTTP-based applications. In this tutorial, we will show you how to install HAProxy on Elementary OS Latest.

Prerequisites

Before starting the installation process, make sure you have the following:

Step 1: Update your system

Before we proceed with installing HAProxy, it's essential to update your system to ensure that we have the latest packages from the official repository. To update the system, run the following command in your terminal:

sudo apt-get update
sudo apt-get upgrade

Step 2: Install HAProxy

With the system updated, we can now proceed with the HAProxy installation. HAProxy can install using the apt package manager. Use the following command to install HAProxy:

sudo apt-get install haproxy

After the installation is complete, you can verify the installation by checking the HAProxy version with the following command:

sudo haproxy -v

Step 3: Configure HAProxy

Now that HAProxy has installed, we need to configure it to work with our application server, for example, Apache, NGINX or another HTTP server. The HAProxy configuration file stores in the /etc/haproxy/haproxy.cfg file. The default file is configured to work with IPv4, but if required, it can also configure to run over IPv6.

Open the file in your preferred text editor with sudo privilege.

sudo nano /etc/haproxy/haproxy.cfg

Update the configuration file as per your requirement. Below is an example configuration file that works with HTTP server such as NGINX:

global
        maxconn         10000
        log             127.0.0.1 local2

        ssl-default-bind-ciphers PROFILE=SYSTEM
        ssl-default-server-ciphers PROFILE=SYSTEM

defaults
        log             global

        mode            http
        option          httplog
        option          dontlognull
        retries         3
        option          redispatch
        timeout connect 5s
        timeout client  15s
        timeout server  15s

frontend lb-frontend
        mode    http
        bind    *:80
        option  http-server-close
        option  forwardfor
        default_backend lb-backend

backend lb-backend
        mode    http
        option  abortonclose
        option  httpchk GET /api-check
        balance roundrobin
        server  web1 192.168.0.4:80 check inter 2000 rise 2 fall 5
        server  web2 192.168.0.5:80 check inter 2000 rise 2 fall 5

After you have made the changes, save the file and exit the text editor.

Step 4: Start HAProxy Service

After configuring the file, you can start the HAProxy service by running the following command:

sudo systemctl start haproxy

You can check the status of the service by running the following command:

sudo systemctl status haproxy

You should see an output similar to this if everything has configured correctly:

haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-09-28 01:52:06 EDT; 1min 34s ago
     Docs: man:haproxy(1)
           file:/usr/share/doc/haproxy/configuration.txt.gz
 Main PID: 1230 (haproxy)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/haproxy.service
           ├─1230 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
           └─1231 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid

Sep 28 01:52:06 ElementaryOS systemd[1]: Starting HAProxy Load Balancer...
Sep 28 01:52:06 ElementaryOS haproxy[1230]: [WARNING] 270/015206 (1230) : parsing [/etc/haproxy/haproxy.cfg:40] : 'option forwardfor' ignored for default-frontend 'lb-frontend' as it requires HTTP mode.
Sep 28 01:52:06 ElementaryOS haproxy[1230]: [WARNING] 270/015206 (1230) : parsing [/etc/haproxy/haproxy.cfg:41] : 'option httpclose' ignored for default-frontend 'lb-frontend' as it requires HTTP mode.
Sep 28 01:52:06 ElementaryOS systemd[1]: Started HAProxy Load Balancer.

Conclusion

That's it! You have now installed and configured HAProxy on Elementary OS Latest. You can now use HAProxy to manage and balance the load of your application servers. If you face any issues, refer to the official HAProxy documentation for more information.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!

Alternatively, for the best virtual desktop, try Shells!