// Tutorial //

How To Migrate WordPress from Shared Hosting to a Cloud Server with Zero Downtime

Published on June 25, 2013
Default avatar
By Ramesh Jha
Developer and author at DigitalOcean.
How To Migrate WordPress from Shared Hosting to a Cloud Server with Zero Downtime

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

Moving to a Cloud Server

Many bloggers start with shared hosting. This may be good for starting out, but when traffic grows (e.g. above a few hundred visitors per day), you should consider moving the blog to a personal cloud server. Moving to one isn't very complicated, just follow the steps correctly and you can migrate a WordPress blog/website within a few hours, without any downtime.

Step 1. Create a Backup

You need to backup all your files, along with the MySQL database (from your current shared hosting server). Most of the shared hosting providers have a simple GUI for managing servers, such as cPanel.

Backup Files (images, themes, plugins)

You don't need to backup core WordPress files, but you must backup images (you may have already uploaded for a post), themes, and plugins. In order to do this, first, create an archive of the wp-contents directory (right-click => compress as zip), then download it.

compress cpanel dir

Back Up the Database

In cPanel, go to "Backup Wizard => MySQL Databases".

wizard

You'll be able to download the MySQL database in *.sql.gz format. Save it as backup_db.sql.gz on your desktop. backup mysql db

Step 2. Set Up the Cloud Server with LAMP Stack

Launch a droplet (cloud server) with Ubuntu 12.04 and follow this guide for the basic setup process: Ubuntu Server Setup. Now that the cloud server is set up, you need to install the WordPress dependencies such as MySQL, PHP and a web server like Apache. Follow this guide to set up a LAMP stack.

Step 3. Install WordPress

Once the LAMP stack is set up, install the latest version of WordPress. To do so, simply follow this guide: Installing Wordpress on Ubuntu.

Step 4. Create a Virtual Host

Create an Apache virtual host for handling your Wordpress blog. Create a new file in the site-available directory:

sudo nano /etc/apache2/sites-available/yourdomain.com

Add a virtual host (replace yourdomain.com and username accordingly) for the blog. Each VirtualHost block defines a seperate cloud server and the number 80 indicates the port that Apache will listen on. ServerName represents your domain name and DocumentRoot should point to the root of the WordPress directory.

<VirtualHost *:80>
     ServerName yourdomain.com
     DocumentRoot /var/www/
</VirtualHost>
<VirtualHost *:80>
     ServerName www.yourdomain.com
     Redirect permanent / http://yourdomain.com/
</VirtualHost>

Then enable this virtual host using the Apache utility a2ensite. It takes the above configuration and tells Apache to listen for yourdomain.com.

sudo a2ensite yourdomain.com

Now reload the Apache server. Whenever you make any changes in server configuration, you must reload the server to apply those changes.

sudo service apache2 reload

Step 5. Restore Database and Files

The WordPress installation is now set up but you haven't imported your old articles, images, themes, etc. Let's upload the files first.

Upload the Backup Files - MySQL Database and File Contents to VPS

scp is very handy for uploading files. Like FTP, you can transfer files, but SCP does it securely over SSH. For uploading files, you need to pass two arguments to the command. The first one is the location of the file you want to upload and the second one is the target server (in the form of username@server_ip_address). To upload the backup files, simply execute these commands locally (on your computer) and it will upload the files to home directory of the server.

scp ~/Downloads/backup_db.sql.gz username@server_ip_address:
scp ~/Downloads/wp-content.zip username@server_ip_address:

Restore the Database

To restore the database, login to the server and type (replace database_name, database_user accordingly) the command below:

mysql -h localhost -u database_user -p database_name < backup_db.sql.gz 

