Migrating to Digital Ocean

I’ve had a number of tiny WordPress sites on shared hosting (BlueHost) for over a decade. It’s been pretty great. Shared hosting has a lot of benefits: it’s cheap, it’s relatively easy to manage, e-mail is included, and there’s basic support.

The drawback is that servers aren’t generally optimized for WordPress performance, and once a site begins to scale in traffic you need to move it to a managed host or a VPS.

droplet-screen

I’ve been wanting to try DigitalOcean (one of the many cloud providers) because my hosting bills have been growing. I still have my basic shared hosting with BlueHost, but I also have large sites on WP Engine, and a VPS with WiredTree. They’ve all been great and have their own benefits, but the idea of consolidating into one provider and reducing my hosting bill significantly is a huge draw.

The big hurdle with cloud hosting for most people is that there are no easy control panels to log into and there’s no support tech available on chat. You also need to be at least somewhat comfortable on the command line.

To hopefully make it a bit easier, I’m sharing the steps I used to set up and migrate multiple sites to the same Droplet.

If you just need one site, it might be simpler to have DigitalOcean set up WordPress (under Applications) when you create the Droplet, and then skip to the migration steps.

Droplet Creation

a) Create an account with DigitalOcean.

b) Click “SSH Keys” and add your public key.

Cheatsheet: To copy your public key, open terminal and enter:

pbcopy < ~/.ssh/id_rsa.pub

That should save it to the clipboard, which you can then paste in the field for DigitalOcean.

GitHub has a good instructions for generating an SSH key if you don't have one.

c) Click "Create" to build a new Droplet.

  1. Give the Droplet a name you'll remember.
  2. Choose a size (I went with the smallest to house my low traffic sites).
  3. Choose location and server, I went with the defaults (New York, Ubuntu 14.04 x64 server).
  4. Add your SSH key.
  5. Click "Create Droplet".

Install WordPress

Your droplet will take few seconds to set up. When it finishes the IP address will be at the top of the dashboard. Go ahead and copy this. Throughout this tutorial I'll refer to that IP address as xxx.xxx.xx.xxx.

Next step is to SSH into your new Droplet. You need to use the root user to do this. Open terminal:

ssh [email protected]

(You shouldn’t need a password if your SSH key was added correctly)

To set up the WordPress site(s), I used a script called EasyEngine. You can read the full documentation.

To install EasyEngine, run this from your Droplet:

wget -qO ee rt.cx/ee && sudo bash ee
ee stack install

For this tutorial, I'm going to use "example.com" as the domain we're setting up. Just replace this with your own.

If Easy Engine prompts you with "Enter hostname [FQDN]:", go and enter the domain name ("example.com").

To install WordPress (including the database set up) run:

ee site create example.com --wp

