Archive for the ‘Cpanel/WHM’ Category

Sync svn repo commits to website documentroot in Cpanel Server

June 8, 2013 Leave a comment


SITUATION: Customer has a cpanel server with one domain hosted on a shared ip and wants to setup svn repository for this domain in such a way that whenever the svn commit operation takes place, the contents of the repository is exported to the documentroot. Thus all updates to files inside documentroot can be done over svn instead of via ftp. Also use ‘svnserve’ daemon for setup and don’t use ‘mod_dav’.


1) Cpanel Server is used.
2) Domain is setup on a shared ip
3) Suphp is the php handler used
4) ‘username’ is the username of the website



1) Install subversion in cpanel server

yum install subversion.x86_64 -y

2) Create a directory named ‘repos’ inside the default documentroot of apache(ie, /usr/local/apache/htdocs/) and start the ‘svnserve’ daemon from that directory. Also make sure that the port 3690 is opened in firewall and you are starting the service as root user.

mkdir /usr/local/apache/htdocs/repos
svnserve -d -r /usr/local/apache/htdocs/repos

3) Create a repository named ‘username’ and import its home directory(/home/username/public_html) to the repository,

cd /usr/local/apache/htdocs/repos
svnadmin create username
cd ~
svn import /home/username/public_html file:///usr/local/apache/htdocs/repos/username -m "username"

4) Now open up the svn repository configuration file “/usr/local/apache/htdocs/repos/username/conf/svnserve.conf” and disable anonymous access and specify the user authentication and authorization files,

anon-access = none
auth-access = write

password-db = /usr/local/apache/htdocs/repos/username/conf/passwd
authz-db = /usr/local/apache/htdocs/repos/username/conf/authz
realm = Project
logfile = /tmp/svn.log

5) Create a new user in user database file “/usr/local/apache/htdocs/repos/username/conf/passwd”

jackal777 = pnity29#@I

6) Set authorization for user created in password file via “/usr/local/apache/htdocs/repos/username/conf/authz”

jackal777 = rw

7) Now finally create post commit hook inside the repository directory “/usr/local/apache/htdocs/repos/username/hooks/post-commit” and set execute permission for that file. Paste the following contents to that file,

svn export --force file:///usr/local/apache/htdocs/repos/username/ /home/username/public_html/
chown -R username:username /home/username/public_html/

The post commit script will export the contents of the repository to the website documentroot and assigns the proper permission to the directory.




Now checkout the repository to your local directory,

svn co svn:// --username=jackal777

Make modifications with the files and then commit to the repository,

cd username
svn commit

Now login to the server and check whether the commit made to repository are shown inside “/home/username/public_html”.

That’s it 🙂

Apache proxy redirect

June 7, 2013 1 comment

SITUATION: Customer has a single website with four different web applications installed under four sub directories of the website. Now configure apache to serve all these four applications from four different ports.


1) OS – Ubuntu 11

2) Website name and documentroot,


DocumentRoot:  /home/jackal/public_html

3) Web application sub-directories and the ports going to be used,

/home/jackal/public_html/app1 : Port 7001
/home/jackal/public_html/app2 : Port 7002
/home/jackal/public_html/app3 : Port 7003
/home/jackal/public_html/app4 : Port 7004

4) Apache mod_proxy module is installed. You can install it using,

apt-get install libapache2-mod-proxy-html -y


1) Open up /etc/apache2/ports.conf and add the following directives,

Listen 80

2) Enable mod_proxy by copying the configuratons from ‘mods-available’ directory to ‘mods-enabled’

cp -pr /etc/apache2/mods-available/*proxy* /etc/apache2/mods-enabled/

3) Create a virtualhost file “/etc/apache2/sites-enabled/” for website with the following contents,

<VirtualHost *:80>
DocumentRoot /home/jackal/public_html

ProxyPass /app1/
ProxyPass /app2/
ProxyPass /app3/
ProxyPass /app4/


DocumentRoot /home/jackal/public_html/app1

DocumentRoot /home/jackal/public_html/app2

DocumentRoot /home/jackal/public_html/app3

DocumentRoot /home/jackal/public_html/app4

4) Test configuration and gracefully restart apache.

apache2ctl -t
apache2ctl -k graceful

5) Now access the url’s,


SCOPE: Using mod_proxy, we could forward requests coming to different servers and make the applications running from several different servers.


Hope this info will be somewhat useful 🙂

Script to Monitor file creation under all cpanel users documentroot

May 11, 2013 1 comment


SITUATION: Customer wants to get the list of all newly created files under all cpanel users documentroot(/home/*/public_html).


