Tutorial

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

Published on June 26, 2013
author

By Ramesh Jha

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
40 Comments
Leave a comment...

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!

<VirtualHost *:80> ServerName example.com … Redirect permanent / http://yourdomain.com/ </VirtualHost>

Maybe example.com must be yourdomain.com ?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 30, 2013

@seobag Replace yourdomain.com and example.com with your domain name.

Yes, I understood it, but yourdomain.com and example.com in this tutorial looks like it was two different names and I think that must be correct to change example.com on yourdomain.com in this example.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 1, 2013

@seobag: Updated the article - thanks!

How do I install multiple wordpress sites on a droplet?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 4, 2013

It means it is possible to upload 4 wordpress websites in single droplet?

Yes. As many as you can fit, given the memory of the droplet.

Hi, I’m using mac. How to upload the database file from my computer to the server? How to locate the file source on my mac? I put the file on the desktop. I’ve also change the port number

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 15, 2013

@marbolec Take a look at the “Upload the Backup Files - MySQL Database and File Contents to VPS” section.

Replace ~/Downloads with ~/User and append “-P 1234” to the command (where 1234 is your new ssh port).

wow well amazing site digital ocean XD

i want to start my first account here for learn more about linux servers

excellent price for 5 dollars i can test my skills in linux servers and learn XD

Sorry Kamal, I meant how to I set up multiple single installs using virtual hosts. The installing wordpress tutorial installs wordpress at the root, what if you are running multiple domains with multiple single wordpress installs?

@postcreative You need to repeat Step 4. in this tutorial for as many domains as you want.

While doing this, make “DocumentRoot” point to a right sub-directory under /var/www For example, if you have 2 domains say abc.com, def.com

  1. Have 2 directories under /var/www => abc.com, def.com
  2. Repeat Step 4 for each domain, pointing to right sub directory
  3. Install Wordpress in each of the sub directory

Hope this helps.

Regards, Ashwin

@ashwin @postcreative These were the same things I was wondering as well. I’m looking at signing up with these guys and been trying to get an answer about this. I don’t know anything about cloud hosting yet and have about 5 WordPress websites to transfer if I switch.

From what @ashwin said, I don’t need another droplet for each WordPress–just install them all in their own sub-directory on whatever lamp is on my single droplet?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 2, 2013

@mkstlouis: Yes. (LAMP is an abbreviation for Linux, Apache, MySQL, PHP).

You can simple follow this article for each Wordpress site you have, but replacing /var/www/ with /var/www/sitename.com so that you can have multiple sites on your droplet.

Hi @kamain7, I’ve already installed LAMP and created a virtual host separate from this article. So, how do I work around Step 4.

Also, I must be reading this wrong or something. Isn’t Step 3 still installing only WordPress install, with one username? Wouldn’t you have to repeat this step as well for multiple single domain WordPress installs so that each one has it’s own configuration (username,password etc…)? Thanks!

@mkstlouis Sorry, I did read this wrong, I didn’t see that you were installing WordPress in the root and then moving it to the virtual hosts. You can delete my other comment if you’d like.

I get a error 1064 when trying to import my database… http://prntscr.com/1jdh8l

Can anybody help with this please ?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 4, 2013

@ozzrkhan: The sql dump is an archive, you have to extract it first:

<pre>tar xvf file.tgz</pre>

Kama that was fixed :) I was doing it wrong way…

Now problem is that everything seems to work just fine but some parts of my website.

My website is a url shortener type of website… Database connections are working just fine but at some places errors are coming up.

For instance when I create a new shortened link it does send the data to db and I can see that in tables. But when I visit that url I get the 404 error, which basically means it is unable to read from database.

Can you help with this matter ?

Script doesn’t have errors because I have it running just fine on my other hosting.

Or if there is any way to change PHP defaults that will help too !

Thanks a lot !

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 5, 2013

@ozzrkhan: If it’s a MySQL error, add <pre>die(var_dump(mysql_error()));</pre> (assuming you’re using mysql_*) after the query that’s failing - it should output the error.

Here is the error i am getting. i did change my port to 5500 so i dont know where the 22 is coming from. it should be change

root@radio:~# sudo service apache2 reload

  • Reloading web server config [fail] root@radio:~# scp ~/aolor/downloads/backup.zip root@192.241.231.174: ssh: connect to host 192.241.231.174 port 22: Connection refused lost connection
Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 6, 2013

Run this command instead:

<pre>scp -P 5500 ~/aolor/downloads/backup.zip root@192.241.231.174: </pre>

Side note: I had to create an .htaccess file for my post links to work. I didn’t see that mentioned, but if someone is using a permalink style that uses htaccess/mod_rewrite, they’ll need to be sure it’s all enabled and the htaccess file is in place.

