Moving a wordpress blog between OSX and Ubuntu

I recently had to move a wordpress blog from one server (on OSX running under  MAMP) to another on Ubuntu.

I thought I’d write up my experiences primarily as a record for myself, but also in case it helps someone else, as I had to solve a couple of issues that weren’t mentioned in the other information sources I found. I also had to use command-line tools as I didn’t have a graphical login on the ubuntu server.

The first step is to dump the mysql database that holds the wordpress data, which is done with the command:

mysqldump  --user=root --password  mydatabase > mydatabase.sql

(where mydatabase is the name of the wordpress database). If in doubt as to the name, log in to mysql  ( “mysql -u root -p” ) and type the command: “show databases;”, which will print a name of the databases, and you should see one that corresponds to your wordpress installation.

Then, tar up the entire wordpress directory containing the wordpress code, your themes and pictures/files. With MAMP, the files live in:  /Applications/MAMP/htdocs/, so cd there and (assuming your wordpress blog is in a folder called wordpress) then run:

tar -cjf wordpress

To prepare the ubuntu server for wordpress, you need to install a LAMP stack on the server (instructions here).

Then use ftp/scp/carrier pigeon to copy the tarred up wordpress directory and database dump to the Ubuntu server and unpack the wordpress directory where you’d like your website served from.

NB: unless you want to wrestle with all sorts of permissions nightmares, put your wordpress folder in /var/www, or at the very least a folder that is writeable by the apache user (www-data by default) and accessible from the root directory, or you will hit all sorts of errors like: “You don’t have permission to access /XXX/ on this server.” I’ve hit problems in the past where I placed the wordpress file in my home directory, and gave www-data read/write access on the wordpress folder. This won’t work if www-data can’t read your home directory – if in doubt su to www-data and make sure you can read the directory.

Within the wordpress directory the wp-config.php file contains the configuration information for your installation. Look in this file for the DB_USER and DB_PASSWORD fields. You then need to add this user to your mysql database with the commands:

# Log into to mysql as root
mysql -u root -p

# db_user and db_password are the values found above - in single quotes
CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'db_password';

# mydatabase is the name of the database from above
GRANT ALL PRIVILEGES ON mydatabase.* TO 'db_user'@'localhost' IDENTIFIED BY 'db_password';


This sets up mysql. In my case I’d used permalinks and a different port number on my initial wordpress installation and was changing these on the new server. I therefore needed to change all references from e.g. to I therefore used sed to make these changes to the sql dumpfile with the command:

sed 's@:8888/oldirectory/@:8444/newdirectory/@g' < mydatabase.sql > mydatabase_rename.sql

The final stage is to load the mysql dumpfile into mysql with the command:

mysql -u root -p mydatabase < mydatabase_rename.sql

(note – mydatabase is the name of the database from above and must be matching in mydatabase_rename.sql ).

With mysql set up, we need to set up apache to serve the installation. Apache stores its configuration files in /etc/apache2, and the virtual sites available on your server are symlinks from the directory /etc/apache2/sites-enabled to /etc/apache2/sites-available. Therefore, create a file called /etc/apache2/sites-available/mysite and symlink it to /etc/apache2/sites-enabled/000-mysite. The file should contain something like:

<VirtualHost *:8111>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

The port number (8111) in this case should match the NameVirtualHost and Listen values in the /etc/apache2/ports.conf

If you use peramalinks in your wordpress installation, you will need to include the “AllowOverride All” directive and also enable the apache mod_rewrite module. This is done by creating a symlink from /etc/apache2/mods-enabled/rewrite.load to  /etc/apache2/mods-available/rewrite.load

If you miss the last bit out, you will be able to access the wordpress homepage, but for all other pages you will get the dreaded “You don’t have permission to access /XXX/ on this server.” error.

With all that set up, it just remains to restart apache with: sudo service apache2 restart

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s