← Back to Blog Home

  •  Wednesday, October 24, 2012   
  •   Comments

Welcome to part 2 of our WordPress Hosting Guide, in the last part we delved into the various ways that you can find a web host to suit your individual needs. Assuming you’ve already done that, we’re now going to get our server setup so that it’s primed for our WordPress install.

The Setup

In this guide we’re going to setup WordPress on Ubuntu 12.04 LTS 64-bit using Nginx, PHP-FPM, Percona Server for MYSQL, Memcache (Which WP Super Cache will be using as the data store) & MaxCDN to serve static assets.

The actual installation of WordPress itself will be in the next part, here we’ll purely be looking at how to install everything we need on the server. We’re using the WSWD SSD VPS 1, you can do this on any server with SSH access.

Logging Into Your Server

Firstly you’ll need to SSH into the server, to do this you can use Terminal on OSX or Putty if you’re on Windows.

ssh root@serveripaddress

You’ll then be prompted for your root password, which you should have received (or set) when you signed up to your VPS plan.

When you log in successfully you’ll see the shell, which looks something like this:

root@serverbear:~# 

You’ll notice that the hostname is already serverbear if you want to change this you can use the command:

echo "serverbear" > /etc/hostname
hostname -F /etc/hostname

Simply replace serverbear with whatever you want to set your server hostname as. You can use the command hostname afterward to check it’s set properly.

Updating Ubuntu & Packages

The first thing we’re going to to is get Ubuntu up to date:

apt-get update
apt-get upgrade

This will update & install packages, you may be prompted to select Y at some point during this step.

Security

This step isn’t totally necessary, but you run the risk of letting someone run rampant on your server should they find or guess your root password. So we’ll so a few things just to lock the box down a bit before setting up the necessary services to run WordPress.

Configure Firewall

Configuring the firewall just enables us to control which services are listening by default, instead of everything.

ufw allow ssh
ufw allow http
ufw logging off
ufw enable

If you wanted to go a step further you can install fail2ban to prevent any brute force type attacks on your passwords.

Creating a Non-Root User

First lets create a new user that isn’t root

adduser serverbear

You can replace serverbear with whatever you want to call your user. We will also need to give this user the ability to execute commands via sudo (which will allow you to run commands as root if needed)

usermod -a -G sudo serverbear

Again just replace serverbear with your chosen username. Once completed you’ll need to log out, close the window or type exit. Then we’ll login with the user we’ve created:

ssh serverbear@serveripaddress

You can also secure your VPS further by setting up login via SSH keys only, something we’ll leave for another guide.

Setting Up & Configuring MYSQL

We’re going to use the Percona version of MYSQL for this tutorial, Percona has more flexibility, stability & is able to handle more transaction volume – improving general scalability.

There’s a few commands we need to first first:

gpg --keyserver  hkp://keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A
gpg -a --export CD2EFD2A | sudo apt-key add -
sudo sh -c 'echo "deb http://repo.percona.com/apt precise main" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb-src http://repo.percona.com/apt precise main" >> /etc/apt/sources.list'

Then do a local update (if you don’t do this the installation will fail below):

sudo apt-get update

Now we can install the Percona client & server packages:

sudo apt-get install percona-server-server-5.5 percona-server-client-5.5

Keep a note of your root password, as you’ll need this to get into MYSQL. You will also have access to a few extra commands to start/stop/restart MYSQL when needed:

service mysql start
service mysql stop
service mysql restart

Setting Up MYSQL User

You have to login as root to MYSQL to set up your first user/database:

mysql -u root -p

Here we’re going to create a database called wordpress, with a user called wp_user, you can choose your own password by replacing the password field.

CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO "wp_user"@"localhost" IDENTIFIED BY "PASSWORD";
FLUSH PRIVILEGES;
EXIT

Installing Nginx, PHP-FPM & Memcache

Nginx is pretty easy to install:

sudo apt-get install nginx

Then just start it up with the same syntax as we learned before:

sudo service nginx start

You can also check your current version:

nginx -v
nginx version: nginx/1.1.19

Now we’re going to install the rest of the dependancies for PHP-FPM + Memcache too.

sudo apt-get install php5-cli php5-common php5-mysql php5-suhosin php5-gd php5-dev php5-fpm php5-cgi php5-memcache

Now just check that PHP is installed correctly:

php5-fpm -v
PHP 5.3.10-1ubuntu3.4 (fpm-fcgi) (built: Sep 12 2012 19:03:32)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with Suhosin v0.9.33, Copyright (c) 2007-2012, by SektionEins GmbH

We’ll also make sure that Nginx, PHP5-FPM, MYSQL & Percona all start on boot (incase of an emergency reboot)

sudo apt-get install chkconfig
chkconfig --add nginx
chkconfig --add php5-fpm
chkconfig --add mysql
chkconfig --add memcached

Creating Directories

We’re going to have all our sites sitting in the /home/ directory. For the purpose of this guide we’ll put this installation in /home/wordpress (but you can call the directory whatever you like).

sudo mkdir -p /home/wordpress/
sudo chown www-data:www-data /home/wordpress/
sudo chmod 755 /home/wordpress/

Configuring Nginx

Now that we know what directory we’re installing our WordPress site we need to configure Nginx. We’ll use the nano text editor, as this is normally easier to grasp than vi.

sudo apt-get install nano

You can call the site whatever you like, here we’ve called it wordpress, but equally it could be serverbear.com or wordpress.com or something that uniquely identifies the configuration for that site.

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/wordpress

The next step is the config, copy & paste it from below into the window. You’ll need to change a few bits:

  1. server_name: Change this to the URL of your own server. If you want the blog on a subdomain you can do blog.yourserver.com
  2. root: This must be the directory we created earlier. If you named it something else reflect that here.
  3. fastcgi_param: The URL here must also reflect the same directory. Otherwise PHP won’t work correctly.
server{
        listen 80;
        server_name yourserver.com;
        root /home/wordpress;

        location / {
          gzip on;
          gzip_http_version 1.0;
          gzip_vary on;
          gzip_comp_level 3;
          gzip_proxied any;
          gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
          gzip_buffers 16 8k;
          index index.php;

          # this serves static files that exist without running other rewrite tests
          if (-f $request_filename) {
              expires 30d;
              break;
          }

               set $supercache_file '';
               set $supercache_uri $request_uri;

               if ($request_method = POST) {
                       set $supercache_uri '';
               }

               if ($query_string) {
                       set $supercache_uri '';
               }

               if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
                       set $supercache_uri '';
               }

               if ($supercache_uri ~ ^(.+)$) {
                       set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
               }

               if (-f $document_root$supercache_file) {
                       rewrite ^(.*)$ $supercache_file break;
               }

          # this sends all non-existing file or directory requests to index.php
          if (!-e $request_filename) {
              rewrite ^(.+)$ /index.php?q=$1 last;
          }
        }

        location ~ \.php$ {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /home/wordpress/$fastcgi_script_name;
            include         fastcgi_params;

        }
}

Use CTRL + X to quit then Y to save. When this is done you’ll need to restart Nginx with the service nginx restart command. If everything goes well you should get no errors.

Now we’re all ready to install WordPress.