Mar142016

How to deploy a WordPress blog into DigitalOcean hosting

Introduction

After many years suffering a lot of server unavailability and very slow page loading of our 1and1 server, we decided to migrate to another low cost provider. not looking for best specifications but looking for best stability at similar price.

We decide to use DigitalOcean as provider because it is very easy to use and also very cheap. It has plans staring from 5 € per month and guarantee some minimum basic resources, enough for our purposes, also you can always increase or decrease your server specifications at any moment very easily, because the are cloud servers.

Moreover you will only pay for the hours you actually used the server, so you can shutdown your server if you don’t need it and you will not pay for it. Also you can increase your server specifications during a while, then return to your original plan and will only pay the superior plan price for the hours you used it.

Creating your first “droplet”

You have to sign up in DigitalOcean, also you can get profit of their referral program to get 10$ in order to do your test for free. The DigitalOcean referral program allow current clients sharing a special URL that will allow new clients to get 10$ for free and also, when this new client spent 25$, DigitalOcean gives to the URL’s owner another 25$. So both parts win.

If you want to follow this guide without spent money, you can use our referral URL: https://www.digitalocean.com/?refcode=a5dafd7b12aa.

Once you are signed up, it is time to create your droplet, where your web page will be hosted, following this steps:

Step 1: Name and plan selection


Droplet_1

Step 2: Region y other settings

Droplet_2

 

 

Step 3: Preinstalled software

droplet_lemp16-04

Last step: SSH key

Droplet_5

DNS server

Now you need to link our server to our domain, you will not need to make any domain transfer, just go to DNS tab, write your domain name and select the droplet you want to use for this domain. After that, you can see what are your assigned DNS servers and set it on your domain control panel.

DigitalOcean DNS configuration example

Droplet_6

Setting up your Droplet

First of all, is important to improve your server security, otherwise you will be hacked in a few minutes, so start a new SSH session:

$ ssh root@SERVER_IP

As you added a SSH key, yuo will not be answered for password, but is very important setting a new as hard as possible root password:

# passwd

Now is time to create your user:

# adduser USER
# gpasswd -a USER sudo
# usermod -a -G www-data USER

Exit an log in again using your new user:

# exit
$ ssh USER@SERVER_IP

Copy your SHH key to your new user’s home in order to access without password and to improve security.

$ mkdir .ssh
$ chmod 700 .ssh
$ sudo cp /root/.ssh/authorized_keys .ssh/
$ sudo chown USER:USER .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

Before anything else, we temporary disable the firewall , to avoid lost connection and control during the setup.

$ sudo ufw disable

Now you can do some tweak to the SSH server, to start, open configuration file:

# nano /etc/ssh/sshd_config

Change the default port:

Port 2234

Disable SSH root login and password login, allowing only SSH key login.

PermitRootLogin no
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

Save and exit perrsing CTRL-X, then Y, and finally ENTER.

Enable the firewall and open necessary ports. First SSH port:

$ sudo ufw delete limit 22
$ sudo ufw allow 2234/tcp

And then HTTP and SSL/TLS ports:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp

If you want to have SMTP server, you can open port 25, for example (I do not recommend):

$ sudo ufw allow 25/tcp

Now you can start the firewall:

$ sudo ufw enable

Restart SSH server:

# service ssh restart

Log out and log in again using the new port:

# exit
$ ssh USUARIO@IP_DEL_SERVIDOR -p 2234

The next step is configuring the time zone and enabling NTP:

$ sudo dpkg-reconfigure tzdata
$ sudo apt-get update
$ sudo apt-get install ntp

To improve a little bit the server performance wen memory ram gets full, you can create a SWAP file:

$ sudo fallocate -l 512M /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
$ sudo sh -c 'echo "/swapfile none swap sw 0 0" >> /etc/fstab'

Setting up your Web server

At this point, you have an acceptable configured server, so you can install and configure your web server.

Start creating needed directory structure:

$ sudo mkdir -p /var/www/EXAMPLE.COM/html
$ sudo chown -R www-data:www-data /var/www/EXAMPLE.COM/html
$ sudo chmod -R 775 /var/www

Add your domain to Nginx and configure it:

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/EXAMPLE.COM
$ sudo nano /etc/nginx/sites-available/EXAMPLE.COM

Modify your configuration file to be similar to this:

# Cache
#fastcgi_cache_path /var/cache/nginx/EJEMPLO.COM levels=1:2 keys_zone=EJEMPLO.COM:10m max_size=128m inactive=1h;

#server {
# server_name "~^www\.(.*)$" ;
# return 301 $scheme://$1$request_uri ;
#}

