
If you have started an internet business and you want to keep your site in the best possible condition, one of the most important things to pay attention to is the speed of the site. There are many programs and mechanisms used to do this, and HAProxy is one of them. After reading this post, you will learn how to configure HAProxy on AlmaLinux 8.
What is HAProxy?
HAProxy stands for High Availability Proxy and is a free and open-source program that can change the URLs on the server through the URL change mechanism, and web servers can optimally serve these incoming requests. HAProxy uses a technique called URL Rewriting to do this. When a request enters the server, it actually needs to link to a URL and retrieve information from the site it needs. HAProxy distributes requests evenly across multiple servers to balance.
HAProxy is one of the most popular load-balancing software that runs on Linux Solaris and FreeBSD operating systems. Load Balancer increases the efficiency and availability of your servers by distributing the load on the servers. HAProxy is used in GitHub, Instagram, Twitter, and so on. Also, HAProxy is one of the most effective server-side programs that can take control of server traffic and work in such a way that many requests do not disrupt the overall operation of servers and sites.
HAProxy Features
– Transparent Proxy: This allows the user to connect directly to the server.
– Content Inspections: This feature allows users to block unwanted protocols.
– CLI For Server Management: You can make changes to the server environment with this feature such as activating and deactivating the server inside HAProxy.
– Support various Protocols: HAProxy is compatible with various protocols such as HTTP, HTTP/2, gPRC, and FastCGI.
– HTTP Authenticator: The authentication process in HTTP is very simple and basic. All you have to do is enter your Username and Password to be allowed to log in.
Prerequisites to Install HAProxy on AlmaLinux 8
– A Linux VPS Server running AlmaLinux 8
– A non-root user with administrative privileges
How to Install HAProxy on AlmaLinux 8
Before doing anything, you should launch an Interactive Terminal. First, you need to install HAProxy with the following command:
sudo dnf install haproxy
In response to the question “Is this OK?“, enter “Y“.
After the installation, you should enable HAProxy:
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.
Now You have to start HAProxy by running the following command. It will run with a default configuration on the server.
sudo systemctl start haproxy.service
You can check the status of HAProxy with the following 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 in your desired editor:
sudo vi /etc/haproxy/haproxy.cfg
Now you need to press “i” to navigate to INSERT mode. After that find the line log 127.0.0.1 local2 and add 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 restricts the HAProxy process to access files in the /var/lib/haproxy directory. 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 it does not exist by default:
sudo mkdir /var/lib/haproxy/dev
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 configuration in AlmaLinux 8 does not handle HAProxy reports, you 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 should press “i” to navigate to INSERT mode and paste 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 }
A Unix domain socket is created in the specified location using the above command. The :programname, startswith, “haproxy” section specifies the file that Rsyslog writes the log entries to that it collects from the socket. Then save the file and exit.
Next, you need to determine if SELinux applies access control on AlmaLinux 8 system. You can check the current SELinux policy with the following command:
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 by entering the following command:
curl -si http://127.0.0.1:5000
Now you need to check /var/log/haproxy.log for each HTTP 503 response by executing the following command:
sudo grep -E 'NOSRV.+503' /var/log/haproxy.log
That is it.
Conclusion
This article introduced you to HAProxy and taught you how to install and configure HAProxy on AlmaLinux 8. After installation is complete, you can implement HAProxy in the production environment to achieve high performance and scalability.