How to Host WordPress in Digital Ocean VPS (Step by Step Tutorial)

A simple walk-through in how we can host our WordPress site(s) in Digital Ocean (un-manage VPS) Cloud Server. Using Ubuntu and LAMP.

Introduction

digital-ocean

There’s a lot of WordPress hosting out there, and the price is affordable. But recently several developer friend at Theme Hybrid community shared that they move to Digital Ocean because of several reasons:

  • Better server (response time, ssd storage, bandwidth) than shared hosting
  • Dedicated IP for each droplet
  • Simple interface/easy to use (for a unmanaged vps)
  • Simple backup and we can create snapshot of our server
  • Easier to scale when we need
  • Amazing price, start at $5/month
  • Charged per hour usage, so we can easily create a test site (for clients) and destroy it when we no longer need it.

Note:
Managing your own server is complex, this walk-through and some explanation maybe not complete or I forgot some step in configuring the server, this walk-through/some part might not be the best practice. If you find this post is not accurate, incomplete, not safe, or there’s a better way to do this, please contact me via contact page or comment.

What I Use

  1. $6, you can pay using credit card or paypal. I activate backup option and it cost 20% ($1) of the droplet usage cost ($5).
  2. I’m using windows computer (Windows 7)
  3. We need to install PuTTY to use SSH. You need to install PuTTY installer (the one with “A Windows installer for everything except PuTTYtel“)
  4. FileZilla to easily browse our server.
  5. Notepad ++ text editor.

If you use windows, you need to install the PuTTY and Filezilla to do this walk-through. Notepad ++ is optional, you can use your preferred text editor.

Create your first Droplet

Droplet is a term in Digital Ocean for a server instance.

create-droplet

After you create your account and add some balance via paypal/credit card, you now can create a new “droplet” by clicking “Create” button from your control panel.

There’s going to be several option which is straight forward and easy to understand:

Droplet Hostname

You can input any text here, but if you have a domain you want to host you can add it there, you can rename them later. But It’s best to add it if you already have domain to host in your droplet.

Select Size

you can select server size, I use the lowest package ($5) but might need to upgrade later/create new one. I might need the $40 package to hosts my sites + client sites.

Select Region

You can select server data center, pick one nearest to your area/visitor area. For example, I live in Indonesia (south east asia), for my local client with Indonesia target visitor, Singapore server might be best, for this site New York or California region is better.

Select Image

select-image-digital-ocean

There’s several tabs in this section. We’re going to skip “Linux Distributions” Tab because we’re going to install via “Applications” option.

In this Applications tab, were going to select “LAMP on Ubuntu”, so our server will be using Ubuntu OS with LAMP (Linux, Apache, MySQL, and PHP) pre-installed. You can also use “WordPress on Ubuntu” option which will also install WordPress for you. I try to use this option on my first droplet, but i didn’t really like the setup, and of course because they misspelled “Wordpress” 🙂

Settings

In settings section I enable VirtIO and Backup. Even though we can create “server snapshot” we need to do it manually and it require to “poweroff” the server. Backup is similar to snapshot but it’s done automatically (Digital Ocean stated that it’s every several days depend on the size of the Droplet) and it’s done in the background, so we don’t need to turn off/poweroff our server.

That’s it, now we have a droplet to use.

After all done, you’ll get an email about your server account/root access to your server:

  • IP Address: {your droplet IP address}
  • Username: root
  • Password: {your root password}

You can try visit your site, just type your IP Address in your browser.

Connect Your Domain/DNS

Change Your Domain Name Server

Set your domain Name Server to point to Digital Ocean Name Server:

NS1.DIGITALOCEAN.COM
NS2.DIGITALOCEAN.COM
NS3.DIGITALOCEAN.COM

Add your domain using Digital Ocean DNS Setting

  1. Go to DO (Digital Ocean) Control Panel, and click “DNS” button/visit DNS setting.
  2. Click “Add Domain” Button.
  3. Type your domain in the “name” field.
  4. Select your droplet, this will auto fill the “IP Address” field.
  5. “Create Domain”.

In the next section, I’m using example-domain.com, of course you need to use your actual domain name.