ASSUMPTIONS: The ‘inotifywait’ command is installed. This command comes with the inotify-tools package.


SOLUTION: The following script spawns multiple ‘inotifywait’ processes into background, with each of these processes recursively monitoring and recording file creation events in each cpanel users documentroot. Newly created files under each users home directory are saved in location “/root/monitor/” with the filenames as each users name.

Save the script as “/etc/init.d/inotifywaitd” and grant execute permission to this script.





if [ $# != 1 ];then
   echo "Usage: /etc/init.d/inotifywaitd {start|stop}"
   exit 1

if [ ! -d ${DESTDIR} ];then
   mkdir ${DESTDIR}

case $1 in


      for i in `ls -d /home/*/public_html`
         user=$(echo "${i}"|cut -d\/ -f3)
         ${INOTIFY_CMD} -m -r -e create --format '%f' ${i} > ${DESTDIR}/${user}&


   stop) pkill inotifywait ;;

   *) echo "Usage: /etc/init.d/inotifywaitd {start|stop}" ;;



ERRORS: Sometime you may get the following error while running this script,

Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.

To resolve this issue, increase the filesystem inotify maximum user watches system variable as follows,

1) Get the current value of max_user_watches,

# sysctl -e fs.inotify.max_user_watches
fs.inotify.max_user_watches = 524288

2) Open up /etc/sysctl.conf and set value of “fs.inotify.max_user_watches” higher than 524288.

fs.inotify.max_user_watches = 924288

3) Reload sysctl configuration,

# sysctl -p /etc/sysctl.conf
Categories: Cpanel/WHM, Scripts

Rvsitebuilder autoinstaller using Selenium for WHM/Cpanel

May 14, 2012 Leave a comment



I was trying to setup an autoinstaller to perform the installation of rvsitebuilder in WHM. But the rvsitebuilder installer requires a java-script enabled browser for installation. So, tools like curl,lynx,twill-sh,etc. were out of scope. After little bit of googling, selenium
seemed to be the best option. Selenium requires a browser like firefox or chrome installed and working(requires X).


I have used the python selenium API to interact with the selenium server running in port 4444. You can use the selenium firefox ide for getting started with selenium API.




1) Selenium server to be up and running

java -jar selenium-server-standalone-2.21.0.jar

2) Python version >= 2.5 with support for selenium package. In order to install the package run the following command.

pip install -U selenium

3) Firefox or chrome






Before running this python code, manually ssh to the WHM server(IP: ) and download the rvsitebuilder setup files.

cd /usr/local/cpanel/whostmgr/docroot/cgi/
rm -rf /usr/local/cpanel/whostmgr/docroot/cgi/rvsitebuilderinstaller/
rm -f rvsitebuilderinstaller.tar
tar -xvf rvsitebuilderinstaller.tar
chmod 755 addon_rvsitebuilder.cgi
rm -f rvsitebuilderinstaller.tar
cp -p /var/cpanel/cpanel.config /var/cpanel/cpanel.config.bak`date +%F`
sed -i 's/maxmem=[0-9]\+/maxmem=512/'  /var/cpanel/cpanel.config
/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings



Pasted below is the python code used to install rvsitebuilder. What the code does is, it will connect to the selenium server running in localhost and open a firefox window and then connect to the WHM server via IP ‘’ and start the installation of rvsitebuilder.


# This script is executed in the machine where the selenium server runs

from selenium import selenium
import os,sys
import random,string

SELENIUM_HOST = "localhost"
BROWSER = "*firefox"

RVSIEBUILDER_USER = "rvsitebuilder"
#Generate a random string of 10 character length
RVSIEBUILDER_PASS = ''.join(random.sample(string.ascii_uppercase + string.digits,10))  