server {
 listen 80;
 listen [::]:80;

 root /var/www/EJEMPLO.COM/html;
 index index.php;

 server_name EJEMPLO.COM www.EJEMPLO.COM;

 location / {
 try_files $uri $uri/ /index.php?q=$uri&$args;
 }

 location ~ /\.ht {
 deny all;
 }
 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
 #fastcgi_cache EJEMPLO.COM;
 #fastcgi_cache_key $scheme$host$request_uri$request_method;
 #fastcgi_cache_valid 200 301 302 5s;
 #fastcgi_cache_use_stale updating error timeout invalid_header http_500;
 }

gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
 expires 1w;
 add_header Pragma public;
 add_header Cache-Control "public";
 try_files $uri =404;
}

}

Redirect www subdomain (optional)

Probably, you will want that visitors that access through www subdomain be redirected to the main domain, if this is your case comment out green lines and remove orange lines.

Enable cache (optional)

Enabling cache, you can improve pages load speed adn accept more simultaneous visits. To do it comment out blue lines and create the proper folder to store cache files:

$ sudo mkdir /var/cache/nginx
$ sudo chown -R www-data:root /var/cache/nginx

Enabling your new site

$ sudo ln -s /etc/nginx/sites-available/EXAMPLE.COM /etc/nginx/sites-enabled/

Setting up MySQL server

Use this command to get the message of the day, where you can find the MySQL root password:

$ sudo cat /root/.digitalocean_password

Now that we know the default MySQL password, we proceed to execute the basic configuration wizard and setup some parameters:

$ mysql_secure_installation

The wizard will ask us some questions that can be answered as follows:

[...]
Enter password for user root: TYPE HERE YOUR NEW MYSQL PASSWORD
VALIDATE PASSWORD PLUGIN can be used to test passwords
[...]
Press y|Y for Yes, any other key for No: N
[...]
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
[...]
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
[...]
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
[...]
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
[...]
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

Log in to your MySQL server:

$ mysql -u root -p

Create the database and user for “WordPress”:

CREATE DATABASE DB_NAME;
CREATE USER WORDPRESS_DB_USER@localhost IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON DB_NAME.* TO WORDPRESS_DB_USER@localhost;
FLUSH PRIVILEGES;
exit

Installing WordPress

From this point, you only need to do a normal WordPress installation. Start downloading the last WordPress version and extracting tar file:

$ cd /var/www/EXAMPLE.COM/html/
$ wget http://wordpress.org/latest.tar.gz
$ tar xzvf latest.tar.gz
$ mv wordpress/* .
$ rm -rf wordpress

Ensure that you have the proper dependencies installed:

$ sudo apt-get install php5-gd libssh2-php

Create and edit the configuration file:

$ cp wp-config-sample.php wp-config.php
$ nano wp-config.php

Set your database settings, save and close the file:

. . .
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'DB_NAME');

/** MySQL database username */
define('DB_USER', 'WORDPRESS_DB_USER');

/** MySQL database password */
define('DB_PASSWORD', 'PASSWORD');
. . .

And finally ensure that files has correct permissions:

$ sudo chown www-data:www-data -R *
$ sudo find . -type d -exec chmod 775 {} \;
$ sudo find . -type f -exec chmod 664 {} \;

¡Done!

Política de comentarios

Dada la importancia de los comentarios como espacio de participación, te pedimos por favor que leas detenidamente y cumplas con las siguientes normas de participación.

9 respuestas para “How to deploy a WordPress blog into DigitalOcean hosting

Jamil Ahmed

Hello Elias, You did an excellent post on how to deploy a WordPress blog into DigitalOcean hosting. DigitalOcean’s cloud infrastructure is one of the most reliable in the industry. Websites hosted on DigitalOcean rarely go down. Through the Cloudways Cloud Platform, you get fully managed DigitalOcean cloud infrastructure.


Elías R.M.

Thank you Jamil, I’ll take a look to Cloudways.

Regards


Jamil Ahmed

Welcome Elias 🙂


Shivani

Very informative article. Overall a nice guide but it is somewhat complicated for beginners.
Thanks for sharing


Elías R.M.

Thank you very much Shivani.

I am working on a simpler version, probably using scripts that simplify the process.

Regards


Imam

I also have some blogs hosted at GoDaddy. My one blog is having massive traffic so facing downtime with GoDaddy shared plan. Even I migrated VPS plan of GoDaddy then also was facing downtime and slow loading with GoDaddy so I cancel their VPS plan and re-started using shared. Now I am looking an alternative GoDaddy, Some people were saying that Digitalocean is good but configuring the things there is very tough. Here after reading your article now I found that it is very easy to use Digital ocean. You have explained the things with very perfection. Your article is very helpful.


Elías R.M.

I’m glad to hear that. Thank you very much Imam.

I also remember that this article is a little bit old and some things are not valid now, so I’ll update it, as I already did with the spanish version.

Regards


Gaurav

Hi, a Useful article on WordPress hosting. Thanks for sharing.


Elías R.M.

You are very welcome Gaurav

Cheers


Leave a Reply

Your email address will not be published.