How to Install Wordpress with Nginx, PHP FPM and MariaDB on Ubuntu 14.04 (LEMP)

Leo-G edited this page Jun 16, 2016 · 14 revisions

First install and configure Nginx

  sudo apt-get update
  sudo apt-get install nginx

Add the below to /etc/nginx/sites-enabled/yoursite.conf, replacing with yoursite.conf

    server {
        listen       *:80;

       # Define default caching of 24h
        expires 86400s;
        add_header Pragma public;
        add_header Cache-Control "max-age=86400, public, must-revalidate, proxy-revalidate";

        # deliver a static 404
        error_page 404 /404.html;
        location  /404.html {

        # Deliver 404 instead of 403 "Forbidden"
        error_page 403 = 404;

        # Do not allow access to files giving away your WordPress version
        location ~ /(\.|wp-config.php|readme.html|licence.txt) {
            return 404;

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        # Don't log robots.txt requests
        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;

        # Rewrite for versioned CSS+JS via filemtime
        location ~* ^.+\.(css|js)$ {
            rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last;
            expires 31536000s;
            access_log off;
            log_not_found off;
            add_header Pragma public;
            add_header Cache-Control "max-age=31536000, public";

        # Aggressive caching for static files
        # If you alter static files often, please use
        # add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
        #location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|
        #wri|xla|xls|xlsx|xlt|xlw|zip)$ {
         #   expires 31536000s;
          #  access_log off;
           # log_not_found off;
            #add_header Pragma public;
            #add_header Cache-Control "max-age=31536000, public";
        error_log  /var/log/nginx/;
        access_log /var/log/nginx/;

        root   /home/wordpress-user/;

        location / {
            index  index.php index.html index.htm;

       # get friendly url links working
       if (!-e $request_filename)
            rewrite ^(.+)$ /index.php?q=$1 last;

        # pass all PHP files through php-fpm
        location ~ \.php$ {

    try_files $uri =404;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /home/wordpress-user/$fastcgi_script_name;
            include        /etc/nginx/fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_ignore_client_abort off;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;

        # deny all apache .htaccess or .htpasswd files
        location ~ /\.ht
            deny all;
    # Deny access to hidden files
        location ~ /\. {
            deny all;
            access_log off;
            log_not_found off;

Restart Nginx

sudo service nginx restart

Next Install PHP, PHP-FPM, PHP-GN and PHP MySQL drivers. PHP-GD is for image cropping and rotating to work.

 sudo  apt-get install php5-fpm php5 php5-mysql php5-gd

Configure PHP-FPM to serve your website

 cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/yoursite.conf

Add a user for your site and Modify the below parameters in /etc/php5/fpm/pool.d/yoursite.conf

 useradd -s /sbin/nologin wordpress-user
 vim /etc/php5/fpm/pool.d/yoursite.conf

 ; Add a pool name to identify your site'.

 ;Modify the port for PHP-FPM to listen on
 listen =

 ;Add the user we created earlier
 user = wordpress-user
 group = wordpress-user

Restart PHP-FPM

 sudo service php-fpm restart

Install MariaDB by adding it's repository

 sudo apt-get install software-properties-common
 sudo apt-key adv --recv-keys --keyserver hkp:// 0xcbcb082a1bb943db
 sudo add-apt-repository 'deb [arch=amd64,i386] trusty main'
 sudo apt-get update
 sudo apt-get install mariadb-server

It will prompt you to set a root password. Once set login and create a wordpress user and database.

 mysql -u root -p
 CREATE USER wordpress-user@localhost IDENTIFIED BY 'password';
 GRANT ALL PRIVILEGES ON wordpress.* TO wordpress-user@localhost;

Finally Download and Install Wordpress

 cd /tmp
 tar -xzvf latest.tar.gz
 sudo mv wordpress /home/wordpress-user/
 sudo chown -R wordpress-user:wordpress-user /home/wordpress-user/wordpress

You should be able to view the wordpress installation page on

To Install a SSL Certificate for your Wordpress Blog from Let's Encrypt see