how to install wordpress site in digital ocean virtual server and also how to create a database (MySQL)?

Very interesting stuff, just created an account to try some of this out, I would be interested in a one click WP install where virtual hosts are already configured, instead of using one click then having to go backwards and reconfigure apache would it not make more sense to create an option for those that are migrating from shared hosts and want to get started with as little head bashing as possible? Just curious if someone has a config like that now.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 21, 2013

@reelstuff: We do have a one click installation image that sets up the virtualhosts for you (along with other stuff).

https://www.digitalocean.com/community/articles/one-click-install-wordpress-on-ubuntu-12-10-with-digitalocean

I’m getting stuck on <b>Step 5</b>. Am I correct in that <b>Step 5</b> assumes that the destination server has password-based logins? How can one transfer files to a destination server that’s set up w/password-less logins (i.e., login only possible w/an SSH key)?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 21, 2013

@Pablo: <strong>scp</strong> uses regular SSH authentication – if you have your SSH keys properly set up it should just work.

@reelstuff,

If you already have a WordPress site up and running on shared hosting, check out the WP <a href=“http://wordpress.org/plugins/duplicator/”>Duplicator</a> plugin.

<b>“You’ll be able to download the MySQL database in *.sql.gz format.”</b>

I’m trying to migrate a WordPress installation from GoDaddy to DigitalOcean. GoDaddy does not offer cPanel, but does provide phpMyAdmin access. Unless I’m missing it, exporting the database in <b>.gz</b> format is not an option.

RE: compression, the options available with GoDaddy are (i) none; (ii) zipped; and (iii) bzipped.

The database I’m working with is relatively small. Are either of these three options preferred over the other two? If a DB is exported as a <b>.zip</b> or <b>.bzip</b> file, will the steps in the <b>Restore the Database</b> section remain the same?, e.g.

<pre>mysql -h localhost -u database_user -p database_name < backup_db.sql.zip</pre>

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 6, 2013

Pablo, bz2 archives are usually the smallest. You can extract the sql file before importing it:

<pre>tar vxjf backup_db.sql.bz2 mysql -h localhost -u database_user -p database_name < backup_db.sql.bz2</pre>

You can also just export it as a regular .sql file without archiving it.

Please let me know if that works.

I dont know what I am doing wrong, but when I try to restore database I am getting access denied for use errors… example: mysql -h localhost -u username -p dbname < backupname.sql.gz

error: ERROR 1045 (28000): Access denied for user ‘user’@‘localhost’ (using password: YES) at first I thought I forgot password, I checked wp-config.php retyped password again, the same error.

Then I went back to shared hosting, created new user, new password,assigned full rights, created new backup, again did the upload, try to restore db, again the same error. What am I doing wrong I wonder. I am using Wordpress predefined droplet.

On the step 6, I run “sudo service nscd restart” command but I get error like “nscd: unrecognized service”. Whats the solution ?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 11, 2013

@melihmucuk: You can ignore that step – it’ll work perfectly fine without it.

nscd is probably not installed on all systems so if it’s not there you can just ignore it.

@Kamal Nasser: Ok. I have 2 more questions,

1-) On Step 4, we should create virtual host for our domain, in this tutorial its specified “yourdomain.com” . I want to use with subdomain, in this case Should I replace “yourdomain.com” with “sub.yourdomain.com” ?

2-) Skip all these settings, I install wordpress on ubuntu and then restore my db, it is possible ? Because there are lot of linux command and I don’t know linux and feel relaxed working with linux server.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 12, 2013

@melihmucuk:

  1. You should replace it with the actual domain that is going to serve the contents which is sub.yourdomain.com
  2. Yes that should be enough but make sure you have the same Wordpress version on both ends.

hi, I can have wordpress and django in the same droplet??

I dont get these lines:

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

what is that locations and ip and all. is it of old hosting?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 18, 2013

@tusharthakur123: <pre>~/Downloads/backup_db.sql.gz</pre> should be replaced with the path of your actual sql dump download (step 1). The same goes for <pre>~/Downloads/wp-content.zip</pre>

<pre>username@server_ip_address:</pre> should be your username on your droplet and your droplet’s IP address. E.g.: <pre>wordpress@1.2.5.8:</pre>

@Kamal Nasser Thanks for Quick.

I have old hosting file in about 200MB so it is not convenient. any way if I created zip of files to my old URL like: http://olddomain.com/files.zip and db.zip

also I cant get the current location path. I mean suppose I put zip file on my desktop then what will be the location in this line? scp ~/Downloads/backup_db.sql.gz username@server_ip_address:

Thanks in advance.

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.