×


Steps to sync two Apache web servers

Webmasters generally perform replication to keep their services up and running when an unexpected event affects any of their servers. The process of setting up sync between Apache web servers involves a series of steps.
Here at Ibmi Media, as part of our Server Administration Services, we regularly help our Customers to solve Apache related issues.

In this context, we shall look into how to sync Apache web servers.

Steps to sync two Apache web servers?

Here in this guide, you will learn how to use Cloud Load Balancer, along with two VPS web servers, and one VPS database server, connected to each other via the Cloud Private Network with private IPs.

Let us assume that Apache is set up on each web server for name-based virtual hosting and is installed at "/etc/httpd/". PHP is also installed on both machines, and their configuration files are static and matched.

Finally, our database server has mariadb installed and running, and the firewall is open for external mysql connections.

The process to sync Apache servers involves the steps below:

i. Setting Up Apache.
ii. Configuring Databases.
iii. Installing and Configuring LSyncD.
iv. Setting Up Apache Configuration Replication.
v. Routing Traffic.

Now we will discuss each of these steps one by one.

1. Setting Up Apache

To begin, we will set up a single folder with single configuration files for each domain so that it can sync the Apache VirtualHost files. In our main Apache config file at "/etc/httpd/conf/httpd.conf", we will add the following line at the very end:

IncludeOptional vhosts/*.conf


This will allow Apache to load configuration files from the "/etc/httpd/vhosts/" folder. Now to create the vhosts file, from the command line, execute:

mkdir /etc/httpd/vhosts

Perform these steps on both servers.


In this folder, we can set up our individual domain configuration files. Make sure they end in ".conf" so that Apache will load them.

For example, make two files: "domain.com.conf" and "domain.net.conf". These both will be set up with valid VirtualHost blocks. The docroots of the domains are at "/var/www/domain.com/" and "/var/www/domain.net/".

We only need to make these configuration files on one server for now, since we will sync them later.

2. Setting Up Databases

For applications that require a dedicated database server, we now need to add grants so that all of the web nodes can connect.

For instance, after configuring the database user with a good strong password, we may need to execute the following grant statement( ( Assume IP1 and IP2 are private IPs of the web nodes):

mysql -e "grant all privileges on your_db.* to your_user@’IP1’; grant all privileges on your_db.* to your_user@’IP2’"


3. How to Install and Configure LSyncD

The Live Sync Daemon (lsyncd) can watch a folder for activity, and then replicate that activity with rsync in another local or remote location. We need to add the EPEL repository to install it via yum by running the following commands:

yum -y install epel-release
yum -y install lsyncd


Now that lsyncd is installed, let us configure it to sync vhost directory and docroots to other nodes. In the "/etc/lsyncd.conf" file, delete the example sync command, and set up the following block of data:

sync {
default.rsyncssh,
source = "/var/www/domain.com",
host = "IP2",
targetdir = "/var/www/domain.com",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
hard_links = true,
update = true
}
}


Similarly, set up blocks for "/var/www/domain.net" or any other running sites. Just add them all one after the other in the "/etc/lsyncd.conf" file.

Finally, enable and start lsyncd by running the following commands:

systemctl enable lsyncd
systemctl start lsyncd


Now, the contents of the directory will be available in the second server as well. Changes to the file will be effected in just 5-10 seconds.


4. Setting Up Apache Configuration Replication

For servers that configure new domains or update Apache configuration frequently, we need to set up a script to sync this and restart Apache on the other servers.

This can be called this from lsyncd as the rsync execution binary and have it run post-sync tasks. Create a file called "/root/vhostsync.sh" that looks like this(replace IP2 with the IP address of the second server):

#!/bin/bash
/usr/bin/rsync "$@"
[ $? -eq 0 ] && ssh IP2  “systemctl reload httpd”


Ensure that the file has the execute permissions.:

chmod 700 /root/vhostsync.sh


Now reload the Apache configuration on the second server. Then head back into "/etc/lsyncd.conf" and add this sync block:

sync {
default.rsyncssh,
source = "/etc/httpd/vhosts",
host = "IP2",
targetdir = "/etc/httpd/",
rsync = {
binary = "/root/vhostsync.sh"
}
}

Since we declare our script as the rsync binary, this script will execute instead of running rsync by itself. Reload lsyncd to add this sync to the config:


systemctl restart lsyncd


5. Routing Traffic

Here we will verify that the content being served from both machines is the same. For this,  connect to both the servers and verify the contents with via hosts file modification.


Then, we shall create the Cloud Load Balancer to route traffic to the IPs for the servers. Use the hosts file again to connect to the load balancer’s VIP to ensure we can reach the nodes.


After testing, public DNS for the hosted domains can be changed to the VIP of the load balancer, allowing traffic to be routed into the server cluster.

This only sets up replication from the first server to the second, not vice versa. Therefore, make sure to upload content/ make changes to the website from the first server.

Thus, to add a new domain to the servers, we will need to do the following:

i. Create the document root on the first server.
ii. Create any databases on the database server and set up SQL grants
iii. Add a new virtualhost file to the "/etc/httpd/vhosts/" folder
iv. Add a new sync statement to "/etc/lsyncd.conf" for the new docroot
v. Restart lsyncd and Apache on the first server.

Additionally, set up new blocks to the "etc/lsyncd.conf" while adding or removing a server. Also, set up a new vhostsync wrapper for restarting Apache on those new nodes after configuration sync.

[Need More support in fixing sync Apache issues? – We are available to help you today.]


Conclusion

This article will guide you on how to setup syncing Apache web servers which involves a number of procedures such as installation and configuration of LSyncD, setting up Apache Configuration Replication, and so on.