How To Install WordPress On A LEMP Configuration

Introduction

In this tutorial, you’ll learn how to install WordPress on a freshly created instance. I’ll demonstrate the installation on an Ubuntu 14.04 server. These instructions may also work on older versions of Ubuntu and Debian.

So, let’s start.

Step One: Update existing packages

apt-get update && apt-get upgrade

Step Two: Install Nginx

Nginx is a high-performance, lightweight web server designed with the purpose of delivering large amounts of static content with efficient use of system resources. In contrast to Apache, Nginx uses an asynchronous event-driven model, which provides more predictable performance under load.

Let’s add a third-party repository to install the latest version of Nginx (1.6.1).

sudo apt-get install python-software-properties
add-apt-repository -y ppa:rtcamp/nginx
sudo apt-get update 
sudo apt-get install nginx
service nginx start

Now, let’s test if the server is up and running.

http://YOUR-VPS-IP

It should take you to Nginx’s default landing page.

Step Three: Install PHP 5.5

PHP is a widely used open-source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Let’s install the latest version of PHP on our server.

sudo add-apt-repository ppa:ondrej/php5
sudo apt-get update
sudo apt-get install php5-common php5-mysqlnd php5-xmlrpc php5-curl php5-gd php5-cli php5-fpm php-pear php5-dev php5-imap php5-mcrypt

If you want to check your PHP version, run the following command:

php -v

You will see something like this.

PHP 5.5.16-1+deb.sury.org~trusty+1 (cli) (built: Aug 25 2014 10:24:59)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
withZendOPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies

Now, we’ll make a slight configuration change to make our setup more secure. Open the main php5-fpm configuration file with root privileges:

sudo nano /etc/php5/fpm/php.ini

Press Ctrl+W and search for cgi.fix_pathinfo=. Uncomment it (delete 😉 and change 1 to 0. After changes, the line should look like this:

cgi.fix_pathinfo=0

Save Ctrl+O and close the file Ctrl+X.

Now, we just need to restart our PHP processor by typing:

sudo service php5-fpm restart

Step Four: Install MySQL

To store and manage databases, we need to install MySQL. You can easily install it by typing the following in the console:

sudo apt-get install mysql-server

During the installation process, you will be asked to set a root password for MySQL. Once you have set the root password, we will have to tell MySQL to generate the directory structure where it will store databases.

sudo mysql_install_db

Let’s finish it up by running a security script that will modify some default insecurities.

sudo mysql_secure_installation

Just type the MySQL root password and type n if you don’t want to change it. After that, type y for every question.

Set Timezone (conditional)

By default, the timezone of your server is UTC. If you’re living in a different timezone, you can change it by typing in the following command:

sudo dpkg-reconfigure tzdata

At this point, your LEMP server is up and running.

Step Five: Configuring Nginx to serve WordPress

Let’s start our WordPress installation by creating an Nginx server block for our site.

sudo nano /etc/nginx/sites-available/wordpress

Paste the following code there:

server {
       listen 80;

       root /var/www/wordpress;
       index index.php index.html index.htm;

       server_name domain.com;

       error_page 404 /404.html;

       error_page 500 502 503 504 /50x.html;
       location = /50x.html {
               root /usr/share/nginx/html;
       } 
location / {
               # try_files $uri $uri/ =404;
               try_files $uri $uri/ /index.php?q=$uri&$args;
       }


       location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.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;
       }

location = /favicon.ico {
       access_log off;
       log_not_found off;
       expires max;
}
location = /robots.txt {
       access_log off;
       log_not_found off;
}

# Cache Static Files For As Long As Possible
location ~*
\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$
{
       access_log off;
       log_not_found off;
       expires max;
}
# Security Settings For Better Privacy Deny Hidden Files
location ~ /\. {
       deny all;
       access_log off;
       log_not_found off;
}
# Return 403 Forbidden For readme.(txt|html) or license.(txt|html)
if ($request_uri ~* "^.+(readme|license)\.(txt|html)$") {
   return 403;
}
# Disallow PHP In Upload Folder
location /wp-content/uploads/ {
       location ~ \.php$ {
               deny all;
       }
}
}

This is a well-tuned WordPress configuration file with permalink support. Save Ctrl+O and close the file Ctrl+X. Let’s enable the server block by symlinking:

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress

Next, we’ll delete the Nginx default server block.

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

Now, we’ll tune the main Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Make sure that the number of worker processes is equal to the number of cores in your instance.

user www-data;
worker_processes 1;
pid /run/nginx.pid;

Add use epoll to the events block.

events {
worker_connections 4096;
multi_accept on;
use epoll;
}

Add client_max_body_size and server_tokens off directive. Set keepalive_timeout to 30 seconds.

        ##
       # Basic Settings
       ##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size  100m;


       # server_names_hash_bucket_size 64;
       # server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

Make sure that the whole Gzip settings block looks like this:

        ##
       # Gzip Settings
       ##

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Save Ctrl+O and close the file Ctrl+X. Then restart the server:

sudo service nginx restart

Step Six: Configure PHP

If you want to upload files of more than 2mb to your WordPress site, you have to increase the PHP upload size variables in php.ini.

sudo nano /etc/php5/fpm/php.ini

Now, press Ctrl+W, search for upload_max_filesize and set it to 100m.

upload_max_filesize=100M

Do the same with post_max_size. post_max_size needs to be the same size or larger than upload_max_filesize.

post_max_size=100M

Restart PHP.

sudo service php5-fpm restart

Step Seven: Setting up the MySQL database

In this step, we’ll create the database user and tables. Go ahead and log into the MySQL shell:

mysql -u root -p

Log in using your MySQL root password. We will need to create a WordPress database, along with a user in the database. First, let’s make the database (feel free to give it whatever name you like):

CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)

After that, we need to create a new user. Please replace the database, name, and password with whatever you prefer:

CREATE USER wordpressuser@localhost;
Query OK, 0 rows affected (0.00 sec)

Set a password for your new user:

SET PASSWORD FOR wordpressuser@localhost= PASSWORD("password");
Query OK, 0 rows affected (0.00 sec)

Finish up by granting all privileges to the new user. Without this command, the WordPress installer will not be able to start up:

GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

Then refresh MySQL:

FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Exit the MySQL shell:

exit

Step Eight: Installing the WordPress files

We’re almost done. Let’s proceed to install WordPress.

First, navigate to the site root directory:

mkdir /var/www/
cd /var/www/

Now, download the latest version of WordPress:

wget http://wordpress.org/latest.tar.gz

Extract it from the archive:

tar -xzvf latest.tar.gz

Give the permissions of /var/www/wordpress to the www-data user. It will allow future automatic updating of WordPress plugins and file editing with SFTP.

sudo chown -R www-data:www-data wordpress/
sudo usermod -a -G www-data www-data

You’re done! Your new WordPress site is now ready. Just navigate to your website and finish the installation.