Although you can install and run HAProxy from the default package repository for Ubuntu, the version available is usually out of date. Now if you are more interested in stability than features this may be fine. For those who are looking to use new features only available in more recent versions, you will need to compile HAProxy on your own. Fear not, this is actually super easy to accomplish and doesn’t take much effort at all.
Before you can compile HAProxy, you will need to install a few packages. One contains development libraries that allow the usage of SSL, another improves the performance of regex styled filtering in the HAProxy configuration file, and finally the third contains everything necessary to actual compile source code.
- Install the build-essential package.
sudo apt-get install build-essential
- Install libssl-dev to allow SSL functionalily
sudo apt-get install libssl-dev
- Install PCRE development library for C++, used to improve regex performance.
sudo apt-get install libpcre++-dev
- Create the user account that HAProxy will run as. This account should standard user privileges for security reasons. Running as Root can be done, but it is strongly discouraged!
sudo useradd haproxy
Download HAProxy Source
The source files for HAProxy can be downloaded from the projects website – http://www.haproxy.org. During the time this tutorial was written, the most recent stable version available was 1.5.12. The HAProxy project is very active, so it is very likely that the version you see will be higher.
The tar ball can be downloaded using wget. To download version 1.5.12, you can use the following command.
- Extract the haproxy source files from the downloaded tar file.
tar xvf haproxy-1.5.12.tar.gz
- Change into the extracted directory.
- Compile HAProxy. The options I use optimize the binary for the processor architecture used on the server, to optimize for Linux kernel 3.00+, allow SSL, and use PCRE.
make TARGET=linux2628 CPU=native USE_STATIC_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1
- Install the newly compiled program.
sudo make install
HAProxy Upstart Script
The Upstart script is what allows us to start and stop the service. It is essential just a bash script that accepts predefined arguments for each action that the service will accept. Unfortunately, although the HAProxy source tar contains an example, it is written specifically for Red Hat \ CentOS servers. For Ubuntu, you will have to craft your own. Thankfully, there are plenty of examples available. The following is one I use for Ubuntu that I have modified to work with default install locations for HAProxy.
- Create a new file upstart file for HAProxy.
sudo touch /etc/init.d/haproxy
- Make the file executable.
sudo chmod +x /etc/init.d/haproxy
- Open the file in a text editor
sudo vi /etc/init.d/haproxy
- Copy the contents of the sample below and paste them into the file.
HAProxy Configuration File
Finally, we need a configuration file for HAProxy. If you are familiar with HAProxy, this is what defines how it will run and the web applications/services that will be proxied or load balanced. To get you started, I’ve included a sample configuration.
- Create the HAProxy directory in /etc.
sudo mkdir /etc/haproxy
- Create the configuration file.
sudo vi /etc/haproxy/haproxy.cfg
- Add the contents of the example below into your file.
- Save your changes and exit the text editor.
Everything is now in place. It’s time to test our deployment to ensure everything is working. To do that, we will start the service to ensure there are no errors.
sudo service haproxy start
If all went well you should HAProxy should have started and the following text outputted to the console.
* Starting haproxy haproxy