Setup Your Domain DNS Record

  1. Click “Add Record” button.
  2. Select “CNAME” record type.
  3. In “Enter Name” field type “www
  4. In “Enter Hostname” field type “example-domain.com.” (with trailing dot in the end).
  5. explaination: This is to properly redirect “www.example-domain.com” to your site using ServerAlias in Apache config.
  6. Crete another Record, this time select “TXT” in record type.
  7. In “Enter Name” field type “@“.
  8. In “Enter Text” field type "v=spf1 mx -all" (with quote)
  9. Crete another Record, this time select “MX” in record type.
  10. In “Enter Hostname” field type “example-domain.com.” (with trailing dot in the end).
  11. In “Enter Priority” field type “0” (zero).
  12. explanation: this is to use mail functionality used by our site/WordPress for email notification and contact form. If you are using email hosting such as Google Apps/GMail there’s a useful button to setup your MX Record using GMail configuration.

Wait until DNS resolved, and you can try to visit your site and type http://example-domain.com in your browser.

If the DNS Propagation is complete, and you still cannot visit your site, Reset DNS cache in your computer.

Using PuTTY and SSH to manage your server

Create SSH key using PuTTYgen

  1. Open PuTTYgen app in your computer (you need to install PuTTY first).
  2. Click “Generate” button.
  3. Wave your mouse around in the window area until progress bar complete, this to generate a unique data.
  4. After it’s done, do not close the app yet.
  5. Open your DO Control Panel in browser and click “SSH Keys” link to visit SSH Keys Setting.
  6. Add “SSH Keys” and input key name (something like “my computer” or “my laptop”)
  7. Add “Public SSH Keys” you get from putty.
  8. “Create SSH Keys”, now you’ll get an SSH Keys you can use when creating your Droplet next time.
  9. Back to your PuTTY apps, and save “Public Key” and “Private Key” file (important).

Configure PuTTY session

  1. Open PuTTY app, and you’ll see “PuTTY Configuration” windows.
  2. Type in the “Host Name (or IP address)” field with the Droplet IP address from the welcome email, make sure the port number is “22”, and the connection type is “SSH”.
  3. Click PuTTY Config sidebar and click “Connection > SSH”, make sure it’s using “2” as “Preferred SSH protocol version”.
  4. Click “Session” from sidebar to go back to previous screen.
  5. In “Saved Session” field, input profile name, you can save it as “[email protected]
  6. Click “Save” to save the “profile” so we can re-use this setting in the future.
  7. Click “Open” to start your PuTTY session.
  8. You’ll get a terminal window similar to Windows CMD/ Command Prompt

Login and use PuTTy

In “terminal” Window you’ll need to input your login as “root”

login as: root

After that you need to input your root password (you get this in email after you create your droplet). While you type your password, your password will not visible, however it is still being entered as you type. Now you are loggin to your server.

If your SSH folder does not yet exist, create it manually using this command (one by one):

mkdir ~/.ssh
chmod 0700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 0644 ~/.ssh/authorized_keys
  1. mkdir is to create a directory
  2. chmod is to set file/folder permission
  3. touch is to create a file.

So from this commands, we are actually create a “.ssh” folder in our server, create a file “authorized_keys” and set it to the correct file permission.

Now, we need to edit this file and add our SSH Keys. Type this command:

nano ~/.ssh/authorized_keys

nano is a terminal text/file editor.  in this command we are editing the file “authorized_keys” using nano.

  1. Next step is to copy your “SSH Public Keys” from PuTTYgen to your clipboard.
  2. Go back to PuTTY terminal, using your mouse, “right-click” to the screen.
  3. This will “paste” your clipboard to PuTTY.
  4. Press “CTRL+x” to exit
  5. Press “y” to save changes.
  6. Press “Enter” to save the file.
  7. Type “reload ssh” command in terminal.
  8. Type “exit” to exit PuTTY session. (this will close the terminal)

How Copy-Paste in PuTTY works

  1. In Windows, we are used to “copy” using “CTRL+c” and using “CTRL+v” to paste. In PuTTY it works differently.
  2. To copy text from PuTTY, simply select it with your mouse, with no extra action, every time you select a text, it’s saved to your clipboard.
  3. To paste the text, simply use “right-click”/”middle click” of your mouse.
  4. You can also use keyboard “SHIFT+INS” to paste.
  5. You can copy-paste from PuTTY to other app such as text editor or browser.

