How To Install HAProxy on Ubuntu 18.04 LTS

Install HAProxy on Ubuntu 18

HAProxy is a free HTTP/TCP high availability load balancer and proxy server. It spreads requests among multiple servers to mitigate issues resulting from 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 through the step by step installation HAProxy on an Ubuntu 18.04 bionic beaver.

Install HAProxy on Ubuntu 18.04 LTS Bionic Beaver

Step 1. First, make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Network Details.

To install and configure HAProxy load balancer on Ubuntu 18.04 we will consider three system as follows:
Web Server Details:

Server 1: web1.wpcademy.com 192.168.1.104
Server 2: web1.wpcademy.com  192.168.1.105
HAProxy Server:
HAProxy: haproxy 192.168.1.46

Step 3. Installing HAProxy on Ubuntu.

At first we will install HAProxy by executing below commands:

sudo add-apt-repository ppa:vbernat/haproxy-1.7
sudo apt update
sudo apt install haproxy

Step 4. Configuring HAProxy.

Now edit haproxy default configuration file /etc/haproxy/haproxy.cfg and start configuration:

nano /etc/haproxy/haproxy.cfg

At the end of the file we will add folowing information:

<em>frontend Local_Server
bind 192.168.1.46:80
mode http
default_backend webserver</em>

backend webserver
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server system1.osradar.com 192.168.1.104:80
server system2.osradar.com 192.168.1.105:80

To verify the configuration:

sudo haproxy -c -f /etc/haproxy/haproxy.cfg

If above command return output as “configuration file is valid” then restart HAProxy service:

systemctl restart haproxy

Step 5. Accessing HAProxy.

With the HAProxy configured and running, open your load balancer server’s public IP in a web browser and check that you get connected to your backend correctly. The parameter stats uri in the configuration enables the statistics page at the defined address:

http://192.168.0.46

Congratulation’s! You have successfully installed HAProxy. Thanks for using this tutorial for installing HAProxy load balancer in Ubuntu 18.04 bionic beaver system. For additional help or useful information, we recommend you to check the official HAProxy website.

How To Install HAProxy on Ubuntu 16.04 LTS

Install HAProxy on Ubuntu 16

HAProxy is a free HTTP/TCP high availability load balancer and proxy server. It spreads requests among multiple servers to mitigate issues resulting from 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.

Install HAProxy on Ubuntu 16.04 LTS

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 through the step by step installation HAProxy on a Ubuntu 16.04 (Xenial Xerus) server.

Step 1. First make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.

sudo apt-get update
sudo apt-get upgrade

Step 2. Network Details.

Below is our network server. There are 3 web servers running with Apache2 and listening on port 80 and one HAProxy server:
Web Server Details:

Server 1: web1.wpcademy.com 192.168.1.101
Server 2: web2.wpcademy.com 192.168.1.102
Server 3: web3.wpcademy.com 192.168.1.103
HAProxy Server:
HAProxy: haproxy 192.168.1.16

Step 3. Installing HAProxy.

Now install HAProxy with the following command:

apt-get -y install haproxy

After the installation you can double check the installed version number with the following:

haproxy -v

Step 4. Configuring HAProxy.

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

### nano /etc/haproxy/haproxy.cfg

global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon

# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private

# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3

defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http

frontend Local_Server
bind 192.168.1.16:80
mode http
default_backend My_Web_Servers

backend My_Web_Servers
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server web1.wpcademy.com 192.168.1.101:80
server web2.wpcademy.com 192.168.1.102:80
server web3.wpcademy.com192.168.1.103:80

listen stats *:1936
stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth username:password
stats uri /stats

Since you have done with all necessary configurations for proxy server, verify configuration file before restarting service using following command:

haproxy -c -f /etc/haproxy/haproxy.cfg

If above command return output as “configuration file is valid” then restart HAProxy service:

systemctl restart haproxy

Step 5. Accessing HAProxy.

Open your favorite browser and access port 80 on ip 192.168.0.16 (as configured above) in web browser and hit refresh. You will see that HAProxy is sending requests to backend server one by one (as per round robin algorithm).

Congratulation’s! You have successfully installed HAProxy. Thanks for using this tutorial for installing HAProxy load balancing on Ubuntu 16.04 LTS (Xenial Xerus) system. For additional help or useful information, we recommend you to check the official HAProxy web site.

You Might Also Like: How To Install and Configure HAproxy on CentOS 6

How To Install and Configure HAproxy on CentOS 6

Install and Configure HAproxy on CentOS 6

HAProxy is a free and open-source Linux application used for load balancing network traffic. 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. I will show you through the step by step installation of HAproxy on CentOS 6.x from source.

In this tutorial we will show you how to install and configuration of HAProxy on your CentOS 6 server.

Install and Configure HAproxy on CentOS 6

Step 1. First add yum repository your system.

HAProxy isn’t available in the default repositories for CentOS. In order for us to be able to install it, we need to either compile it from source (preferred) or add the EPEL repository to our server and install it using Yum.

#CentOS 6 – 32-bit
 rpm -Uvh http://mirror.overthewire.com.au/pub/epel/6/i386/epel-release-6-8.noarch.rpm

#CentOS 6 – 64-bit
 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Step 2. Install HAProxy using Yum.

 yum install haproxy

Step 3. Configuring HAProxy.

We have to modify the configuraion file of haproxy i.e. /etc/haproxy/haproxy.cfg as per our requirement. (Change this configuration as 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_wpcademy 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_wpcademy 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

Step 4. Once you have configured HAProxy, its time to start the service.

service haproxy start
chkconfig haproxy on

Step 5. Now you will able to browse your applicaiton using the IP of the haproxy server. For haproxy Status dashboard you have to browse the URL: http://192.168.2.102:31337. It will ask you for the username and password. Use the username and password you defined on the configuraion file as “stats auth”.

Congratulation’s! You have successfully installed HAProxy. Thanks for using this tutorial for installing HAProxy on CentOS 6 system. For additional help or useful information, we recommend you to check the official HAProxy web site.

You Might Also Like: How To Install Nginx Web Server On CentOS