CentOSLinuxTutorials

How To Install HAProxy on CentOS 7

Install HAProxy on CentOS 7

In this tutorial, we will show you how to install and configuration of HAProxy on your CentOS 7 server. For those of you who didn’t know, HAProxy is a free HTTP/TCP high availability load balancer and proxy server. It spreads requests among multiple servers to mitigate issues resulting from a single server failure. HA Proxy is used by a number of high-profile websites including GitHub, Bitbucket, Stack Overflow, Reddit, Tumblr, Twitter, and Tuenti, and is used in the OpsWorks product from Amazon Web Services.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation HAProxy on a CentOS 7 server.

Prerequisites

  • A server running one of the following operating systems: CentOS 7.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install HAProxy on CentOS 7

Step 1. First, let’s start by ensuring your system is up-to-date.

yum clean all
yum -y update

Step 2. Installing HAProxy.

HAProxy package is by default available in CentOs and RHEL Repositories. We can install it by using the yum command as follows:

yum -y install haproxy

Step 3. Configuring HAProxy.

We have to modify the configuration file of HAProxy i.e. /etc/haproxy/haproxy.cfg as per our requirement. (Change this configuration to your network requirements). For more configuration details check this url.

#nano /etc/haproxy/haproxy.cfg global

log 127.0.0.1 local0
log 127.0.0.1 local1 debug
maxconn 45000 # Total Max Connections. This is dependent on ulimit
user haproxy
group haproxy
daemon

defaults
timeout server 86400000
timeout connect 86400000
timeout client 86400000
timeout queue 1000s

# Configuration for HTTP site
listen http_idroot 192.168.2.102:80
mode http
balance roundrobin # Load Balancing algorithm
option httpchk
option forwardfor
server server1 192.168.2.100:80 weight 1 maxconn 512 check
server server2 192.168.2.101:80 weight 1 maxconn 512 check

# Configuration for HTTPS site listen  
https_idroot 192.168.2.102:443
mode tcp
balance source# Load Balancing algorithm
reqadd X-Forwarded-Proto:\ http
server server1 192.168.2.100:443 weight 1 maxconn 512 check
server server2 192.168.2.101:443 weight 1 maxconn 512 check

listen stats 192.168.2.102:31337
mode http
option httpclose
balance roundrobin
stats uri /
stats realm Haproxy\ Statistics
stats refresh 5s
stats auth admin:passwd123

Once you have configured HAProxy, its time to start the service:

systemctl start haproxy
systemctl enable haproxy

Step 4. Accessing HAProxy.

HAProxy will be available on HTTP port 8980 by default. Open your favorite browser and navigate to http://yourdomain.com:31337 or http://server-ip:31337. It will ask you for the username and password. Use the username and password you defined on the configuration file as “stats auth”. If you are using a firewall, please open port 31337 to enable access to the control panel.

Congratulations! You have successfully installed HAProxy. Thanks for using this tutorial for installing HAProxy on your CentOS 7 system. For additional help or useful information, we recommend you to check the official HAProxy website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Save

Save

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button