Re-Configure PuTTY to use SSH Keys

After it’s done, we’ll re configure our PuTTY profile to use our SSH Keys so we don’t need to input our password every time we login to our server.

  1. Open PuTTY again.
  2. Select your saved profile. example: “[email protected]
  3. Click “Load”
  4. Navigate to “Connection > Data” and input “root” in “Login details” / “Auto-login username:”, this to always use “root” user when using this saved session.
  5. Navigate to “Connection > SSH > Auth” and  “browse” our “private key” file we saved previously using PuTTYgen.
  6. Navigate back to “Session” and “Save” our profile.
  7. Click “Open” to start session terminal.
  8. You are no longer asked for user name and password.
  9. type “exit” to exit PuTTY (if you want to close session).

Using FileZilla to browse and manage your server

In this post, we are going to use FileZilla a lot, and less using command line. Using command line is faster and easier for advance user but this post is for a linux newbie (like myself), I think it’s important to understand the structure using some UI. I found it easier when I just getting started, because I don’t know much about command line.

Use “Site Manager” to save your FileZilla session

  1. Open FileZilla.
  2. Open “Edit > Setting”
  3. Navigate to “Connection > SFTP”
  4. Click “Add keyfile..” and select your “private key” we get from PuTTYgen.
  5. Click “OK” to save.
  6. Open “File > Site Manager”.
  7. Click “New Site” to create a new profile for our server.
  8. In “General” tab, input “example-domain.com” in “Host”, “22” in “Port” and select “SFTP” in “Protocol” drop down, in “Logon Type” select “Interactive“, and in “user” input “root“.
  9. Click “Connect” to start our session.
  10. You are login to your server using FileZilla and now can browse your server and edit files, upload, etc.
filezilla-root-folder

When you first browse/login in FileZilla you’ll see “root” folder. Click the folder link above it to browse your whole server to get better understanding of your server folder structure.

Your public_html folder for your site/domain located on “/var/www” folder.

Server Preparation and Configuration

Before we start installing WordPress in our domain/site, we need to configure our server to make sure all functionality working correctly and easier for us to manage in the future. Open PuTTY again and start a session.

Enable Mail

currently our server don’t have server we need to install sendmail app using command:

apt-get sendmail

Install Unzip

This is optional, but very useful app, using this we can easily unzip a file using command line. Type this in the terminal:

apt-get unzip

Install Firewall

We need to install Firewall application to make sure our server secure. Read this tutorial in how we can configure and setup firewall: How To Setup a Firewall with UFW

Install phpMyAdmin

To manage our database, we can to install phpMyAdmin, but before installing phpMyAdmin, read this tutorials to get better understanding in managing MySQL databases and users:

And follow this tutorial to setup a secure phpMyAdmin installation: How To Install and Secure phpMyAdmin on Ubuntu 12.04

Further Server Configuration

Create New User and Grant Root Privileges

In the tutorial above/previous section we use “root” user to manage our server, it’s better to create a new user and assign root capability than using root user. When not using root user we need to use “sudo” command to do stuff using PuTTY. Example when we need to install app, previously we use:

apt-get unzip

When using other user, we use:

sudo apt-get unzip

To create a new user and grant user privileges follow this tutorial: Initial Server Setup with Ubuntu 12.04

Configure Virtual Memory (Swap File)

In DO Droplet we Swap File is not enabled by default. Swap file is like an extended RAM / Memory, when memory is already full, “linux swap” will be used. In short with swap file, we can get/use more memory than was originally physically available. But because it’s using disk  drive this extended memory is slower than RAM, but because Digital Ocean using SSD, the performance of swap file is better than regular server using disk drive.

To configure and setup swap file in your server, you can follow this tutorials:

Install Subversion

Even though more and more developer moving to GIT, but I’m still using Subversion. I even use SVN for my GitHub Repository. If you want to install subversion and use it to install WordPress and manage your project in your server you can follow this guides:

Create Your First WordPress Install

This section assume that you already familiar with Installing WordPress Manually (Without using one click install such as Fantastico Deluxe)

First, you need to check that your domain already connected to your server by visiting your site, you’ll see a simple html page with content like this:

