Using cPanel (WHM) to Manage a Cluster

WHM is a decent server management tool. It’s paid, but the developers did a solid job. It’s fairly common to install WHM/cPanel on a single server that holds the databases, site files, and user accounts all together. But what do you do if you have several servers and a separate database server?
At first glance it’s dead simple - install WHM/cPanel on several servers and enjoy life. But that’s only at first glance. In this article I want to describe an approach to setting up a server cluster where WHM/cPanel is used to manage the configuration.

On the first/main server:

  • Install cPanel on the main server.
  • Set up an nfs server on it
  • Mount the home folder and /usr/local/apache/conf on the second server.

Generate ssh keys on the first server for the root user using ssh-keygen

Copy its public part to the second server:

scp ~/.ssh/id_rsa.pub root@**remote_ip**:~/.ssh/

Now our main server can connect to the second one without a password.

Now we need to tweak the apache host template on the first/main server:

nano /var/cpanel/templates/apache2_2/vhost.default  
nano /var/cpanel/templates/apache2_2/ssl_vhost.default

In the very first line add the following right after [% ipblock.ip %]:[% ipblock.port %]

*:[% ipblock.port %]

As a result the first line of the file will look like this:

<VirtualHost[% FOREACH ipblock IN vhost.ips %] [% ipblock.ip %]:[% ipblock.port %] *:[% ipblock.port %][% END %]>

Copy the files to the second server

rsync -Hogva /opt/* root@**remote_ip**:/opt/  
rsync -Hogva /usr/local/cpanel root@**remote_ip**:/usr/local/  
rsync -Hogva /var/cpanel root@**remote_ip**:/var/  
rsync -Hogva /usr/local/apache root@**remote_ip**:/usr/local/  
scp /etc/init.d/httpd root@**remote_ip**:/etc/init.d/  
scp /usr/bin/php* root@**remote_ip**:/usr/bin/

Create the files /usr/local/cpanel/scripts/postkillacct and /usr/local/cpanel/scripts/postwwwacctuser with the following content:

#!/bin/bash
scp /etc/passwd root@<strong>remote_ip</strong>:/etc/
scp /etc/group root@<strong>remote_ip</strong>:/etc/

ssh root@remote_ip '/etc/init.d/httpd restart'

Make them executable:

chmod +x /usr/local/cpanel/scripts/postwwwacctuser  
chmod +x /usr/local/cpanel/scripts/postkillacct

Create a hook that will run before executing easyapache:

nano /usr/local/cpanel/scripts/preeasyapache

Add the following lines to it:

#!/bin/bash
cp /etc/init.d/httpd /root/httpd_$(date %d-%m-%Y).bak

Create a hook that will run after executing easyapache:

nano /usr/local/cpanel/scripts/posteasyapache

Add the following lines to it:

#!/bin/bash
rsync -Hogva /opt root@<strong>remote_ip</strong>:/
rsync -Hogva /usr/local/cpanel root@<strong>remote_ip</strong>:/usr/local/
rsync -Hogva /var/cpanel root@<strong>remote_ip</strong>:/var/
rsync -Hogva /usr/local/apache --exclude=conf --exclude=conf.d --exclude=logs --exclude=domlogs root@<strong>remote_ip</strong>:/usr/local/
scp /usr/bin/php* root@<strong>remote_ip</strong>:/usr/bin/
mv /root/httpd_$(date %d-%m-%Y).bak /etc/init.d/httpd

Make them executable:

chmod +x /usr/local/cpanel/scripts/posteasyapache  
chmod +x /usr/local/cpanel/scripts/preeasyapache

What’s left is to create a custom hook that pokes apache on the remote server (I decided not to overthink it. I’ll just dumbly restart it):

nano /usr/local/cpanel/scripts/postrestartsrv_httpd
#!/bin/bash
ssh root@<strong>remote_ip </strong>'/etc/init.d/httpd restart'

Set the X flag:

chmod +x /usr/local/cpanel/scripts/postrestartsrv_httpd

All that’s left is to tell WHM to run this hook. Edit /usr/local/cpanel/scripts/restartsrv_apache with the following line:

exec '/usr/local/cpanel/scripts/postrestartsrv_httpd'

You need to add the ip addresses of all servers to the list of additional mysql hosts
Screenshot from 2014-09-12 15:09:02

On the second server

Create symlinks:

ln -s /usr/lib64/mysql/libmysqlclient.so.16.0.0 /usr/lib64/libmysqlclient.so.18  
ln -s /usr/local/apache/bin/apachectl /usr/sbin/apachectl

Install the needed packages:

yum install libtool-ltdl mysql mysql-libs remote_ip libXpm libpng libjpeg-turbo freetype aspell

If you’re using ImageMagic:

yum install lcms-libs
ln -s /usr/local/cpanel/3rdparty/bin/convert /usr/local/bin/convert

Restart apache. That’s it. Adding a 3rd, 4th, etc. server is done the same way. It’s pretty important to edit all the hooks so that changes made on the main server get replicated to the dependents.