cPanel Logo Reflection

How to clone a website in cPanel

Would you like to clone any website in cPanel? There is a pretty fool-proof way to clone a website and change the references to URLs, paths and database details. There isn’t one perfect catch-all method, but you can cover most bases using this short guide.

In this guide we’ll use web1 and web2 as the usernames and web1.yourdomain.com and web2.yourdomain.com as their respective URLs. Note that if the source website is live, it will be offline for the time it takes to restore the data from its backup.

Create a backup of the source website

Run this command in shell:

/scripts/pkgacct web1

This will place a file named cpmove-web1.tar.gz or similar into the /home directory.

Modify the username and URL

In WHM go to Modify Account, select the source username: web1 and edit the main details to use the new URL and usernames: web2 and web2.yourdomain.com.

This is your cloned website. It is now live at the new URL, but if there is a database or a configuration, this will need to be updated to reflect its new location.

Restore the source

Get the source website back up and running by restoring the backup you’ve just created.

/scripts/restorepkg web1

Update the configuration files

For modern websites, you’ll need to update its configuration to use the newly cloned database and perhaps the paths and URLs. You can search and replace text within files using this command.

sed -i 's/web1/web2/g' /home/web2/public_html/app/etc/local.xml

This will update the Magento configuration file. If you have WordPress, this command will update its configuration:

sed -i 's/web1/web2/g' /home/web2/public_html/wp-config.php

Change the location of the file as needed. Searching for the old database name will be a good place to start if you’re unsure about which file to change:

grep -r 'web1_dbname' /home/web2/public_html/

This will give a list of files containing the previous database name. Change it to the source path or source URL for more files. It is best to ignore any log file results, press Ctrl C if you’re getting deluged by results.

Update the database

I use a really cool database search and replace script that I found to change the URL of WordPress websites. It has a CLI version that will connect to any database and change its values, including serialised data.

php ./srdb.cli.php -n web2_dbname -u web2_user -p "pass#123_xyz" -h localhost -s "web1.yourdomain.com" -r "web2.yourdomain.com"

Change the details here to reflect your setup.

Remove temporary files and empty the cache

If you’re unsure about this part, probably backup these files before deleting them.

rm -Rf /home/web2/public_html/{tmp,cache}/*

This will clear temporary Magento files. If you have WordPress and use a cache, this will clear the cache files:

rm -Rf /home/web2/public_html/wp-content/cache/*

If you’re cloning multiple times, you can string a list of commands to perform the clone with minimal human input.

If you spot anything that I’ve missed, please drop me a comment here.

Leave a Reply

Your email address will not be published. Required fields are marked *