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 techarena51.com with yoursite.conf

    server {
        listen       *:80;
        server_name  techarena51.com  www.techarena51.com;

       # 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 {
            internal;
        }

        # 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|
        #gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|
        #mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|
        #ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|
        #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/techarena51.com-error.log;
        access_log /var/log/nginx/techarena51.com-access.log;

        root   /home/wordpress-user/techarena51.com;


        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_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /home/wordpress-user/techarena51.com$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'.
 [wordpress]

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

 ;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://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
 sudo add-apt-repository 'deb [arch=amd64,i386] http://mirror.fibergrid.in/mariadbrepo/10.1/ubuntu 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 DATABASE wordpress;
 CREATE USER wordpress-user@localhost IDENTIFIED BY 'password';
 GRANT ALL PRIVILEGES ON wordpress.* TO wordpress-user@localhost;
 FLUSH PRIVILEGES;
 exit

Finally Download and Install Wordpress

 cd /tmp
 wget http://wordpress.org/latest.tar.gz
 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 yoursite.com.

To Install a SSL Certificate for your Wordpress Blog from Let's Encrypt see http://techarena51.com/index.php/installing-and-securing-nginx-websites-on-linux-with-a-free-ssl-certificate-from-lets-encrypt/?utm_source=wordpress-lemp-github&utm_medium=github&utm_campaign=devops-wiki