How to install the latest version of haproxy on centos 7

1) Define and download a new version of haproxy

You can view all current versions of haproxy on the page:

http://www.haproxy.org/download/2.0/src/
We just use the command below and for convenience write the latest version into the variable:
LATEST_HAPROXY=$(wget -qO-  http://www.haproxy.org/download/2.0/src/ | egrep -o "haproxy-2\.[0-9]+\.[0-9]+" | head -1)

Lets download and unpack

cd /usr/src/
wget http://www.haproxy.org/download/2.0/src/${LATEST_HAPROXY}.tar.gz
tar xzvf ${LATEST_HAPROXY}.tar.gz

2) Environment preparation and installation

Installing the necessary packages to build haproxy

yum install gcc-c++ openssl-devel pcre-static pcre-devel systemd-devel -y

Haproxy assembly and installation

cd /usr/src/${LATEST_HAPROXY}
make TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1 USE_SYSTEMD=1
mkdir /etc/haproxy
make install

Create a systemd service

cat > /usr/lib/systemd/system/haproxy.service << 'EOL'
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid"
ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE
ExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -q
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify

[Install]
WantedBy=multi-user.target
EOL
systemctl daemon-reload

3) Creating a haproxy config and launching

Let's create a config with tcp forwarding port 80 to 127.0.0.1

cat > /etc/haproxy/haproxy.cfg << 'EOL'
global
 log /dev/log local0
 log /dev/log local1 notice
 daemon


defaults
 log global
 option dontlognull
 timeout connect 50000
 timeout client  50000
 timeout server  50000


listen LietenName
        bind *:80
        mode tcp
        server YourServer 127.0.0.1:80
EOL

Haproxy launch

systemctl start haproxy

Check the status of the service

systemctl status haproxy

Complete script

LATEST_HAPROXY=$(wget -qO-  http://www.haproxy.org/download/2.0/src/ | egrep -o "haproxy-2\.[0-9]+\.[0-9]+" | head -1)
cd /usr/src/
wget http://www.haproxy.org/download/2.0/src/${LATEST_HAPROXY}.tar.gz
tar xzvf ${LATEST_HAPROXY}.tar.gz
yum install gcc-c++ openssl-devel pcre-static pcre-devel systemd-devel -y
cd /usr/src/${LATEST_HAPROXY}
make TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1 USE_SYSTEMD=1
mkdir /etc/haproxy
make install
cat > /usr/lib/systemd/system/haproxy.service << 'EOL'
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target


[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid"
ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE
ExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -q
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify


[Install]
WantedBy=multi-user.target
EOL


cat > /etc/haproxy/haproxy.cfg << 'EOL'
global
 log /dev/log local0
 log /dev/log local1 notice
 daemon


defaults
 log global
 option dontlognull
 timeout connect 50000
 timeout client  50000
 timeout server  50000


listen LietenName
        bind *:80
        mode tcp
        server YourServer 127.0.0.1:80
EOL
systemctl start haproxy
systemctl status haproxy