Setup HA Proxy on Ubuntu Server in AWS
HA Proxy is a proxy and software load balancer with plenty of features including SSL termination , TCP load balancer and traffic regulator.
This post focusses on installing HA proxy on Ubuntu running in AWS.
Prerequisite
- EC2 instance with Ubuntu OS
Note: I have launched an EC2 instance of t2.micro with Ubuntu 24.04 LTS version .
The security group associated with EC2 instance is modified to allow traffic on port 80. This is the port on which HA Proxy will be listening for incoming requests.
Step — Install HA Proxy on Ubuntu
I have used HA Proxy 2.9 for this post.
If you want to use different combination of Linux and HA Proxy version refer the following link. https://haproxy.debian.net/#distribution=Ubuntu&release=noble&version=2.9
# prevents the download of recommended packages that aren't required by the package being installed, upgraded, reinstalled, or installed
sudo apt-get install — no-install-recommends software-properties-common
# Enable dedicated Personal Package Archive
add-apt-repository ppa:vbernat/haproxy-2.9
# install haproxy 2.9 and stick to 2.9 version for future updates
sudo apt-get install haproxy=2.9\*
# check haproxy version
haproxy -v
# check the haproxy status
service haproxy status
Step — Configure HA proxy
HA Proxy configuration is managed through /etc/haproxy/haproxy.cfg file.
The sample content of the file is shown below.
HA Proxy configuration consist of four key sections. The names are intuitive enough to understand the impact of configuration
- global — global parameters are process wide and often OS-specific
- defaults — http mode and various default timeouts , note the various errorfiles configuration (e..g 503.http)
- frontend — receives traffic from external world and it dispatches to backend
- backend — set of servers (pool) which receives the requests from HA Proxy
Generally configuration of frontend is the first step. The default configuration does not have any frontend section so let us configure it to listen on all IP addresses and on port 80.
# restart HAProxy
sudo systemctl restart haproxy
If you do curl to localhost then HA Proxy tries to send it to any configured backend. However there is no backend configured so it shows the 503 error message.
If you go to the public ip of EC2 instance the same message is shown in the browser.
That is all for this post !
This is good enough to start hacking HA Proxy and configure it to meet your requirements. I personally like HA Proxy as it is feature rich , robust and easy to configure software load balancer. The configuration manual is available at https://docs.haproxy.org/2.9/configuration.html