The command requires a few arguments: the -h option for specifying the host address (in this case it's localhost, because the database is running on the same server), the second argument, -u, provides the database username, the third option, -p, means the password will be supplied on prompt, the fourth option specifies the name of the WordPress database, and the last argument is the input - the backup database.

You will be asked to enter the password for the database user. Within a few seconds, the database contents will be imported to the specified database.

Restore wp-content Files

To restore themes, uploaded media (images, videos, etc.), and plugins, simply extract (using the unzip command) the zip archive. It will extract and merge the contents into the existing wp-contents directory.

sudo unzip wp-content.zip -d /var/www/

Step 6. Test your Blog

To achieve zero downtime, this step is important. You have to make sure that the blog is set up properly on the new location. To do this, first update your hosts file.

sudo nano /etc/hosts

Add this line to the hosts file (now when you visit yourdomain.com, it will point to your new server, but only on your computer - this makes testing easier).

server_ip_address  yourdomain.com 

Next, clear your DNS cache (since you've updated the hosts file, you must clear the DNS cache to apply the changes. nscd is nice little tool for flushing out the DNS cache results)

sudo service nscd restart

Now, if you visit yourdomain.com, it will be loading pages from your new server. If it looks as expected (all your articles, images, pages, plugins, custom designs etc.), then it means you are all set and you should move on to the next step. Otherwise, try to figure out what went wrong. (After testing successfully, remove the above line from the hosts file).

Step 7. Update Your DNS Settings

Now you should update the DNS settings with your Domain Registrar. For the A record, update the IP address to the new value (IP address of your VPS) or you could also move your DNS to your VPS provider (and add A records there). For DigitalOcean, you need to put these name servers:

ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com

Note: Your DNS server will start propagating the new values but it will take some time, so do not terminate your old shared hosting immediately after the transition (preferably after a day).

Article Submitted by: Ramesh Jha

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
10 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

On Ubuntu 13.10 by default you have to create the .conf file like so: sudo nano /etc/apache2/sites-available/yourdomain.com.conf instead of sudo nano /etc/apache2/sites-available/yourdomain.com. Otherwise a2ensite won’t add your VirtualHost (it will say “ERROR: site **** does not exist”).

This error may occur because of the fact that the files in sites-enabled are included in the configuration by this directive in /etc/apache2/apache2.conf:

Include the virtual host configurations:

IncludeOptional sites-enabled/*.conf

It only reads files matching *.conf.

Good Luck with your websites!

I made a video of migrating my blog from a shared host to digital ocean. The tutorial above is out of date by multiple Linux Ubuntu versions and led me wrong several times. But I solved it ultimately and chronicled all the steps here https://www.youtube.com/watch?v=UrY1P041uUU

Ok, so for everyone doing this on Ubuntu 14.04 (because you do want to do this with the latest version and not have to upgrade in 2017)

A) You will follow mostly the same steps in this tutorial except for using the 14.04 versions of the various tutorials it links to:

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04 https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04 https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-14-04

B) Very important: Any step (4 & 5 namely) in this tutorial that refers to /var/www/ needs to refer to /var/www/html/ instead, since that’s the home directory for Ubuntu 14.04

C) Step 4 of this tutorial remains the same, but you must configure your file as :

sudo nano /etc/apache2/sites-available/yourdomain.com.conf

and not

sudo nano /etc/apache2/sites-available/yourdomain.com

The .conf extension is required in 14.04. For reasons I don’t understand and as a cookbook layperson may never understand ;)

D) You may not be able to directly restore the database from a .sql.gz file in Step 5. You might first have to extract it using gunzip and then restore it as a .sql file. That’s what worked for me.

Hi, I’m using Ubuntu 20.04. I’m stuck at step 5 while trying to restore db.

I have kept my sql.gz file at Downloads (on Mac) and then trying the scp command - replaced username and server IP address with mine: sudo scp ~/Downloads/db-backup.sql <<my username>>@<<my IP address>>:

running this command gives below error: <<my username>>@<<my IP address>>: Permission denied (publickey). lost connection

Can anyone please help me in understanding what’s happening here? I tried google but could not get an answer.

Thank you in advance.

Thanks for writing this awesome article. It really helped me change my mind. Earlier, I was given the suggestion of going for the managed digitalOcean server which is normally powered b y third party host. But, after going through this article I am able to to host it on the conventional digitalOcean dedicated server. Which is very impressive as the article was written back in 2013 and is still that much effective.

do we have an update for this?

Is the same process on Ubuntu 16.04? And is it possible to transfer the Moodle site in that way?

I have installed LEMP Wordpress and uploaded wp content expanded it, uploaded MySQL and now when I am opening then it’s not working error page coming.

Sir I have installed LEMP STACK what are the commands to migrate in LEMP

Step 4. Create a Virtual Host

There’s a problem, when I followed the tutorial and did this:

sudo nano /etc/apache2/sites-available/yourdomain.com

a2ensite wouldn’t recognize the configuration file afterwards, and I only was able to solve the problem by adding a .conf after the end of the created file, like this:

sudo nano /etc/apache2/sites-available/yourdomain.com.conf