How to Install and Configure HAProxy on AlmaLinux 8

HAProxy is a free tool that helps websites handle more visitors by sharing the traffic between different servers, this keeps websites fast and prevents crashes, in this guide we will provide a full Tutorial Configure HAProxy on AlmaLinux 8, a secure and stable version of Linux, you can check more about HAProxy before starting the tutorial.
Installing and Configuring HAProxy on AlmaLinux 8
Prerequisites:
– A Linux VPS Server running AlmaLinux 8
– A non-root user with administrative privileges
-Access to your server using SSH
– An Internet connection
Step 1: Install HAProxy
Log into your AlmaLinux server using SSH, first you need to install HAProxy since it doesn’t come pre-installed, use the following command to download and install it from the default package manager.
sudo dnf install haproxy
When prompted with “Is this OK?“, type “Y” and press Enter to continue the installation.
Step 2: Enable and Start HAProxy
After the installation, you’ll want HAProxy to start automatically when your server boots up, use this command to enable it:
sudo systemctl enable haproxy.service
The output would be like this:
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
Step 3: Start and Run HAProxy
Now that HAProxy is installed and enabled, you have to start it so you can begin running with its default settings, to do this, run the command below, you can customize this configuration to fit your needs.
sudo systemctl start haproxy.service
Step 4: Verify that HAProxy is Running
To make sure HAProxy starts correctly and is working, you can check its status by using this command:
sudo systemctl status haproxy.service
If you see the Active (running) section in your output, it indicates that HAProxy is running properly on your server.
How to Configure HAProxy on AlmaLinux 8
First, you should open /etc/haproxy/haproxy.cfg, which is the HAProxy configuration file, using your preferred text editor:
sudo vi /etc/haproxy/haproxy.cfg
Now you need to press “i” to navigate to INSERT mode. Next, locate the line containing log 127.0.0.1 local2 and comment it out by adding a # to the beginning of the line.
You can comment it out by doing this:
. . . # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # #log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid . . .
Then add the following line after the commented-out line:
log /dev/log local0
The edited /etc/haproxy/haproxy.cfg section should have the following lines:
. . . # local2.* /var/log/haproxy.log # #log 127.0.0.1 local2 log /dev/log local0 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid . . .
The chroot command line restricts the HAProxy process to access files by confining to the/var/lib/haproxy directory, which adds a layer of security, The log /dev/log local0 line creates a file inside that directory that Rsyslog uses to collect entries from.
Now press ESC, type:wq, and then press Enter again to save and close the file.
Finally, you need to create the /var/lib/haproxy/dev directory because HAProxy expects a specific logging socket there, and it isn’t created automatically:
sudo mkdir /var/lib/haproxy/dev
The directory will contain the UNIX socket used for logging, In the last step, Restart HAProxy with the following command:
sudo systemctl restart haproxy.service
How to Configure Rsyslog to Collect HAProxy Logs on AlmaLinux 8
The default Rsyslog setup on AlmaLinux 8 does not automatically log HAProxy events, so you’ll need to open a new /etc/rsyslog.d/99-haproxy.conf file using your desired editor to collect reports from the HAProxy service:
sudo vi /etc/rsyslog.d/99-haproxy.conf
Now you need to press the “i” to navigate to INSERT mode and paste then the following command into the file:
$AddUnixListenSocket /var/lib/haproxy/dev/log # Send HAProxy messages to a dedicated logfile :programname, startswith, "haproxy" { /var/log/haproxy.log stop }
The command above creates a special communication path in the folder you chose, the part that says “programname, startswith, haproxy” tells Rsyslog to save HAProxy’s log messages, After that, save the file and close your editor. Next, you need to check if SELinux is controlling access on your AlmaLinux 8 system, run this command to see its status.
getenforce
If you get Permissive or Disabled output, you can restart Rsyslog by running the following command:
sudo systemctl restart rsyslog
How to Test HAProxy Logging on AlmaLinux 8
By default, the Haproxy package is sent with a configuration file that creates an HTTP listener socket on port 5000; the configuration points to an existing backend server, so any request to the port will result in an HTTP 503 error.
First, you should create an HTTP request to check if HAProxy is working properly, you can do this by running the following command in your terminal:
curl -si http://127.0.0.1:5000
Now you need to check /var/log/haproxy.log for each HTTP 503 response:
sudo grep -E 'NOSRV.+503' /var/log/haproxy.log
That is it.
Conclusion
By following this guide, you’ve learned how to install, configure, and test HAProxy on AlmaLinux 8, this powerful tool helps your server manage large amounts of traffic by spreading it across multiple servers, making your website or application faster and more reliable, you also learned how to set up logging so you can monitor HAproxy’s activity and spot problems early, whether you’re managing a personal project or a production server, setting up HAProxy probbly ensures better performance and staibility for your web services.
You might like it