Compiling and Running HAProxy from Source on Ubuntu 14

Overview

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.

 

Preparing Ubuntu

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 actually compile source code.

  1. Install the build-essential package.
    sudo apt-get install build-essential
  2. Install libssl-dev to allow SSL functionality
    sudo apt-get install libssl-dev
  3. Install PCRE development library for C++, used to improve regex performance.
    sudo apt-get install libpcre++-dev
  4. 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 website of the project – 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 tarball can be downloaded using wget. To download version 1.5.12, you can use the following command.

wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.12.tar.gz

Compiling HAProxy

  1. Extract the haproxy source files from the downloaded tar file.
    tar xvf haproxy-1.5.12.tar.gz
  2. Change into the extracted directory.
    cd haproxy-1.5.12
  3. 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
  4. 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 essentially 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.

      1. Create a new file upstart file for HAProxy.
        sudo touch /etc/init.d/haproxy
      2. Make the file executable.
        sudo chmod +x /etc/init.d/haproxy
      3. Open the file in a text editor
        sudo vi /etc/init.d/haproxy
      4. 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.

    1. 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.

 

Starting HAProxy

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