Best VPS Hosting | Website Hosting Pakistan | Domain & Hosting Services

Replication often hep webmasters to keep their services up and running when an unexpected event affects any of their servers. Setup sync between Apache web servers involves a series of steps.

As a part of our Server Management Services, we help our Customers with Apache related requests regularly.

Let us today discuss the steps to sync Apache web servers.

How to sync two Apache web servers?

In this write-up, we will 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.

We will also 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:

  • Set Up Apache
  • Set Up Databases
  • Install and Configure LSyncD
  • Set Up Apache Configuration Replication
  • Route Traffic

Let us look at each of these steps one by one.

Set Up Apache

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, run:

$ mkdir /etc/httpd/vhosts

Perform these steps on both the servers.

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

For instance, 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.

Set 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 run 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’”

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:

$ 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.

Lastly, enable and start lsyncd:

$ 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 copied over in just 5-10 seconds.

Set 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.

We can call 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”

Make sure 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

Route Traffic

Now, let us 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:

  • Create the document root on the first server.
  • Create any databases on the database server and set up SQL grants
  • Add a new virtualhost file to the /etc/httpd/vhosts/ folder
  • Add a new sync statement to /etc/lsyncd.conf for the new docroot
  • 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 any further assistance in fixing sync Apache web servers? – We’re available 24*7]

Conclusion

In short, setup of sync between Apache web servers involves a series of steps that include installation and configuration of LSyncD, set up Apache Configuration Replication, etc. Today, we saw how our Support Engineers set up basic one-way replication sync between two servers.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = “owonCMyG5nEQ0aD71QM”;



Source link

Author

Write A Comment