Installing Haproxy
This guide will help you install HAProxy for use instead of Apache’s mod_proxy_balancer. HAProxy is a highly configurable proxy server with tons of options that you can read all about on their website. We’re mainly interested in using HAProxy because it can tell which of your mongrels are ready to handle a request and avoid pileups on busy mongrels.
Impact to your website
You can configure and test HAProxy without impacting any applications you may already have running. When you’re ready to swap it in, you’ll just need to restart Apache. The downtime to the outside world will be very brief.
Getting HAProxy
The first step is to download, build and install the software.
wget http://haproxy.1wt.eu/download/1.2/src/haproxy-1.2.17.tar.gz
tar xvzf haproxy*
cd haproxy*
make TARGET=linux26
cp haproxy /usr/
sudo install —owner=root —group=root -m700 haproxy /usr/sbin/
Configuring the proxy server
Download this configuration file. At the bottom of the file, edit the listen section to use the correct mongrel ports. If you have more than one application, you can add additional listen blocks. For each additional listen block, increment the haproxy listen port (7001,7002,etc) and adjust the mongrel ports for the application (8000, 8010, 8020,etc).
Save the configuration to your server as /etc/haproxy.conf.
Now save this init script to /etc/init.d/haproxy, then run:
sudo chmod +x /etc/init.d/haproxy
sudo chkconfig haproxy on
sudo /sbin/service haproxy start
Test your configuration before you continue (you might need to ‘yum install -y curl’):
curl localhost:7001
You should see the HTML from the root URL of your application. If not, double check your settings and restart the proxy with sudo /sbin/service haproxy restart until you do.
Configuring Apache
Once things seem to be working, you can update your Apache configuration. This will be located on your server at /etc/httpd/conf/apps/APPNAME.conf. If you configured the application as the default vhost, then it will be at /etc/httpd/conf/default.conf
Be sure to backup your configuration just in case, and then:
- Remove the mod_proxy_balancer that start with: <Proxy balancer://APPNAME_cluster>
- Look for this line:
RewriteRule ^/(.*)$ balancer://APPNAME_cluster%{REQUEST_URI} [P,QSA,L] - And change it to this:
RewriteRule ^/(.*)$ http://localhost:7001%{REQUEST_URI} [P,QSA,L]
Be sure to use the correct listen port for your application. In the haproxy configuration template, we use 7001 for the first application.
Restart Apache to reload the configuration.
sudo /sbin/service httpd restart
Now check to see if your site is still up.