It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.

When you browse using FileZilla, to “/var/www” folder, you should see this file and one extra file “info.php”. This is the folder where we will install WordPress.

Create MySQL database using phpMyAdmin

If you already following the guide from the link about phpMyAdmin from previous section, creating database is simple. Just create a blank database/import your database if your site is already have content from previous hosting.

Prepare and upload your WordPress files

Prepare your WordPress file with all configured, wp-config.php need to use your new database name and user, etc, and delete all files in “/var/www” and upload your WordPress files.

Set the owner of directory and files

Because we upload and create files in “var/www” directory as root user, the folders and files are “owned” by “root”. You can see this from FileZilla file browser in “Owner/Group” column. We need to change the owner to this files to “www-data” to make sure WordPress have the permission to modify the files, such as uploading files, updating theme and plugins, and core update.

The “www-data” user is the server, so the App/Server (in this case WordPress) own it.

root-to-www

We need to use PuTTY to do this, open PuTTY and type this command:

chown -R www-data:www-data /var/www

After that, refresh the FileZilla file browser to check the changes.

Install WordPress

You should be familiar with this process, visit your site and install 🙂

Testing your WordPress install

To make sure everything working correctly, we can do simple testing of our WordPress install.

  1. Change your permalink to test pretty permalink, visit your pages/post and see if pretty permalink is configured/enabled.
  2. Upload image using media uploader to make sure uploading files are working correctly.
  3. Change akismet version to previous version, and check for update, and update it. This test is to make sure WordPress upgrader is working.
  4. Install Contact Form 7 and create form and submit it, this is to check mail functions working.
  5. Logout to your site and click “Lost your password” and request link to create new password via email. This to check WordPress email notification and email function.
  6. All mail test should be sent to your email account without going to your mail “spam” folder.
  7. Any other test you need..

Connect More Domain and assign folder

Of course we might want to host another WordPress site in our server, this is how you can do it:

Add domain in DNS setting

It’s the same step with previous step about connecting your domain to your droplet above. We are going to use “secondary-domain.com” as example. So we need to set our domain name server to DO name servers, and add the domain in DO DNS Control Panel.

Visit your domain

After DNS is resolved, when you visit “secondary-domain.com” in your browser, you’ll see your main site which is located in “/var/www” folder path. We need to change this to another location/folder/path.

Assign folder for your domain

Using FileZilla, navigate to “/etc/apache2/sites-enabled” and create a file, for example “secondary-domain.com”, edit this file (right-click > view/edit) and add this content, and upload/save back to your server.

<VirtualHost *:80>
    ServerName secondary-domain.com
    ServerAlias www.secondary-domain.com
    DocumentRoot /var/web/secondary-domain.com
</VirtualHost>
  1. VirtualHost *:80 : is to set up Virtual Host for port *:80 (accessible via browser)
  2. ServerName : is the domain
  3. ServerAlias :  is the domain alias, when we visit “www.secondary-domain.com” we will redirected to “secondary-domain.com”. This is optional if you don’t need server alias.
  4. DocumentRoot :  is the path for this domain/host. as the above we are using “/var/web/secondary-domain.com” for our host path location, so our files for our secondary-domain.com will be located there.

All files in “sites-enabled” folder is loaded as Apache virtual host configuration, In different version of Apache this config file need to use “.conf” ext. To understand more about it you can check the files “/etc/apache2/apache2.conf”.

You can also read this tutorial: How To Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS.

Note: you can create virtual host config file with any file name and assign any path in your server, the above configuration is an example to make it manageable and easy to understand.

Create folder in the path set previously

  1. Now we need to create the folder, navigate using FileZilla to “/var” folder, and create a folder “web”.
  2. Inside this (“web”) folder, create another folder “secondary-domain.com”
  3. Upload a dummy file, for example: “testing.txt” with a content “WOOHOO!”.

Restart Apache to load configuration we added

We need to go back using PuTTY, and type this command:

service apache2 restart

After Apache service is restarted successfully, visit “http://secondary-domain.com/testing.txt” using your browser, you should see the text “WOOHOO!” we added.

Installing WordPress in Secondary Domain

Basically the same step as before, the difference is we will use the path “var/web/secondary-domain.com”.

