Configuring Heartbeat And Apache Part 2
In the last part of our high availability tutorial series we looked at configuring heartbeat to move an IP address between the two VPS servers we had set up, and also to restart Apache to use the IP address as it moves over. Unfortunately, heartbeat doesn’t do all the work for us, so we need to do some configuration work on Apache itself so that it is ready for use.
The first thing to do is ensure that Apache only listens on the additional IP address that is shared between the servers. This way your website can only be reached by people going to that IP, and not from visiting the server’s own IPs directly. We need to ensure that only the active server is being used by visitors at any time.
Apache is Listening
To start with, we need to modify Apache’s ports.conf file:
sudo nano /etc/apache2/ports.conf
Look for lines that start with the word “Listen” – there will be one that ends in 80 and some others with 443. The default configuration for Apache is that it listens in on all of the IP addresses configured on the server for connections. We can force Apache to only listen in on a specific IP address by prefixing the number after listen with the desired IP address and a colon, for example:
Listen 192.168.0.4:80
That line tells Apache to listen for incoming connections on the IP address 192.168.0.4 port 80. Replace the IP address with the additional IP address you have for your configuration, while keeping the number after the colon the same as the number that was originally on the listen line. Once the required changes are made, save the file and exit.
The above changes need to be made the same on both of your servers. Once these changes are made to both servers, we can then look at testing the failover.
Testing the Failover
Let’s create a file in a server’s web root:
sudo nano /var/www/html/index.php
Then paste in the following code:
<?php
$hostname = gethostname();
echo “This server is “.$hostname;
?>
Next, save and exit the file, ensuring it’s owned by the www-data user:
sudo chown www-data:www-data /var/www/html/index.php
Now let’s delete the default index page from the server:
sudo rm /var/www/html/index.html
If everything is working as expected, then Unison should take care of copying the new file from the server you created it on to the other and deleting the old index page.
Now is the time to start heartbeat on each server:
sudo systemctl start heartbeat
With this running, we can next visit the additional IP in a web browser:
http://192.168.0.4
You should see an output in your web browser along the lines of:
This server is vps01
The vps01 text should be replaced with the hostname that you gave one of your servers. That server is the one that is currently using the IP additional IP address.
Next you need to log into the server shown in the output from your web browser, and stop heartbeat there:
sudo systemctl stop heartbeat
This should cause the other server to take over the IP address and start serving web pages from it. It may take a short period of time for the failover to complete. You can find out whether it has worked by refreshing the page in your web browser, and the text should change to show the other server’s hostname. If this has worked as expected, you can start heartbeat again on this server.
Rebooting Heartbeat
The final step is to ensure that heartbeat restarts automatically following a reboot:
sudo systemctl enable heartbeat
With that working, your systems are configured and working in a highly available solution as desired with files and database data being transferred between them, as well as heartbeat allowing an IP to move between the servers. Unfortunately we aren’t quite finished yet: having the highly available solution is one stage, but the the other is to keep it running. So next time we’ll be looking at configuring some basic monitoring and alerting to alert you should one of the servers encounter a problem.