#WHM/Cpanel server details
USERNAME = "root"
PASSWORD = 'password'


	#Connect to the cpanel sever via selenium service running in SELENIUM_HOST at port SELENIUM_PORT
        #After this statement if the login credentials are valid then the rvsitebuilder download starts"id=login") 

	# The rvsitebuilder package download process will be taking 3 to 20min
	# we are waiting for this process to get completed in an infinite loop
	while True:
		if (str(sel.get_title()) == "RVSiteBuilder Installation - Download"):
			print "Downloading.."
			time.sleep(300)   #Wait for 5min	
		elif (str(sel.get_title()) == "RVSitebuilder :: Installation"):
			break #Download completed; exit out of the loop
			print "An unknown state has reached. HTML text body printed below(for troubleshooting)\n\n"
			print sel.get_body_text()


	while True:
		if (str(sel.get_title()) == "RVSitebuilder :: Installation"):
			print "Updating tables..."
			time.sleep(120)   #Wait for 2min	
		elif (str(sel.get_title()) == ":: System Configuration"):
			break #datbase updated; exit out of the loop
			print "An unknown state has reached. HTML text body printed below(for troubleshooting)\n\n"
			print sel.get_body_text()

except Exception, err:
	sys.stderr.write('ERROR: %s\n' % str(err))


Categories: Cpanel/WHM, Scripts

Redirect HTTP traffic to another IP using iptables

April 6, 2012 1 comment



SITUATION: After migrating a website to a new server, I want to redirect all traffic coming to the old server’s http port(during the TTL change period) to the webserver running in new server with a different ip .



Service 					:	Apache(port 80 and 443)
Interface name in old server(this is a vps)	:	venet0    
Destination ip					:





Use iptables nat to redirect http and https traffic to another server ip and port. Execute the following commands in the source server.


iptables -t nat -A PREROUTING -i venet0  -p tcp --dport 80 -m conntrack --ctstate NEW -j DNAT --to
iptables -t nat -A PREROUTING -i venet0  -p tcp --dport 443 -m conntrack --ctstate NEW -j DNAT --to
iptables -t nat -A PREROUTING -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE


WHMCS Autoinstaller for Cpanel/WHM

February 22, 2012 Leave a comment

A script for automated installation of WHMCS in a linux cpanel server is documented below. This has been tested in a cpanel server and the version of WHMCS used is v4.4.2 .

WHMCS requires php modules such as curl,mysql and ioncubeloader for its working. So, make sure that these modules are installed. Replace the variable section in code(such as license,username,password,etc) as per your requirement.

If you want to install WHMCS under a particular domain, replace the variables DOCUMENTROOT with your websites documentroot and also replace the IP address mentioned in variable URL with website name.





#php curl and mysql module is enabled by default so skipping it
#Enable ioncube loader 
/scripts/phpextensionmgr install Ioncubeloader
/etc/init.d/httpd restart

rm -rf
chown -R nobody:nobody whmcs/
mv whmcs/ whmcs/configuration.php
chmod 777 whmcs/configuration.php

#Installing twill(if it's not already installed) for web-browsing 
if [ ! -f /usr/bin/twill-sh ];then
	wget -O /tmp/twill-0.9.tar.gz
	cd  /tmp
	tar xzf twill-0.9.tar.gz
	cd twill-0.9
	python install
	python install_scripts
	cp -p twill-sh /usr/bin/
	chmod 755 /usr/bin/twill-sh

# Create database and accept mysql password via commandline
echo "create database whmcs;"|mysql
echo "grant all on whmcs.* to 'whmcs'@'localhost' identified by '${DBPASS}'"|mysql
echo "flush privileges;"|mysql

#AutomatedForm submitter
cat > "/tmp/" << EEOF
go ${URL}
fv 1 licensekey '${LICKEY}'
fv 1 dbhost '${DBHOST}'
fv 1 dbname '${DBNAME}'
fv 1 dbusername '${DBUSER}'
fv 1 dbpassword '${DBPASS}'
fv 1 firstname '${FIRSTNAME}'
fv 1 lastname '${LASTNAME}'
fv 1 email '${EMAIL}'
fv 1 username '${USERNAME}'
fv 1 password '${PASSWORD}'