Add Sub-domain and install WordPress

And we can also install a sub-domain of our domain using similar step as adding secondary domain.

Add sub-domain using DNS Setting

  1. Go to DNS Setting in your DO Control Panel.
  2. Select / “view” the domain you want.
  3. Click “Add Record” and select “A“.
  4. In “Hostname” field, input the sub-domain, for example: “subdomain“.
  5. In “IP Address” field, input your droplet IP Address (should be the same as “@” A Record if you want to host it in the same droplet.

So for example we create a sub-domain of our “secondary-domain.com”, with the above setup, the sub-domain we create is “http://subdomain.secondary-domain.com”.

Visit your sub-domain url

When you visit “http://subdomain.secondary-domain.com” you’ll see the same content as your main site (that hosted on “var/www”) which is “http://example-domain.com”.

Assign “public_html” folder for your sub-domain

Same with the step above, we create a file “subdomain.secondary-domain.com” in “/etc/apache2/sites-enabled” and add content:

<VirtualHost *:80>
    ServerName subdomain.secondary-domain.com
    DocumentRoot /var/web/subdomain.secondary-domain.com
</VirtualHost>

As you see, we do not create “ServerAlias” config for this sub-domain.

The next step is the same as configuring secondary-domain.com, we need to create the the path, and check that the sub-domain linked to the DocumentRoot path after we restart apache.

Crete Snapshot of Our Server

After all configured correctly, if you want to re-use the server setup to another droplet, we can create an image / snapshot of our server.

Power Off / Turn Off Our Server.

Using PuTTY type this command:

poweroff

You’ll be disconnected from your server, because it’s now off.

Create Snapshot Image

Go to DO Control Panel, and navigate to “Droplet > Your Droplet”. Click “Snapshot > Take a Snapshot”.

After you create the snapshot you can restore your server to this state or create another droplet using this “image”.

Digital Ocean Promo Code

If you already have Digital Ocean account, go to your “Billing” page in DO Control Panel, and add Promo Code “DOIT10” to get $10 credit.

If you don’t have Digital Ocean account yet, you can use this link to sign up (my DO referral link):
Sign-Up to Digital Ocean >

Thank You 🙂

– end –

9 Comments

  1. James

    Hi David,

    Thanks for the tutorial. The only thing I will mention is that the LAMP stack in Ubuntu 12.04 is not necessarily up to date. For example, Apache 2.4 is out and is much faster than the 2.2 branch.

    Do you think you will stick with DO for hosting? I am on Media Temple, but am not sure if I should take the plunge and use DO. I already know basic linux commands, so maybe it won’t be that hard.

    – James

    • David

      Yes, but not strictly DO. Maybe other unmanaged vps/dedi.

      Apache, Ubuntu, PHP etc, we need to update it ourselves. The WordPress Image (One app install) is using the newer Ubuntu (13) and Apache, btw.

      This is “unmanaged” server. That means we need to update, secured, backup, etc ourselves. It’s not for everyone. Lot’s of responsibility.

      And it’s almost no support for technical stuff. This service simply assume that you already know how to manage the server.

      • James

        That is true, I need to learn more before I use it. It can be dangerous to use it without knowing what you are doing (well, I know basic commands, but not that many) 🙂

  2. Alex

    Great tutorial!

    I encountered an issue when trying to install sendmail, unzip, etc. via PuTTY (I chose to install WordPress automatically. Ubuntu 14.04).

    In case it helps anyone else: when I entered apt-get xxx (xxx being the name of the application) into PuTTY, I got an “E: Invalid operation xxx” error message. However, entering sudo apt-get install xxx all worked fine.

    • Alex

      Aggh! I didn’t mean to include sudo in my comment above. I copy and pasted from elsewhere, but it’s the install part that did the trick.

  3. subhan

    Thank you so much. This article really solved my problem. I bookmarked this page as my future reference. A really helpful article.

  4. subhan

    Hi,

    For part “Testing your WordPress install” point 6 “All mail test should be sent to your email account without going to your mail “spam” folder.

    Emails sent by my contact form and WP password recovery went into Spam Folder.

    What should we do to solve this problem?

    Thank You in advance.
    Sn.

Comments are closed.