Installing HAProxy From Source On CentOS 7

By | 2019-04-27

The CentOS 7 repositories provide HAProxy version 1.5. You may want or need features in newer versions. Fortunately, installing HAProxy 1.9.7 from source is relatively straightforward.

The steps to do so are:

  • Install required packages
  • Download the Lua source code
  • Download the HAProxy source code
  • Compile and install Lua
  • Compile and install HAProxy
  • Create an unprivileged service account
  • Create a SystemD unit file
  • Create a SystemD environment file
  • Create a test configuration file
  • Test your installation
  • Configure HAProxy for your environment
  • Enable HAProxy to start automatically on boot

All commands in this guide should be executed as the root user.

Install Required Packages

In order to compile HAProxy, you will need to ensure a few packages from the standard repositories are installed. Run the following command as root to do so:

[root@centos7 ~]# yum install gcc openssl-devel readline-devel systemd-devel make pcre-devel

Download and Extract Source Code

Use the commands below to download HAProxy 1.9.7 and Lua 5.3.5. The version of Lua in the CentOS 7 repositories is too old for HAProxy 1.9.7.

[root@centos7 ~]# cd
[root@centos7 ~]# curl https://www.lua.org/ftp/lua-5.3.5.tar.gz > lua-5.3.5.tar.gz
[root@centos7 ~]# curl http://www.haproxy.org/download/1.9/src/haproxy-1.9.7.tar.gz > haproxy-1.9.7.tar.gz

Extract the source archive files:

[root@centos7 ~]# tar xf lua-5.3.5.tar.gz
[root@centos7 ~]# tar xf haproxy-1.9.7.tar.gz

Install Lua 5.3.5

The latest versions of HAProxy need a version of Lua that is newer than the one provided in the yum repositories. Installing the latest version of Lua should only take a few minutes.

Change your working directory to the directory created by tar when you extracted the archive:

[root@centos7 ~]# cd lua-5.3.5

Lua can be compiled and installed with a single command:

[root@centos7 lua-5.3.5]# make INSTALL_TOP=/opt/lua-5.3.5 linux install

If you want an explanation of the make arguments or wish to alter them, see the Lua 5.3 readme page for details.

Install HAProxy 1.9.7

Change your working directory to the directory created by tar when you extracted the archive:

[root@centos7 lua-5.3.5]# cd 
[root@centos7 ~]# cd haproxy-1.9.7

Compile HAProxy:

[root@centos7 haproxy-1.9.7]# make USE_NS=1 \
USE_TFO=1 \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_LUA=1 \
USE_PCRE=1 \
USE_SYSTEMD=1 \
USE_LIBCRYPT=1 \
USE_THREAD=1 \
TARGET=linux2628 \
LUA_INC=/opt/lua-5.3.5/include \
LUA_LIB=/opt/lua-5.3.5/lib 

If you wish to see a full list of make arguments along with short descriptions, run the command:

[root@centos7 haproxy-1.9.7]# make help

Install HAProxy to /opt/haproxy-1.9.7:

[root@centos7 haproxy-1.9.7]# make PREFIX=/opt/haproxy-1.9.7 install

Create Unprivileged HAProxy User and Group

Create an unprivileged user and group for HAProxy to run as. We will make them identical to the ones created by the HAProxy package in the standard yum repositories.

[root@centos7 haproxy-1.9.7]# groupadd -g 188 haproxy
[root@centos7 haproxy-1.9.7]# useradd -g 188 -u 188 -d /var/lib/haproxy -s /sbin/nologin -c haproxy haproxy

If these commands have the output shown below, HAProxy was or is installed from the repositories. This will not affect source installation. It is safe to ignore this and move to the next step.

[root@centos7 haproxy-1.9.7]#  groupadd -g 188 haproxy
groupadd: group 'haproxy' already exists
[root@centos7 haproxy-1.9.7]#  useradd -g 188 -u 188 -d /var/lib/haproxy -s /sbin/nologin -c haproxy haproxy
useradd: user 'haproxy' already exists

Create SystemD Unit File

Use your favorite text editor to create the SystemD unit file that controls how HAProxy will be started, reloaded, stopped, and monitored. If you are on a minimal system, it is likely vi is all that is available. If you aren’t familiar with vi, I wrote a short tutorial that should teach you enough for basic tasks such as this.

Create the file /etc/systemd/system/haproxy-1.9.7.service with the following contents:

[Unit]
Description=HAProxy 1.9.7
After=syslog.target network.target

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/haproxy-1.9.7
ExecStart=/opt/haproxy-1.9.7/sbin/haproxy -f $CONFIG_FILE -p $PID_FILE $CLI_OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -USR1 $MAINPID

[Install]
WantedBy=multi-user.target

The USR2 signal instructs HAProxy to reload its configuration without bringing it down. USR1 brings down HAProxy, allowing processes to finish what they were doing before exiting.

Create the SystemD Environment File

Create the file /etc/sysconfig/haproxy-1.9.7 with the following contents:

# Command line options to pass to HAProxy at startup
# The default is:  
#CLI_OPTIONS="-Ws"
CLI_OPTIONS="-Ws"

# Specify an alternate configuration file. The default is:
#CONFIG_FILE=/etc/haproxy/haproxy-1.9.7.conf
CONFIG_FILE=/etc/haproxy/haproxy-1.9.7.conf

# File used to track process IDs. The default is:
#PID_FILE=/var/run/haproxy-1.9.7.pid
PID_FILE=/var/run/haproxy-1.9.7.pid

The -Ws option runs HAProxy in a mode where it is able to notify SystemD when it is done starting.

Load the configuration with the following command:

[root@centos7 haproxy-1.9.7]# systemctl daemon-reload

Create an HAProxy Configuration File

First, create the directory that will store HAProxy’s configuration files:

[root@centos7 haproxy-1.9.7]# mkdir /etc/haproxy

Create the file /etc/haproxy/haproxy-1.9.7.conf with the following contents:

global
    daemon
    maxconn 256
    user        haproxy
    group       haproxy
    chroot      /var/lib/haproxy

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http
    bind *:8000
    default_backend servers

backend servers
    server server 127.0.0.1:81

Start and Test HAProxy

Start HAProxy:

[root@centos7 haproxy-1.9.7]# systemctl start haproxy-1.9.7.service

Check to see if it is running:

[root@centos7 haproxy-1.9.7]# ps -ef | grep haproxy | cut -c 1-100
root      6708     1  0 Apr25 ?        00:00:02 /opt/haproxy-1.9.7/sbin/haproxy -Ws -Ds -f /etc/haproxy/haproxy-1.9.7.conf -p /var/run/haproxy-1.9.6.pid
haproxy   6709  6708  0 Apr25 ?        00:00:01 /opt/haproxy-1.9.7/sbin/haproxy -Ws -Ds -f /etc/haproxy/haproxy-1.9.7.conf -p /var/run/haproxy-1.9.6.pid

Configure HAProxy

The details of configuring HAProxy beyond a simple test are beyond the scope of this guide. Consult the official documentation for help. If you are new to HAProxy, the starter guide will help get you started.

Configure HAProxy to Start at Boot

[root@centos7 system]# systemctl enable haproxy-1.9.7
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy-1.9.7.service to /etc/systemd/system/haproxy-1.9.7.service.

References