How to install WordPress on an Ubuntu 16.04 VPS with Nginx

How to install WordPress on an Ubuntu 16.04 VPS with Nginx

Everyone loves, uses, or has heard of WordPress. It’s the most popular CMS out there, and with the advantages of VPS hosting over Shared hosting, a lot of users are moving to a VPS. You may need to install WordPress yourself on your server, so this tutorial will come in handy. Though, some managed hosting providers like RoseHosting offer free WordPress installation and optimization.

In this tutorial, we are going to provide you with step-by-step instructions on how to install WordPress on an Ubuntu 16.04 VPS.
WordPress is the most popular open source CMS written in PHP that allows web developers to create everything from simple websites to complex e-commerce web stores, news portals, magazines etc.
This tutorial was tested and written for an Ubuntu VPS, but it should work on any Debian based Linux distribution.

At the time of writing this tutorial, the latest stable version of WordPress is 4.7.1 and we recommend to install and use:

  • PHP version 7 or higher with the GD graphics library version 2.0.x+ and MySQLi PHP extensions enabled, with memory_limit PHP variable set to 64 MB or higher (128 MB or higher is preferred)
  • Apache or nginx web server;
  • MySQL version 5.6 or higher or MariaDB version 10.0 or higher .

Let’s start with the installation.

Update OS packages:

Make sure your package list and the OS packages are up to date by running the following commands:

sudo apt-get update
sudo apt-get upgrade

To install the latest nginx version on your virtual server from the official nginx repository, edit the ‘/etc/apt/sources.list’ file:

sudo vi /etc/apt/sources.list

Add the following lines to it:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

Add the nginx GPG key:

wget http://nginx.org/packages/keys/nginx_signing.key
cat nginx_signing.key | apt-key add -

Stop the Apache service and remove it:

sudo service apache2 stop
sudo apt-get remove apache2

Install nginx on your virtual server using the following commands:

sudo apt-get update
sudo apt-get install nginx

Configure the nginx service to automatically start on boot:

sudo /lib/systemd/systemd-sysv-install enable nginx

Install PHP and enable required PHP modules:

sudo apt-get install php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-imap php7.0-json php7.0-mbstring php7.0-mysql php7.0-opcache php7.0-pspell php7.0-readline php7.0-xml php7.0-mcrypt php7.0-zip
sudo phpenmod mcrypt
sudo phpenmod imap

Then, start with the WordPress installation procedure.

Download WordPress

Get the latest version of WordPress available at their official website. Download it to a directory of your virtual server and extract it using the following commands:

cd /opt/
wget https://wordpress.org/latest.zip
unzip latest.zip -d /var/www/html/

Create a new nginx configuration file and add the following virtual block for your domain name:

vi /etc/nginx/sites-available/your-domain.com.conf

Add the following lines:

server {
listen 80;
server_name your-domain.com www.your-domain.com;

root /var/www/html/wordpress;
index index.php index.html;
access_log /var/log/nginx/your-domain.com-access.log;
error_log /var/log/nginx/your-domain.com-error.log;
charset en_us.UTF-8;

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

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Then, edit the main nginx configuration file (/etc/nginx/nginx.conf) and add the following line before the end of the http block:

include /etc/nginx/sites-enabled/*.conf

Activate the new nginx block by creating a symbolic link between the sites-available directory and the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/your-domain.com.conf /etc/nginx/sites-enabled/your-domain.com.conf

Do not forget to replace your-domain.com with your actual domain name. Then, delete the ‘default’ nginx configuration file:

rm /etc/nginx/sites-enabled/default

Open the ‘/etc/php/7.0/fpm/pool.d/www.conf’ PHP configuration file and change the ‘listen’ variable:

change:

listen = /run/php/php7.0-fpm.sock

to

listen = 127.0.0.1:9000

Edit the ë/etc/php/7.0/fpm/php.inií PHP configuration file:

vi /etc/php/7.0/fpm/php.ini

Add/modify the following settings:

max_execution_time = 300
max_input_time = 300
memory_limit = -1
post_max_size = 64M
upload_max_filesize = 64M

The web server user (www-data) needs to be able to write to files and directories inside the ‘/var/www/html/wordpress’ directory, so it can easily be accomplished by executing the following command:

sudo chown www-data:www-data -R /var/www/html/wordpress/

Test the nginx configuration:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the test is successful, restart php7.0-fpm and Nginx services for the changes to take effect:

sudo service php7.0-fpm restart
sudo service nginx restart

Create new MySQL user and database:

WordPress requires a database to work as this is where data is saved, so create a new MySQL database on your server:

mysql -u root -p
mysql> SET GLOBAL sql_mode='';
mysql> CREATE DATABASE wpdb;
mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'y0ur-pAssW0RD';
mysql> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

Copy the WordPress sample config file (wp-config-sample.php):

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

Set up the MySQL database information:

vi wp-config.php
define('DB_NAME', 'wpdb');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'y0ur-pAssW0RD');

Do not forget to replace ‘y0ur-pAssW0RD’ with a strong password.

To install WordPress, open http://your-domain.com using a web browser and follow the easy instructions: select a language, create an administrator user account, enter your email address and click ‘Install WordPress’.

That is it. The WordPress installation is now complete. You can now start adding some plugins like W3 Total Cache to improve your website’s speed and performance.


As we mentioned before, you don’t have to do any server work if you get Managed VPS Hosting from RoseHosting. They will install, configure and optimize WordPress if you get WordPress hosting from them.

Leave a Reply