Sync webservers using lsyncd

What is lsyncd?

Lyncd is a linux daemon which sync your local directory with remote machines or other local directory on your machine.

It requires source and destination folders to sync. It keeps watching on source directory and if it detects any changes on source it will copy them to destination directory.

If you want to learn how to sync local folders follow the link below:

How to sync local folders?

How to sync web servers?

If you are working on higher traffic websites you may be using load balancers. Let's imagine that you have following model where you have one load balancer and two web servers.

During deployment you have to physically log on to two web servers and deploy your code. Imagine when you horizontally scale your servers and add 5 more web servers.

Now, you have to deploy on 7 web servers the same code. It is annoying and to solve this issue we will take another approach.

We will have one deployment server which we use to deploy our code and from their code will be copied to other servers. Now, you have to deploy only two one server yupiee!!

How to sync two web servers?

Let's imagine that we have two web servers and we want to sync them I mean we want to make sure both of them uses the same code.

Let say we bought a new server and we use it only for deployment purpose and later our deployment server will be in sync with other two servers.

Copy ssh keys to remote servers

First of all we will login to our deployment server. Once we logged on we will generate ssh key and we will copy these generated ssh key to other web servers.

Once we copy our deployment server key and put them on other webserver our deployment server will have access to those remote servers.

# log on to deployment server
$ ssh root@deployment_server_ip

# create your ssh key on deployment server
$ ssh-keygen -t rsa -b 4096 -C ""

# copy your generated ssh key to webserver-1
$ ssh-copy-id root@webserver_1_IP

# copy your generated ssh key to webserver-2
$ ssh-copy-id root@webserver_2_IP​

Install lsyncd on deployment server

Now, we have to install lsyncd daemon on our deployment server because we want deployment server to copy all the deployed changes and put them on other web servers.

If you want to know how to install lsyncd daemon follow the link below:

Install lsyncd daemon on ubuntu

Once you installed the lsyncd daemon on our deployment server. Let's now configure our deployment server so that it knows if any changes have been detected it needs to be migrated to other servers.

Open lsyncd config file /ect/lsyncd/lsyncd.conf.lua and paste following changes:

settings = {
     logfile = "/var/log/lsyncd/lsyncd.log",
     statusFile = "/var/log/lsyncd/lsyncd.status"

remote_servers = {
   "webserver_1_IP",  -- webserver 1 ip
   "webserver_2_IP"   -- webserver 2 ip

for _, server in ipairs(remote_servers) do
  sync {
      rsync = {
          compress = true,
          archive = true,
          verbose = true,
          rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"

Finally, we need to restart our lsyncd daemon and try to deploy our code:

Deploying code using git

We will first restart our layncd daemon and then using git we will deploy our code to our deployment server then lsyncd will detect these newly deployed changes and will copy them to our web servers.

# start the daemon
$ sudo service lsyncd start

# install git
sudo apt-get install git

# go to local source folder where
# we need our lsyncd daemon to watch
cd /home/www/

# clone the repo make sure git user have read
# access to this git repo or deployment will fail
git clone​

Now, check the lsyncd logs to see if it is copying all changes to remote servers.

How to check running process for lsyncd?

To check the status of what is lsyncd doing in the background check the status file using tail command:

tail -f /var/log/lsyncd/lsyncd.status​