(There's also other options listed in the EasyEngine docs you can check out.)

Once EasyEngine sets up the site, copy down the username / password it creates if you are setting up a new site and need access to it. If you're migrating a site (as I am), then this isn't important as the database will get overwritten.

EasyEngine installs sites in the "/var/www/" directory. If you want to view those files, go:

cd ../var/www/

View the Site

To view the new WordPress site on the Droplet, you'll want to update the hosts file on your local machine. Leave the remote shell and open a new terminal tab on your local machine. To edit the hosts file:

nano /private/etc/hosts

You'll want to add a new entry to the bottom of the hosts file that has the new IP and the domain name you wish to point:

xxx.xxx.xx.xxx example.com

When finished, save out the hosts file.

At this point, let's also clear cached DNS so we can make sure we're viewing the right site.

From terminal run:

sudo killall -HUP mDNSResponder

Then open Chrome (I'm just assume you use Chrome), and go to this url: chrome://net-internals/#dns. Click the "Clear Host Cache" button.

Now, if you migrate to example.com you should see a fresh install of WordPress. Perfect!

Migrate a Site

There are many different approaches you could take to migrating a site. I've been using WP DB Migrate Pro(affiliate) as it's dead simple to set up and I don't need to SSH into multiple servers.

But doing it manually is not too rough. For the purposes of this tutorial, I am going to assume you have the "wp-content" directory for the site you wish to migrate and an export file of the mySQL database ("database.sql") saved on your local desktop (though the steps aren't too much different even if you are ssh'd into a different remote server).

First we'll want to compress the "wp-content" directory:

tar -czf wp-content.tar.gz wp-content/

Now, transfer that to the remote directory where the new WordPress install is:

scp wp-content.tar.gz [email protected]:/var/www/example.com/htdocs

(Depending on the size of the file, this might take quite a while to transfer.)

Let's also get the database export uploaded, we'll put this in the root of the new site though:

scp database.sql [email protected]:/var/www/example.com/

Now, switch back to the remote shell and make sure you're in the files directory for the site (/var/www/example.com/htdocs). We're going to remove the current "wp-content" directory, unzip the new one we uploaded, and then remove that zip file:

rm -rf wp-content
gunzip -c wp-content.tar.gz | tar xopf -
rm wp-content.tar.gz

Next is the database. Let's grab the database credentials that were set up by Easy Engine.

Navigate to the root of the new site directory (/var/www/example.com/) and open the wp-config.php file:

nano wp-config.php

Copy down the database_name, username and password.

We've already uploaded the database export to this directory, so now let's overwrite the current database (replace username and database_name with your own of course):

mysql -p -u username database_name < database.sql

Check in the browser to see if your site loads at example.com. If so, we can now remove that database file:

rm databse.sql

Everything is good right? The site is up, looking nice. Great, one more step.

Migrate DNS

Remember we set our hosts file locally to point to the new IP address? Let's remove that now and flush the DNS again. This should get us pointed back to the original site. (I'm not going over the steps again, just hop back up to that point in the tutorial and follow it in reverse).

In the DigitalOcean control panel, click "DNS" and then "Add Domain". You'll want to add the domain name for the new site and select the Droplet it points to.

Now you're ready to point the nameservers for the actual domain to DigitalOcean. You'll need to find where your domain is hosted, log into that control panel, and point the nameservers to:

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

Those steps are described in more detail here in the DigitalOcean docs.

Wrap Up

Hopefully this has been helpful. It took a few hours of extensive Googling and a lot of trial and error for me to piece together all the steps. If you have any additional suggestions or tips, feel free to leave them in the comments.

If you want to try DigitalOcean, feel free to use my referral code. It'll get you $10 off and send a few dollars my way too.

If you haven't had enough, I also posted some additional tricks and shortcuts for syncing files.

Additional Tools

If you're not sure how to edit or view your databased without PHPMyAdmin, I found a tool for OSX called Sequel Pro which is free and worked pretty well.

Server Pilot was also recommended to me by a few folks. It provides a control panel and some other features which make it easier to deploy to cloud providers.

About Devin

I am a developer based in Austin, Texas. I run a little theme shop called DevPress and help manage a WooCommerce shop with Universal Yums. Find me on twitter @devinsays.

20 Responses

  1. Hey Devin,

    Thanks for sharing this, especially the steps. I had on several occasions to move to Digital Ocean, but then moved back to my shared host, as there are too many other things to take care of, such as scripts to automatically start a service like mysql or nginx when they shut down, etc.

    Easy Engine is a great tool too but managing server is just time consuming.

  2. I’ve consider cloud hosting overkill since you’re planning for traffic that just may not happen. Somehow a VPS that supports multiple IPs (helps group sites into top spammer, flaky owners, folks that trust me, etc :) and lets you tweak its config seems better. Have you tried becoming a Verio affiliate. Their VPS are great (I rented one for 14 years).

  3. Awesome to see another dev fond of DO! Five years ago I did everything possible to avoid running my own VPS. It just seemed like too much responsibility. Last year I moved all of our sites to new Digital Ocean SSD drives. It cut my hosting costs dramatically and sped up all sites tremendously. I’d echo the UFW recommendation. We also ditched GitHub for private GIT repo hosting and now use GitGo.io. It deploys automatically to our DO servers. Super happy with our setup now and the ecosystem around DO is full of innovation. I really sound like a fanboy, eh?

  4. Gilbert

    Wanted to migrate over to digital ocean from my current host for a site that potentially will do over 10k hits a day (currently under 5k), would a basic DO plan be sufficient enough for this kind of traffic?

  5. Hey Devin,
    I think a lot of people are doing this. I checked out 5 different solutions and DO won on price/performance/ease-of-use.

    I migrated my client sites using Backup Buddy and so far all is good. Several of the sites are using your theme actually.

    I am curious to see the impact on our SEO efforts to have the added speed.

    I did trial WP Engine, but there are some limitations with plugins with them (specifically Backup Buddy). WP Engine does have insane support though.

    Digital Ocean has a lot of useful tutorials for this as well:

    WordPress Install
    http://c3p.me/1vn6JQQ

    PHPMyAdmin Install
    http://c3p.me/1BmpJ6t

    Thanks for the article.

  6. John

    I’m looking into VPS. For clients, i’m curious to know what service do you recommend for email? I don’t want to get into managing my own email server. integrated email is the bit that really makes shared hosting convenient.

    Thanks

  7. Jay

    I’m in the process of migrating several WordPress sites to DO and I’ve had tab open, displaying this page, for about a week now. Thanks for providing a great resource.

    There are three things I think most people will run into that are worth mentioning:

    1) When exporting the database, you’ll want to use the Custom export method and select “Add DROP / TABLE / VIEW …” in the object creation options.

    2) Many will be switching from Apache (LAMP) to Nginx (LEMP) and will need to translate any rules defined in their .htaccess to the corresponding Nginx statements. They should be added to the Nginx config file in /etc/nginx/sites-available/mysite.com.

    3) Those who change their DNS to the DO nameservers will want to replicate the individual DNS records over to DO. Alternatively, you could forgo changing the nameservers and point the A record on your current DNS zone editor to your new droplet’s IP.

  8. Denisa

    I’ve also migrated two websites to DigitalOcean, I have to say I am quite happy with my decision.

    I used ServerPilot control panel which is awesome, easy to use and quite fast. Basically it installs the necessary software to run WordPress for you with a single line of code. Also you have a very nice and clean control panel to manage databases and so on.
    All in all is definitely worth a try.
    I wrote a nice step by step guide how to install ServerPilot on a DigitalOcean droplet and run WordPress.
    Here is the link: http://www.mybloggingthing.com/digitalocean-serverpilot-wordpress-setup/

    Thanks Devin!

  9. nikolai

    Hi! I can not figure what username and database I should enter at “mysql -p -u username database_name < database.sql" . The username and database of the new wordpress install on digitalocean or the old one ?

  10. Mark Paralovos

    Migrating the database is a little tricky. And yeah, i’m coming from shared hosting so i’m thinking a LAMP stack instead of a LEMP stack.

    So, when i navigate to my site (after uploading my host file and DB) i get a blank page. The correct favicon but no content.

    If i go to example.com/wp-admin it offers the login page, but my credentials are not correct.

    So – and this may have been asked before – when importing the db the steps look like this:

    1. Go into /var/www/example.com/ and edit wp-config.php
    2. copy the username and password for the db from this file
    3. overwrite the username and password in the sql file that i uploaded to /var/www/example.com with the username and pass from #2? Or use the username and pass from the example.com website i’m migrating?
    4. db is now written and nothing else needs to be done, delete the .sql file i uploaded.

    I tried using the credentials from #2 and it didn’t work. I tried using my username and password (that was backed up in lastpass) and i wrote those to the db. no joy. Just a little confused.

    Also, would be helpful to put the windows steps to edit the hosts file (i figured it out, but was a little confused because the server computer is ubuntu and you had ubuntu commands for editing the hosts file).

    Thanks for the write up..i’m closer than i’ve been to migrating to DO. Just need this final step to work!

Leave a Reply