I am stuck with an issue and im hoping someone can point me in the right direction.

I have a server that hosts multiple sites using Apache Virtual hosts (shared IP's). A few months ago my server provider had downtime at the Data Center making my server and all its sub accounts unavailable.. Thanks!

A lot of the accounts are not that critical however there are 1 or 2 which I would like to make a bit more resilient/highly available. I have been playing with cloud server instances (amazon and digital ocean) and HAProxy and got a good test system up and running:

4 servers:

  1. 1 x HA Proxy LB
  2. 2 x PHP/HTTP App Servers (Mirroed using RSync)
  3. 1 x MySQL DB

However I am now trying to do this with the virtual host account and i'm experiencing problems.. An exercpt from my HA Proxy config file:

backend AppServers 
balance   roundrobin 
mode   http 
server TestServer1 check 
server TestServer2 check backup 

**I've swapped out the IP's for the purpose of this post.*

A diagram of what I am trying to achieve:

load balance virtual host account and cloud server

Im not a professional with HAProxy and most of the routes I have tried have failed.

Has anyone ever done this and succeeded? or know what im doing wrong?


Yes this sound like a pretty simple one to solve from what I understand.

I would first address your haproxy configuration and make a tad more efficient by using ACL's to detect if the requested account is a critical or not critical :-)

You will need to add this to your front end:

acl critical_1 path_beg /~accname1
acl critical_2 path_beg /~accname2

use_backend AppServers_Critcal if critical_1
use_backend AppServers_Critcal if critical_2

default_backend AppServers

Then add these backends.

backend AppServers_Critcal
balance   roundrobin 
mode   http
server TestServer1 check inter 10s fall 3 
server TestServer2 check backup 

backend AppServers
balance   roundrobin 
mode   http
server TestServer1 check 

So a little overview of what is happening here; when the user requests a page it will see if the url begins with a account name that is deemed critical, if so then the user will use the critical server farm which has a backup server specified. However if the account requested is a non critical account it will be sent to the standard server farm.

In addition you may add option forwardfor in addition to any other options you may have on the backends to ensure the real IP of the client is forwarded to the server. (If you do choose to do this then the server will need mod-rpaf to read the headers sent via the proxy)

(inter 10s fall 3) You may be wondering what exactly it does; well in short it simply waits 30 seconds and then moves on to the backup.

So it waits ten seconds then check for a consecutive three times before giving up and moving on to the backup server.

(Rsync Guide) Secondly if you have not already addressed the issue of replicating / synchronizing the server to a remote location via rSync you can follow the steps below to setup a simple automatic system.

First you will need to install rSync & SSH if not already installed. (On both the local & remote server)

(RedHat Based Operating System : Below)

# yum -y install ssh rsync

(Debian Based Operating System : Below)

# sudo apt-get install ssh rsync

Once these packages have been installed you can now create a keyless link between both servers which will allow you to connect without a password from the local machine we are synchronizing to the remote location.

** However please note this is not best practice neither is it the safest however it is one of the simplest methods to achieve full automation. **

(Part 1) On the local machine you will need to run this command to generate a ssh-keygen file.

# ssh-keygen

This command will then prompt you to answer few questions; this process is pretty straight forward, all you need to do is press the enter key for each question.

(Part 2)

Next we will copy the public key to the remote host using the following command.

ssh-copy-id -i ~/.ssh/id_rsa.pub admin-user@ #(Your remote server IP)

It will then prompt you for that server's ssh login password (Using the defined user above; in our case admin-user)

Now were done with that; you will now notice you can ssh to the remote server without it asking for a password, which is what we need to achieve full automated synchronization.

Now we can move back to the rSync.

We can sync the files we need by running this command

rsync -av --delete -e ssh /websites admin-user@

and simply we can even add this to crontab in order to make this process run every X days or hours.

A simple example is the one below which will run this command every day at midnight.

00 00 * * * rsync -av --delete -e ssh /websites admin-user@

This can be added via typing

crontab -e

I hope this is of help!

  • Hi Jnrprimo thanks for your in depth reply, I believe there may be some confusion on your solution (maybe I didnt explain it correctly). Lets say the domain testdomain.co.uk is pointing to my shared server with the multiple accounts (Virtual Hosts) with shared IP's. When I define the backend IP's (EG it just shows a default website page and not the site thats meant to be there.) As the IP's are shared how can I provide this in the HA Proxy config.
    – rosey85uk
    Aug 5 '15 at 11:41
  • No problem. So If I understand correctly you would like HaProxy to display content from a backend IP which is contained within a folder (Basically?) . (E.g. testdomain.co.uk -> HaProxy -> If so I think I may have a soluton :-) Aug 5 '15 at 12:31
  • All the accounts have their own /home/accname/ directory, and to view the website you would navigate to to view it. but when I supply that in the haproxy config it doesnt show the webpage, just a deafult website page :-/
    – rosey85uk
    Aug 5 '15 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.