cat /tmp/|/usr/bin/twill-sh >>/tmp/whmcsinstalllog.txt`date +%F`
if [ $? -ne 0 ];then
	#In case of error to retry drop the whmcs database and then try again
	echo "Installation failed. Check /tmp/whmcsinstalllog.txt`date +%F` for more details"
	exit 1
	echo "Installation successful"

#Add crontab
crontab -l > /tmp/cronwhmcs
grep "/whmcs/admin/cron.php" /tmp/cronwhmcs >/dev/null
if [ $? -ne 0 ];then
	echo "01 1 * * * php -q ${DOCUMENTROOT}/whmcs/admin/cron.php" >> /tmp/cronwhmcs
	crontab /tmp/cronwhmcs
rm -f /tmp/cronwhmcs

#Disable Installation folder
chmod 000 ${DOCUMENTROOT}/whmcs/install

#Remove twill-sh
rm -rf /tmp/twill-0.9.tar.gz /tmp/twill-0.9 /usr/bin/twill-fork /usr/bin/twill-sh

Hope this will be helpful:)

Fantastico Autoinstaller for Cpanel/WHM

February 22, 2012 1 comment

The installation of fantastico in WHM can now be automated using the following script. This script uses the python application named “twill-sh” to automate the installation process.

This script has been tested using cpanel version and the latest version of fantastico(ie, De Luxe 2.10.4 r50 ). Please replace the URL,USER and PASS variables to match your servers WHM url,root user and password.


#Assign root password to this variable

#Install ioncube and download fantastico files
/scripts/phpextensionmgr install IonCubeLoader
cd /usr/local/cpanel/whostmgr/docroot/cgi
wget -N
tar -xzpf fantastico_whm_admin.tgz
rm -rf fantastico_whm_admin.tgz

#Installing twill(if it's not already installed) for web-browsing 
if [ ! -f /usr/bin/twill-sh ];then
	wget -O /tmp/twill-0.9.tar.gz
	cd  /tmp
	tar xzf twill-0.9.tar.gz
	cd twill-0.9
	python install
	python install_scripts
	cp -p twill-sh /usr/bin/
	chmod 755 /usr/bin/twill-sh

#AutomatedForm submitter
cat > "/tmp/" << EEOF
go ${URL}
fv 1 user '${USER}'
fv 1 pass '${PASS}'
go scripts/command?PFILE=Plugins
follow "addon_fantastico.cgi"
go fantastico.php?Primary_Action=Install&Secondary_Action=Pre
go fantastico.php
go fantastico.php?Primary_Action=Home
go fantastico.php?Primary_Action=Scripts&Secondary_Action=Update&Tertiary_Action=Missing

echo -e "\n\nTrying to install fantastico.. check /tmp/fantasticoinstall_log.txt`date +%F` for more details"

cat /tmp/|/usr/bin/twill-sh >>/tmp/fantasticoinstall_log.txt`date +%F`
if [ $? -ne 0 ];then
	#In case of error to retry drop the whmcs database and then try again
	echo "Installation failed. Check /tmp/fantasticoinstall_log.txt`date +%F` for more details"
	exit 1
	echo "Installation successful"

#Clear unwanted files
rm -f /tmp/

#Uninstalling fantastico
# rm -rf /var/netenberg/
# rm -rf /usr/local/cpanel/whostmgr/docroot/cgi/fantastico/
# rm -rf /usr/local/cpanel/3rdparty/fantastico*
# rm -rf /usr/local/cpanel/base/frontend/*/fantastico
# rm -f /usr/local/cpanel/base/frontend/x/cells/fantastico.html
# rm -f /usr/local/cpanel/whostmgr/docroot/cgi/addon_fantastico.cgi
# crontab -l|grep -v "docroot/cgi/fantastico/scripts" > /tmp/fant.txt
# crontab /tmp/fant.txt
Categories: Cpanel/WHM, Scripts