Moving a Site

Moving a website may be done for many reasons, including:

  • Copying a development site to the staging or live server for the first time.
  • Transfering from one hosting company to another.
  • Backup.
  • Copying a live site to a development or staging site so you can add/test new features and content.

The process is basically the same for any CMS-based site:

  1. Move the files.
  2. Move the database.
  3. Update the settings in external files.
  4. Update the settings in the database.

Most of my sites are done in either Drupal or WordPress, so I'll note any differences.

Development, Staging and Live

First, a note about maintaining different versions of a website. In my experience, it is best to maintain three versions of any website that has more than one maintainer:

  1. The Development site, on the developer's personal machine.
  2. A Staging site, ideally on a different but similar server from the live site, but still accessible over the web.
  3. The Live site, on the web hosting server.

In the beginning, there is just the development site. Developing locally has many advantages including being able to edit files in your favorite text editor and immediately see the results just by reloading the browser page (no need to FTP files around) and complete isolation so nobody sees any half-baked sites. Developers are also free to experiment with new features and break things without impacting anyone else. I use my MacBook Pro as my development machine and I have MAMP installed to provide the Apache server and MySQL database.

Once development is done, the site is generally moved to a staging site for collaborative content editing. The staging site is kept in off-line mode (if the CMS supports it) or in a password protected directory. I generally create a subdomain on my JustMagicDesign.com hosting account (on a Linux server) for the staging site. Once the content is done and the site is tested and signed off, it is copied to the live server. I always use a Linux server for the live site as well.

In the diagram above, the directories are the website files root directories that I use -- your mileage will vary. Once a site goes live, the database on the live server is the "golden" version, so any subsequent work that is done is generally done on the development or staging servers after making a fresh copy from the live server

Now, on to the "recipe" for moving the website.

Copy the Files

Open a Mac Terminal window to get to the command line.

Local to Remote

cd ~/Sites/drupal/sites/site.com    <-- your site directory will surely be different
tar cvf files.tar .
gzip files.tar
ftp user@remote.com [enter pw]
cd public_html/sites
put files.tar.gz
bye
ssh user@remote.com [enter pw]
cd public_html/sites                <-- also probably different
rm -rf site.old
mv site.com site.old
mkdir site.com
cd site.com
mv ../files.tar.gz .
tar xvf files.tar.gz
rm *.gz
exit
rm *.gz

Remote to Local

cd ~/Sites/drupal/sites
ssh user@remote.com [enter pw]
cd public_html/sites/site.com
tar cvf files.tar .
gzip files.tar
exit
ftp user@remote.com [enter pw]
cd public_html/sites/site.com
get files.tar.gz
delete files.tar.gz
bye
rm -rf site.old
mv site.com site.old
mkdir site.com
cd site.com
mv ../files.tar.gz .
tar xvf files.tar.gz
rm *.gz

Copy the Database

On a Drupal site, before you export the database, visit your site as an administrator do these steps:

  • Flush all caches. You should have the Administrative menu module installed, in which case you can do this from the drop down in the leftmost menu which uses your site's favicon as the top item.
  • Go to Configuration > Search and metadata > Clean URLs and disable Clean URLs.
  • Go into Reports > Recent log messages and clear the log.

Now you need to export the MySQL database. The easiest way to do this is to use phpMyAdmin. If you're going local to remote, visit http://localhost:8888/phpMyAdmin. (You may not need the port number if you're not using MAMP.) If you're going remote to local, you may be able to get to it at http://remote.com/phpMyAdmin or http://remote.com/phpmyadmin, or you may have to go to http://remote.com/cpanel and then open phpMyAdmin in the Databases section.

Once you're logged in to phpMyAdmin with the appropriate username/password for your database, select the database for the site on the left, then choose the Export tab. Leave most things defaults, but save it to a file.

I generally save the database file to my ~/Projects/projectname/backups directory and call it database.sql.

Switch to phpMyAdmin on the destination machine. And do these steps:

  • Find the old database for this site, (if any, usually called site-old), select it and choose the Drop tab and confirm.
  • Find the current database for this site (if any), select it and choose the Operations tab. Rename it to site-old.
  • Create a new, empty database using the site name (domain name minus TLD). (This may require going back to a separate mySQL manager in the control panel on some machines. In this case, make sure you give your database user all priviledges for this database.)

Now I switch to the command line because phpMyAdmin has problems importing large databases more times than not. Open a shell on the destination machine (either by just opening a Mac Terminal window or doing ssh user@remote.com from a terminal window). Then use this command:

mysql -u username -p site < ~/Projects/projectname/backups/database.sql

If this is a WordPress site, you'll need to replace all instances of http://old.tld to http://new.tld in the database. The safest way to do this is with Search Replace DB by Interconnect/it

If you're moving a site that uses CiviCRM, you'll have to repeat the steps above for its database. (Note: also delete the file ConfigAndLog/Config.IDS.ini which is in the files/civicrm directory on a Drupal site.)

Edit the Settings File

The Drupal settings file is <drupalroot>/sites/site.tld/settings.php (or <drupalroot>/sites/default/settings.php if not using multisite). The WordPress settings file is <wp-root>/wp-config.php. You need to change the database name, user, password, and sometimes the port. (In a WP file, to change the port, make the DB_HOST be, for instance, 'localhost:8889'.) These files are well documented, so it should be obvious what to change.

Now, assuming you have your domain set up properly, you should be able to visit your site with your browser to set some configuration parameters that get stored in the database.

Edit Some Database Settings

On a Drupal site, you'll need to go back to Configuration > Search and metadata > Clean URLs and re-enable Clean URLs. You should also go to Configuration > Media > File system and check the file directories to make sure they're correct for the server. 

About Maria Greene

Maria Greene's picture
Maria wears the propellor hat at Just Magic Design. She has a degree in Computer Science from Brown University and has been making websites since 2002. Besides web development, Maria tries to keep up with her three school-age kids and serves on the boards of several non-profits. Her husband adds joy to her life and her dog adds slobber.

Add your comment