Setting up Wordpress and NGINX in your VPS

Wordpress is the most famous blogging platform on the web. It's written in PHP, a well-stabilished and ever-evolving language and uses MySQL as a database to store its posts and configuration.

New webpage administrators tend to use it as it has a lot of available themes, plugins and resources.

If you're on a shared hosting, the web environment is usually ready and you only have to install wordpress and, sometimes, it is even already installed for you.

When you run your own VPS (Virtual Private Server), you need to install and setup everything yourself. Apache is usually the go-to web server for most wordpress installations, but nginx is a competitor server that is gaining ground because of its simplicity, speed and low need of resources, and that is what we'll be using in this tutorial.

Note: If you're using debian or ubuntu, you'll have the apt-get command available for you to install packages. These commands works just as well with other distros with rpm, aptitute or yum. If you're not issuing these commands as root, be sure to prefix the commands with sudo.

Note 2: The # character prefixing the commands are for the user to notice that these commands must be run as root (or with sudo). Alternatively, $ denotes the regular user prompt.

Installing MySQL

MySQL (also known as MariaDB) is the world most famous SQL Database server (are you noticing a pattern?). It is also simple to install and we will not need to know much information about how to handle it, since WordPress will be doing the heavy-lifting for us.

To install, simply:

# apt-get install mysql-server

During the install, the server will ask for a root password. Choose one and take note. It will be needed later.

Installing PHP

Just as simple as the MySQL database:

# apt-get install php5-mysql php5-fpm

Note that we're installing two packages. One to be able to connect to the MySQL server trough PHP, other to handle processes sent by nginx.

The php5-fpm package comes installed nicely on debian. No configuration needed.

Installing NGINX

Nginx is also very simple to install

# apt-get install nginx

Just like that, nginx is installed and ready to run on your server. Now we need to add a server and send the php requests to php5-fpm.

Assuming that this is the only server that will be available on your VPS, it's time to change our default configuration. Edit the configuration file located at /etc/nginx/sites-available/default and let it be with the following content (lines starting with # are comments in this case):

server {
    # Listen on ipv4
    listen 80 default_server;
    # Listen on ipv6
    listen [::]:80 default_server;

    root /var/www/html;

    # Remember to add index.php first on this list.
    index index.php index.html index.htm;

    # We will respond to every server name pointing here.
    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FPM server listening on /var/run/php5-fpm.sock
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Once you're done, check if the configuration is valid with nginx -t, and if all is well, restart the server.

# service nginx restart

Installing WordPress

First we need to change to the www directory and download wordpress there. The download will create a wordpress folder, which is not what we want -- we want the folder named html, so we delete the latter and rename the downloaded folder properly.

# cd /var/www
# curl -L http://wordpress.org/latest.tar.gz | tar zx -
# rm -Rf html && mv wordpress html

Now, we need to create a database for wordpress to store data into. Remember the mysql root password? We're going to need it now. Lot in mysql and create the user and database with the following commands:

# mysql -u root -p
(mysql will ask for your password)
mysql> CREATE DATABASE wordpress;
mysql> CREATE USER [email protected] IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO [email protected] IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
mysql> exit;

Our database is created! Now we just need to finish wordpress configuration. First, copy the sample config and edit the file

# cd /var/www/html
# cp wp-config-sample.php wp-config.php
# nano wp-config.php

You need to change the apropriate values defined by DB_NAME, DB_USER and DB_PASSWORD. They look like this:

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

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

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

And you're good to go!

Tue, June 21st, 2016