Archive

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

 

ASSUMPTIONS:
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

 

SOLUTION:

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,

[general]
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”

[users]
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,

#!/bin/bash
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.

 

TESTING:

 

Now checkout the repository to your local directory,

svn co svn://37.71.13.54/username --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 🙂

Advertisements

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.

ASSUMPTIONS:

1) OS – Ubuntu 11

2) Website name and documentroot,

Name: jackal777.com

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

SOLUTION:

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

Listen 80
Listen 127.0.0.1:7001
Listen 127.0.0.1:7002
Listen 127.0.0.1:7003
Listen 127.0.0.1:7004

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/jackal777.com” for website with the following contents,

<VirtualHost *:80>
ServerName jackal777.com
DocumentRoot /home/jackal/public_html

ProxyPass /app1/ http://127.0.0.1:7001/
ProxyPass /app2/ http://127.0.0.1:7002/
ProxyPass /app3/ http://127.0.0.1:7003/
ProxyPass /app4/ http://127.0.0.1:7004/

</VirtualHost>

<VirtualHost 127.0.0.1:7001>
DocumentRoot /home/jackal/public_html/app1
</VirtualHost>

<VirtualHost 127.0.0.1:7002>
DocumentRoot /home/jackal/public_html/app2
</VirtualHost>

<VirtualHost 127.0.0.1:7003>
DocumentRoot /home/jackal/public_html/app3
</VirtualHost>

<VirtualHost 127.0.0.1:7004>
DocumentRoot /home/jackal/public_html/app4
</VirtualHost>

4) Test configuration and gracefully restart apache.

apache2ctl -t
apache2ctl -k graceful

5) Now access the url’s,

http://jackal777.com/app1/
http://jackal777.com/app2/
http://jackal777.com/app3/
http://jackal777.com/app4/

 

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.

 

/etc/init.d/inotifywaitd

#!/bin/bash

DESTDIR="/root/monitor"
INOTIFY_CMD="/usr/bin/inotifywait"

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

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


case $1 in

   start)

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

   ;;

   stop) pkill inotifywait ;;

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

esac

 

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.

 

 

Requirements

1) Selenium server to be up and running

wget http://selenium.googlecode.com/files/selenium-server-standalone-2.21.0.jar
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

 

Ref:

http://seleniumhq.org/
http://seleniumhq.org/projects/ide/

 

 

 

Before running this python code, manually ssh to the WHM server(IP: 26.24.82.32 ) 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
wget http://download.rvglobalsoft.com/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 ‘26.24.82.32’ and start the installation of rvsitebuilder.

 

#!/usr/bin/python
# This script is executed in the machine where the selenium server runs

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

#Initialization
SELENIUM_HOST = "localhost"
SELENIUM_PORT = 4444
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"
IPADDRESS = '26.24.82.32'
PASSWORD = 'password'


try:

	#Connect to the cpanel sever via selenium service running in SELENIUM_HOST at port SELENIUM_PORT
	sel= selenium(SELENIUM_HOST,SELENIUM_PORT,BROWSER,"http://"+IPADDRESS+":2086")
	sel.start()
	sel.open("/cgi/rvsitebuilderinstaller/index.php?action=Download")
	sel.type("id=user",USERNAME)
	sel.type("id=pass",PASSWORD)
        #After this statement if the login credentials are valid then the rvsitebuilder download starts
	sel.click("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
		else:
			print "An unknown state has reached. HTML text body printed below(for troubleshooting)\n\n"
			print sel.get_body_text()
			sel.stop()
	 		sys.exit(1)

#	sel.open("/cgi/rvsitebuilder/setup.php")
	sel.type("id=databaseUser-0",RVSIEBUILDER_USER)
	sel.type("id=databaseUserPass-0",RVSIEBUILDER_PASS)
	sel.click("id=_qf_rvsSetupDB_next-0")
			

	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
		else:
			print "An unknown state has reached. HTML text body printed below(for troubleshooting)\n\n"
			print sel.get_body_text()
			sel.stop()
	 		sys.exit(1)

	sel.open("/logout/")
	sel.stop()

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 .

 

DETAILS:

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

 

 

SOLUTION:

 

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 98.59.254.163:80
iptables -t nat -A PREROUTING -i venet0  -p tcp --dport 443 -m conntrack --ctstate NEW -j DNAT --to 98.59.254.163:443
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.

#!/bin/bash

#Variables
DOWNLOADURL='http://webservices.domain.com/whmcs_v442.zip'
DOCUMENTROOT='/usr/local/apache/htdocs'

URL='http://162.24.12.23/whmcs/install/install.php?step=3'
LICKEY='Leased-adv14eu2345678901323'
DBHOST='localhost'
DBNAME='whmcs'
DBUSER='whmcs'
DBPASS='whmcs'

FIRSTNAME='MyName'
LASTNAME='Lastname'
EMAIL='myemail@domain.com'
USERNAME='whmcs'
PASSWORD='whmcsadmin'

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


wget -O ${DOCUMENTROOT}/whmcs_v442.zip ${DOWNLOADURL}
cd ${DOCUMENTROOT}
unzip whmcs_v442.zip
rm -rf whmcs_v442.zip
chown -R nobody:nobody whmcs/
mv whmcs/configuration.php.new 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 http://darcs.idyll.org/~t/projects/twill-0.9.tar.gz
	cd  /tmp
	tar xzf twill-0.9.tar.gz
	cd twill-0.9
	python setup.py install
	python setup.py install_scripts
	cp -p twill-sh /usr/bin/
	chmod 755 /usr/bin/twill-sh
fi


# 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/whmcs_twill.sh" << EEOF
go ${URL}
fv 1 licensekey '${LICKEY}'
fv 1 dbhost '${DBHOST}'
fv 1 dbname '${DBNAME}'
fv 1 dbusername '${DBUSER}'
fv 1 dbpassword '${DBPASS}'
submit
fv 1 firstname '${FIRSTNAME}'
fv 1 lastname '${LASTNAME}'
fv 1 email '${EMAIL}'
fv 1 username '${USERNAME}'
fv 1 password '${PASSWORD}'
submit
exit
EEOF

cat /tmp/whmcs_twill.sh|/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
else
	echo "Installation successful"
fi


#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
fi
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 11.30.6.3 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.

#!/bin/bash

#Assign root password to this variable
URL='http://162.24.12.23:2086'
USER='root'
PASS='myrootpass'

#Install ioncube and download fantastico files
/scripts/phpextensionmgr install IonCubeLoader
cd /usr/local/cpanel/whostmgr/docroot/cgi
wget -N http://files.betaservant.com/files/free/fantastico_whm_admin.tgz
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 http://darcs.idyll.org/~t/projects/twill-0.9.tar.gz
	cd  /tmp
	tar xzf twill-0.9.tar.gz
	cd twill-0.9
	python setup.py install
	python setup.py install_scripts
	cp -p twill-sh /usr/bin/
	chmod 755 /usr/bin/twill-sh
fi


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

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

cat /tmp/fantastico_twill.sh|/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
else
	echo "Installation successful"
fi

#Clear unwanted files
rm -f /tmp/fantastico_twill.sh


########################################
#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