A 15 minute guide to migrating your WordPress blog to a new host
I’ve recently moved my blog to a host at RamNode. And obviously, I had to migrate my blog as seamlessly as possible to my readers. Now that the job is done, I’m posting them here to serve as a reminder to myself and to anyone else that also needs to migrate you self-hosted blog to another host.
There are basically 3 parts to the migration task. Firstly, preparing the new host, then backing up and migrating all the files over, and finally switching the IP address on the DNS server.
So here the 15 minutes (or less) guide on how I got the job done.
Important notes and assumptions
The target host I’m migrating to runs Ubuntu 12.04 LTS. If you are planning to use a different OS, do note that you’re required steps may be different.
I’m also assuming that you have full root access to the server via SSH and is already logged in as root. I’m also assuming you know how to go about using vi editor and the basics of using bash shell and lastly, am familiar with phpmyadmin.
Part 1. Preparing the new host
1. Set the Locale
By default, the locale of the server is not set, assuming that the installed Ubuntu 12.04 LTS is plain vanilla without any configurations or customisations. Run the following comments to set the locale. Again, assuming you’re logged in as root on the SSH terminal, there’s no need to use the sudo command.
sudo vi ~/.bashrc
Then add the following line to .bashrc
Save the changes.
Then log-out and log back into the system.
Run the following commands to generate and re-configure the locales.
sudo locale-gen en_US.UTF-8 sudo dpkg-reconfigure locales
Then run the locale command to make sure you don’t get any errors.
You should see the following output if everything is fine and dandy.
LANG= LANGUAGE= LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=en_US.UTF-8
2. Change the timezone (optional)
The server would general have it’s timezone set to UTC+0 by default. To change it just use the following command and follow through the instructions.
sudo dpkg-reconfigure tzdata
3. Update apt-get database
sudo apt-get update
4. Install LAMP package
Now that apt-get is updated. It’s time to start installing the required packages to your server. The first one is obviously the LAMP stack. This would install Apache 2, MySQL and the basic PHP.
Do note that during this installation process, you would be asked for the MySQL root password. Make sure you jot down what you entered. I suggest using a strong password generator to create a very strong password (min. 25 characters, mix-cased, including numbers and symbols), store it safely and securely and copy and paste over that password for the MySQL setup. For generating and storing passwords, 1Password is a great tool to use.
sudo apt-get install lamp-server^
5. Install PHP5
Next is to make sure more php5 modules are installed as most PHP apps requires them these days. I’ve also included php5-mcrypt in my install since the new phpmyadmin requires it.
sudo apt-get install php5 php5-gd php5-mysql php5-curl php5-cli php5-cgi php5-dev php5-mcrypt
6. Install imagemagick
One of the WordPress plugin I use needs this. So it’s also on my checklist.
sudo apt-get install imagemagick
7. Install APC (Alternative PHP Cache)
WordPress has an awesome support for caching, especially when you couple it with the W3TC plugin (http://wordpress.org/extend/plugins/w3-total-cache/) which I highly recommend.
sudo apt-get install php-apc
Once APC is installed, I recommend increasing the shared memory size from the default of 32M to 64M, or even 128M if you have the RAM to spare. The apc.ini configuration file is location at /etc/php5/apache2/conf.d/apc.ini
sudo vi /etc/php5/apache2/conf.d/apc.ini
Add the following line to the apc.ini highlighted in bold and red below.
8. Increase PHP’s maximum upload file size (optional)
sudo vi /etc/php5/apache2/php.ini
Look for the upload_max_filesize config and chage it accordingly.
upload_max_filesize = 8M
If you want to increase that to anything above 8M, then you will also need to edit the post_max_size settings accordingly.
post_max_size = 8M
Then reload apache for the new settings to take effect.
sudo service apache2 reload
8. Disable Apache directory indexing
You’d probably want to harden Apache a lot more than just disabling directory indexing, but this is a quick step that you’d want to do immediately to prevent anyone from browsing your web directory.
sudo vi /etc/apache2/sites-available/default
Delete all “indexes” keyword from the configuration. Then save the edit.
9. Install phpmyadmin to manage MySQL
The easy way is to just run the following command and you would immediately get phpmyadmin up and running.
sudo apt-get install phpmyadmin
Choose to automatically configure apache2 since that’s what we are using as the web server. Also when asked, say yes to creating a database for phpmyadmin using the dbconfig-common script. You’ll need the MySQL root password and you would also be asked for a password for the phpmyadmin database user. As usual, I’d recommend generating a strong password, just as you’ve done so for the root password.
Once done, you should be able to access phpmyadmin from http://domainname.com/phpmyadmin,
If you cannot reach to the server, check to see if the apache configuration exists in at /etc/apache2/conf.d/phpmyadmin.conf. If not, you can manually create the symlink as follows.
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
While optional, I’d usually want to have phpmyadmin accessible via a subdomain like phpmyadmin.domainname.com. To do so, you’d need to add an A entry with the phpmyadmin as the name and the new host’s IP as the value. It would take a while for the DNS setting to propagate over the Internet so, it might be a while before you can use this.
Next is to edit the default phpmyadmin apache configuration so that the virtualhost listens to requests for this sub-domain.
sudo vi /etc/phpmyadmin/apache.conf
Edit the top section of the file with the following, essentially removing the phpmyadmin alias and replacing it with ServerName and DocumentRoot configs (in bold and red), basically reconfiguring it as a VirtualHost config.
# phpMyAdmin default Apache configuration <VirtualHost *:80> ServerName phpmyadmin.domainname.com DocumentRoot /usr/share/phpmyadmin ... ...
As the bottom most of the config file, add the following line to close off the VirtualHost settings.
With that, you should be able to access phpmyadmin with your sub-domain now!
That’s pretty much it! You’ve got you server all ready and set up.
Note: If this is a new WordPress install from scratch with no intention of migrating over an existing site, the simply install the latest version of WordPress (http://codex.wordpress.org/Installing_WordPress) and you’ll be all set!
Part 2. Backing up and migrating your existing site
This part is somewhat easy. Log in to your existing host (obviously)
1. Migrating your WordPress directory
Firstly, log in to your existing host and archive your existing site using tar or zip.
cd /var/www sudo tar cvzf myblog.tgz myblog
Replace “myblog” above being with the actual directory of your existing WordPress insallation
Next, you’ll then move it to your new host. What I do, and recommend, is to just move the tarball into your existing site’s directory so that you can easily use the wget command to download the file directly to your new server host.
Log in to your new host and download the tarball to your new host
cd /var/www sudo wget http://www.domainname.com/myblog.tgz
Once completed, just extract the tarball on your new server host.
sudo tar xzvf myblog.tgz
After the you untar your blog, just to a quick check that everything is ok. You should see the same directory structure as your existing host. Next, you’ll need to set the wordpress directory so that the auto-update can work.
sudo chown -R www-data /var/www/myblog
2. Configuring Apache for WordPress
Next, let’s create a new Apache configuration for WordPress. To do so, you’ll need to create a copy of the default site configuration and simply edit the copy to listen to your domain name and redirect those requests to the files served from your wordpress directory.
cd /etc/apache2/sites-available sudo cp default myblog sudo vi myblog
The edit the myblog configuration as follows, replacing the text where necessary to fit your needs.
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName www.domainname.com DocumentRoot /var/www/myblog <Directory /> Options FollowSymLinks ... ...
Next, simply enable the new site configuration and reload the apache service
sudo a2ensite myblog sudo service apache2 reload
3. Migrating your WordPress database
Now, the migration is not done yet since the database has not yet been migrated. And the reason why I delayed this till the last step is so that any late changes to the database (comments, etc) would be preserved as much as possible.
To do so, simply export your existing WordPress database and download it to your local machine. If you are also using phpmyadmin on the existing host, then it should be a pretty straight forward process. The default export settings are sufficient, although you may want to export it compressed in gzipped format.
Once you have the database exported and downloaded. Proceed to login to phpmyadmin on the new host. Login as root.
Then create a blank database of the same name to keep things simple on the migration of the database. Then create a new user using the same database user and password you used on the existing host. If you do plan to change, you will then need to edit the wp-config.php file which contains the database connection user and password. Then, edit the privileges of the user you’ve just created and give all privileges for the WordPress database to that user.
Then all you need to do is just import It back into the WordPress database that you’ve just created.
With that, you’re pretty much set to get things going!
Part 3. Switching the DNS
Finally, the moment of truth. Go ahead and point your domain to the new server’s IP address. And then wait. This would take anywhere from 30 mins to a few hours for the dns change to propagate across the globe, depending where you are in the world.
What I do to know if the change has already occurred or not is to quickly post a quick entry on the existing server, just a short message on the change of server host and the possibility of errors during the switch over. You could do this just before switching over to the new IP address. So when the switch over finally happens, you will no longer see that post.
And that’s it. And remember to install the W3 Total Cache (W3TC) plugin for WordPress to take full advantage of the APC module that you have installed on your new server if you’ve not already used it before. It really does help improve the performance and load time of your site.