Varnish

Varnish is a reverse HTTP proxy or web accelerator. Once configured on the clone server, varnish will redirect any web requests for /wp-admin or /wp-login to the master server. This ensures all admin modifications are performed on the master server. Once such a modification occurs lsyncd will replicate the change to the clone server. Pretty cool, right? It's starting to come together!

Log into the Clone Server

If you are not currently logged into your clone server, do so with the ssh command. Insert the IP address for your clone server where applicable.

ssh root@your.clone.public.ip.address

Install Varnish

First you need to use apt-get to install varnish.

aptitude install -y varnish

You will be editing the varnish config file so save the original file as a back up in case issues arise.

mv /etc/varnish/default.vcl /etc/varnish/default.vcl.backup

Now let's create our default.vcl file using the following shell script.

cat << EOF > /etc/varnish/default.vcl
backend default {
  .host = "127.0.0.1";
  .port = "8080";
}
backend master {
  .host = "your.master.private.ip.address";
  .port = "80";
}
sub vcl_recv {
  if (!req.http.X-Forwarded-For) {
    set req.http.X-Forwarded-For = client.ip;
  }
  if (req.restarts > 0 ||
      req.url ~ "wp-(admin|login)" ||
      req.http.Content-Type ~ "multipart/form-data") {
    set req.backend = master;
    return(pass);
  }
}
sub vcl_fetch {
  if (beresp.status == 404 && req.restarts == 0) {
    return(restart);
  }
}
EOF

Notice we replace the .host parameter with the private IP address of your master server.

One slight configuration will need to take place in the default varnish settings before you are ready to start the service. We need to set the DAEMON_OPTS section of /etc/default/varnishto listen to port 80 by swapping out 6081 with 80. Let's use another shell script to handle that automatically.

perl -pi -e 's/6081/80/;' /etc/default/varnish

Now, because we have varnish on our Clone server listening to port 80, we need to set apache to listen on port 8080. Create backups of the files in question.

cp /etc/apache2/ports.conf /etc/apache2/ports.conf.bak$(date "+_%Y%m%d")
cp /etc/apache2/sites-enabled/000-default /etc/apache2/sites-enabled/000-default.back$(date "+_%Y%m%d")

Switch out the port with the following sed scripts.

sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf
sed -i 's/NameVirtualHost \*:80/NameVirtualHost \*:8080/g' /etc/apache2/ports.conf
sed -i 's/VirtualHost \*:80/VirtualHost \*:8080/g' /etc/apache2/sites-enabled/000-default

Restart the apache2 service and start the varnish service. Once this is done you are ready to test.

service apache2 restart
service varnish restart