Recommended VPS Complete list of best VPS hosting providers.

How to Install WordPress on Nginx / LEMP Ubuntu Server

WordPress Nginx Setup

Being a well-known blogging platform, WordPress has been used by hundreds thousands of users and having millions installations. Well, you knew that. Hence, I will not explain more about WordPress. The main objective of this guide is to tell you and many newbies out there on how to install WordPress, step by step with pictures, on an Ubuntu server with full LEMP stack which you have to do it manually since you have no hosting control on it. LEMP or LNMP stack stands for Linux, Nginx, MySQL and PHP. I have explained about how to build a ready-to-use Ubuntu server based on Nginx, MySQL, and PHP7 to host your website on it. I have also explained how to set up Nginx Server Blocks so it can host many websites / domains on it. This article is just a continuation of those previous articles.

REQUIREMENTS

  1. A SSH client like Bitvise or Putty and basic knowledge about how to use it.
  2. Basic knowledge about common Unix command to SSH to your server.
  3. A server or VPS with at least 256MB of RAM (OpenVZ) but 512MB or more is recommended if you are using KVM or Xen.
  4. Ubuntu 16.04 either 32-bit or 64-bit.
  5. About 30 minutes of your time.
  6. a cup of coffee or tea.

Confused on choosing which VPS provider to setup an Apache based hosting server? Read my recommended VPS providers or this list of top low end cloud server providers. Quick recommendation: Digital Ocean, Atlantic.net or Ramnode.

In this guide I am using a 512MB RAM Cloud VPS from Atlantic running Ubuntu 16.04 x64. Unfortunately, this plan is not available any more – as what I can see at their website. However, Atlantic.net provides solid and reliable servers so having the smallest cloud server plan (1GB RAM) is really worth is price and it is very suitable to host your production server.

PREREQUISITES

  1. Provision the Ubuntu server.
  2. Install full LEMP on it. ReadHow to provision a cloud server at Atlantic.net and how to install full LEMP stack on Ubuntu.
  3. Do not forget to always replace domain.tld or servermom.net with your own domain name and replace x.x.x.x with your server IP address.
  4. Setup Nginx Server Block for your domain. ReadHow to add new website and setup Nginx Server Block.
  5. In this tutorial I simply login as root. However, it is advised to login as another user with sudo privilege. Read – Basic Ubuntu Setup to build a production server. Therefore, please add sudo in every and each command.

I’ve explained how to install Nginx, PHP7, and MySQL on my previous post.

Therefore, I assumed at this point you have an Ubuntu server with Nginx, PHP7, and MySQL installed as well as you have created Nginx Server Block for your domain. Let’s get going..

Preparing Database

Step 1 – If you haven’t done it yet, login to your server as root or as a user with root privilege. Make sure you add sudo in front of any command below if you are logging in as sudoer.

Step 2 – Now login to MySQL as root user using password you have defined earlier (see my previous tutorial on full LEMP setup).

mysql -u root -p

Login to MySQL

Step 3 – Create new database by issuing command below.

CREATE DATABASE dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Replace dbname with your own database name. Just make up a name but make sure you remember it or note it down. In this example I use servermomnet as my database name.

Create Database Name

Step 4 – Now create new database user and password while granting it full access to the newly created database. That task can be done simply by issuing command below :

GRANT ALL ON dbname.* TO 'bloguser1'@'localhost' IDENTIFIED BY 'servermom12345';

Replace bloguser1 with your own database username and replace servermom12345 with your own password. Remember that you should not use the same password as the one you have been using as database root password. Example :

Create DB User Grant Access

Step 5 – Finally, refresh database privileges and exit MySQL interface :

FLUSH PRIVILEGES;
exit

Flushing Privileges

Adjusting Nginx Configuration

Step 1 – Edit Nginx Server Block of your website that you have created earlier.

nano /etc/nginx/sites-available/domain.tld

example :

nano /etc/nginx/sites-available/servermom.net

Step 2 – Now put comment mark (#) in front of old configuration line (try_files $uri $uri/ =404;) and add new one below

try_files $uri $uri/ /index.php$is_args$args;

this will make sure that you can make use of WordPress pretty permalink / SEO-friendly permalink.

Adjust Server Block

Step 3 – Next, you may need to add these lines inside that nginx server block file. This is not really necessary but it can tweak your site’s performance and score better in Google Page Speed score.

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }

Eventually, it will look like something like this

inside nginx server block

Do not forget to save changes (Control+O) then exit the editor (Control+X).

Step 4 – Test if nginx configuration you have adjusted is fine and then reload nginx.

nginx -t
service nginx reload

Reload Nginx Configuration

Step 5 – Now install additional PHP modules necessary for WordPress to run properly :

apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc

Remember to add sudo before the command above if you are logged in as sudoer.

Install Additional PHP Modules

The process may take for awhile. Just wait till it finished.

Step 6 – Finally, restart PHP-fpm service

service php7.0-fpm reload

reload phpfpm nginx

Installing WordPress

Step 1 – Go to /tmp folder and download WordPress there :

cd /tmp
curl -O https://wordpress.org/latest.tar.gz

Download WordPress

Step 2 – Extract the newly downloaded WordPress package

tar -xzvf latest.tar.gz

Extract WordPress

Step 3 – Now copy WordPress config sample file and edit it:

cp wordpress/wp-config-sample.php wordpress/wp-config.php
nano wordpress/wp-config.php

Editing WP Config

Step 4 – Now edit the details in MySQL Settings.

Before :

Before Config MYSQL

Put the MySQL database name, MySQL username, and password:

After Editing WP Config

Do not quit Nano editor yet.

Step 5 – Scroll down the page a little bit until you see Salt section. Open up your web browser and open this url :

https://api.wordpress.org/secret-key/1.1/salt/

You’ll get random Salt codes. Copy that :

Salt Key WordPress

Step 6 – Now paste that code in wp-config.php file you are currently editing (delete the existing codes and put the new one):

WP Salt Keys

Do not forget to save changes (Control+O) then exit the editor (Control+X).

Step 7 – Now create new directory / folder called upgrade

mkdir wordpress/wp-content/upgrade

create upgrade folder

Step 8 – Let’s move the whole WordPress files and folders to the web root directory you have created :

cp -a /tmp/wordpress/* /var/www/domain.tld/html
chown -R www-data:www-data /var/www/domain.tld/html

Replace domain.tld with your own. Example

move wp files

Step 9 – Next, issue following command line by line to setup proper file permission:

find /var/www/domain.tld/html -type d -exec chmod g+s {} \;
chmod g+w /var/www/domain.tld/html/wp-content
chmod -R g+w /var/www/domain.tld/html/wp-content/themes
chmod -R g+w /var/www/domain.tld/html/wp-content/plugins

Again, replace all domain.tld parts in those command lines. Example

Setting Up Proper Permission

Step 10 – One thing that I almost forgot, re-edit wp-config.php file.

nano /var/www/domain.tld/html/wp-config.php

Step 11 – Add this line right after define DB_COLLATE (see screenshot):

define('FS_METHOD', 'direct');

Add FS Method Config

This step is necessary to avoid WordPress from asking for FTP credentials when you are doing Plugins or Themes update. Save changes (Control+O) then exit nano editor (Control+X).

Completing Installation

Step 1 – Just open up your web browser again and proceed with regular WordPress install:

Install WP in Browser

Choose the language you want and click the “Continue” button.

Step 2 – Fill in all required details and click the “Install WordPress” button.

Fill WP Details

Step 3 – That’s it. You may now login to your newly installed WordPress blog. Click on that Login button :

Finishing WP Install

Login WP First Time

Congratulation. You have successfully installed WordPress on your Ubuntu server powered with Nginx, PHP7 fpm and MYSQL. This is the basic configuration of a fast webserver. You can still tweak its performance by adjusting nginx configuration like adding either server-based caching or script-based caching (WP Super Cache, W3 Total Cache, etc.).

Now test if you can use Pretty permalink and install new plugin without having to define FTP credentials.

Edit WP Permalink

Success! WordPress SEO / Pretty Permalink works!

Pretty Permalink Success

Now go ahead install new plugin to test if the configuration is ok. You will see something like this indication that you can now install and update any plugin without having to be asked for FTP login details.

Install plugin success

Congratulation and happy blogging.

Most common question : How many visitors this LEMP + WordPress configuration can handle? I will try to do a quick benchmark test in my next post. So, stay tuned. Subscribe to my newsletter or follow me on twitter.

7 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.