2014-04-24

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

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 save, or there’s a better way to do this, please contact me via contact page or comment.

What I Use

$6, you can pay using credit card or paypal. I activate backup option and it cost 20% ($1) of the droplet usage cost ($5).

I’m using windows computer (Windows 7)

We need to install PuTTY to use SSH. You need to install PuTTY installer (the one with “A Windows installer for everything except PuTTYtel“)

FileZilla to easily browse our server.

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.

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



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:

Add your domain using Digital Ocean DNS Setting

Go to DO (Digital Ocean) Control Panel, and click “DNS” button/visit DNS setting.

Click “Add Domain” Button.

Type your domain in the “name” field.

Select your droplet, this will auto fill the “IP Address” field.

“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

Click “Add Record” button.

Select “CNAME” record type.

In “Enter Name” field type “www“

In “Enter Hostname” field type “example-domain.com.” (with trailing dot in the end).

explaination: This is to properly redirect “www.example-domain.com” to your site using ServerAlias in Apache config.

Crete another Record, this time select “TXT” in record type.

In “Enter Name” field type “@“.

In “Enter Text” field type "v=spf1 mx -all" (with quote)

Crete another Record, this time select “MX” in record type.

In “Enter Hostname” field type “example-domain.com.” (with trailing dot in the end).

In “Enter Priority” field type “0” (zero).

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.

Using PuTTY and SSH to manage your server

Create SSH key using PuTTYgen

Open PuTTYgen app in your computer (you need to install PuTTY first).

Click “Generate” button.

Wave your mouse around in the window area until progress bar complete, this to generate a unique data.

After it’s done, do not close the app yet.

Open your DO Control Panel in browser and click “SSH Keys” link to visit SSH Keys Setting.

Add “SSH Keys” and input key name (something like “my computer” or “my laptop”)

Add “Public SSH Keys” you get from putty.

“Create SSH Keys”, now you’ll get an SSH Keys you can use when creating your Droplet next time.

Back to your PuTTY apps, and save “Public Key” and “Private Key” file (important).

Configure PuTTY session

Open PuTTY app, and you’ll see “PuTTY Configuration” windows.

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”.

Click PuTTY Config sidebar and click “Connection > SSH”, make sure it’s using “2″ as “Preferred SSH protocol version”.

Click “Session” from sidebar to go back to previous screen.

In “Saved Session” field, input profile name, you can save it as “root@example-domain.com”

Click “Save” to save the “profile” so we can re-use this setting in the future.

Click “Open” to start your PuTTY session.

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”

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 is to create a directory

chmod is to set file/folder permission

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 is a terminal text/file editor.  in this command we are editing the file “authorized_keys” using nano.

Next step is to copy your “SSH Public Keys” from PuTTYgen to your clipboard.

Go back to PuTTY terminal, using your mouse, “right-click” to the screen.

This will “paste” your clipboard to PuTTY.

Press “CTRL+x” to exit

Press “y” to save changes.

Press “Enter” to save the file.

Type “reload ssh” command in terminal.

Type “exit” to exit PuTTY session. (this will close the terminal)

How Copy-Paste in PuTTY works

In Windows, we are used to “copy” using “CTRL+c” and using “CTRL+v” to paste. In PuTTY it works differently.

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.

To paste the text, simply use “right-click”/”middle click” of your mouse.

You can also use keyboard “SHIFT+INS” to paste.

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.

Open PuTTY again.

Select your saved profile. example: “root@example-domain.com”

Click “Load”

Navigate to “Connection > Data” and input “root” in “Login details” / “Auto-login username:”, this to always use “root” user when using this saved session.

Navigate to “Connection > SSH > Auth” and  “browse” our “private key” file we saved previously using PuTTYgen.

Navigate back to “Session” and “Save” our profile.

Click “Open” to start session terminal.

You are no longer asked for user name and password.

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

Open FileZilla.

Open “Edit > Setting”

Navigate to “Connection > SFTP”

Click “Add keyfile..” and select your “private key” we get from PuTTYgen.

Click “OK” to save.

Open “File > Site Manager”.

Click “New Site” to create a new profile for our server.

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“.

Click “Connect” to start our session.

You are login to your server using FileZilla and now can browse your server and edit files, upload, etc.

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:

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:

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:

A Basic MySQL Tutorial.

How To Create a New User and Grant Permissions in MySQL.

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:

When using other user, we use:

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:

How To Configure Virtual Memory (Swap File) on a VPS.

How To Add Swap on Ubuntu 12.04.

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:

Ubuntu 12 – Set Up An SVN Server.

Installing/Updating WordPress with Subversion.

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.

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

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.

Change your permalink to test pretty permalink, visit your pages/post and see if pretty permalink is configured/enabled.

Upload image using media uploader to make sure uploading files are working correctly.

Change akismet version to previous version, and check for update, and update it. This test is to make sure WordPress upgrader is working.

Install Contact Form 7 and create form and submit it, this is to check mail functions working.

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.

All mail test should be sent to your email account without going to your mail “spam” folder.

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 : is to set up Virtual Host for port *:80 (accessible via browser)

ServerName : is the domain

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.

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

Now we need to create the folder, navigate using FileZilla to “/var” folder, and create a folder “web”.

Inside this (“web”) folder, create another folder “secondary-domain.com”

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:

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

Go to DNS Setting in your DO Control Panel.

Select / “view” the domain you want.

Click “Add Record” and select “A“.

In “Hostname” field, input the sub-domain, for example: “subdomain“.

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:

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:

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 -

Show more