SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

April 19, 2010 – 10:35pm Moving wordpress site to another server

There was a question posted on WP Questions about moving a wordpress from a local development server to the live server. Here is what I do for just about all of my WP sites. It works for me since while I’m building out a site I usually make local changes that I have to sync up to a staging site for people to review.

1. Edit your wp-config.php so it works on various environments. I do this like so:

// ** MySQL settings - You can get this info from your web host ** //
switch ($_SERVER['SERVER_NAME']) {
  case 'mysite.local': // local dev environment
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'mylocal_db');
    /** MySQL database username */
    define('DB_USER', 'root');
    /** MySQL database password */
    define('DB_PASSWORD', '');
    /** MySQL hostname */
    define('DB_HOST', 'localhost');
    break;
  default:
    /** The name of the database for WordPress */
    define('DB_NAME', 'yourlive_db');
    /** MySQL database username */
    define('DB_USER', 'yourlive_dbuser');
    /** MySQL database password */
    define('DB_PASSWORD', 'yourlive_pw');
    /** MySQL hostname */
    define('DB_HOST', 'localhost');
    break;
}

That will let you use the same wp-config.php file on either server.

2. I dump my local database. I do this using a little shell script that will search the mysql dump for my local url and replace it with the live one. I put the following in a file called dbdump.sh:

#!/bin/bash
 
# dump database
mysqldump -u root mylocal_db > mylocal_db_dump.sql
 
# search inside the mylocal_db_dump.sql file for 'mysite.local' and replace with 'thelivesite.com'
perl -pi -e 's/mysite.local/thelivesite.com/g' mylocal_db_dump.sql

Then I can run this script with the command “./dbdump.sh” anytime I want to export my local database to an sql file to import on the live site.

3. I use rsync to sync my local version of the site up to the server. Alternatively, you could use FTP to upload the entire local site, but it’s harder to keep things in sync this way. If you want to use SSH, I put the following into a file named something like “upload.sh” and put it in my blog folder.

#!/bin/bash
GO=$1
: ${GO:=""}
 
if [ "$GO" = "go" ]
then
  ARGS="-avc"
else
  ARGS="-avcn"
fi
 
rsync $ARGS --stats --progress --exclude-from=rsync_exclude.txt -e 'ssh' . username@yourserver.com:/home/path/to/your/public_html

Basically what this does is execute the “rsync” command with a few arguments. It will look at the files in the current directory on your machine and compare them with the files on the server. If you execute this script with a “go” parameter, it will actually perform the copy; otherwise it does a dry-run to show you what changes would occur. I put a list of a few files to ignore while syncing in “rsync_exclude.txt”. I ignore any hidden files like .DS_Store, my custom upload shell script which we just created, and temporary files and uploads.

Here is what mine usually contains:

.DS_Store
.htaccess
upload.sh
dbdump.sh
rsync_exclude.txt
/wp-content/cache/*
/wp-content/uploads/*

You can then run the upload.sh script by executing the command like this:

./upload.sh

And if it looks ok, you actually perform the sync with:

./upload.sh go

4. Now we have all our files synced up… we just need to update the database. You can either either use something like phpMyAdmin to upload your “mylocal_db_dump.sql” file, or if you have SSH access you can execute it using your live mysql user:

mysql -u yourlive_dbuser -p mylocal_db_dump.sql > yourlive_db

This may seem like a lot of steps, but if you are rapidly developing your site and need to re-upload to a staging server frequently, this ends up saving a ton of time. Once you have the files in place, updating the live site is a simple matter of running these commands:

./dbdump.sh
./upload.sh

and then insert your SQL file into your live database, either through the command line or phpMyAdmin.

Now that I have these files I just copy them into any wordpress site I’m working on and I’m ready to upload as often as I want and it only takes a few seconds. I hope you find some use out of them, too.

If you want to download a bundle of the sample files I mention above, you can download a zip archive of them here.

3 Responses to Moving wordpress site to another server

  1. Xav. says:

    I’ve been facing this problem too but didn’t have time to find out a solution. The way you’re doing seems pretty flexible. Thanks !

  2. Thank you so much! I was doing this manually for the longest time and it was such a pain.

    The only sticking point for me was step 2. My local server uses “IP.address/localsite/”, so I had to differentiate between the URLs and directory structures in the perl substitution statement:

    “perl -pi -e ‘s/http\:\/\/mysite.local/http\:\/\/thelivesite.com/g’ mylocal_db_dump.sql”

    “perl -pi -e ‘s/var\/www\/mysite.local/public_html\/thelivesite.com/g’ mylocal_db_dump.sql”

  3. Pingback: Setting up and Configuring a Local Wordpress Installation with Mod_Rewrite | David Merrick