WordPress on Ubuntu 16.04 and Nginx

1
766

A few days ago I decided to change my bare metal server for a cheaper tariff. The new server has less performance. Therefore, I chose to replace a software base for the web server. In related articles, I found that Nginx should work faster than Apache 2 and use fewer resources. Since Nginx does not contain native PHP processing, I had to install FastCGI process manager (php-fpm).

Below you can find a description of the installation process of Nginx + PHP + MySQL + WordPress on Ubuntu 16.04.

Update Ubuntu 16.04

Update the list of packages which can be updated

sudo -s
apt-get update -y

Install updates and then apply them by restarting the system

apt-get upgrade -y && sudo shutdown -r now

Nginx

I want to deploy my applications and WordPress based on the LEMP stack.
I should install Nginx

apt-get install Nginx -y

check the status of Nginx

systemctl status nginx

As we can see the service is active and works

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-05-06 17:44:28 UTC; 2min 11s ago
 Main PID: 1110 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1110 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─1111 nginx: worker process
           └─1112 nginx: worker process

We also can check it by direct access to the web service

Install MySQL

WordPress uses Mysql

apt-get install mysql-server -y

I was asked to supply a root password for using within the MySQL system. And then I used the standard script, to modify some insecure defaults. During this script, I removed anonymous users, disallowed root login remotely, removed test database and reloaded privilege tables.

mysql_secure_installation

At this point, your database system is now set up, and we can move on.

Install and configure PHP 7.0

Since Ubuntu 16.04, PHP 7.0 has replaced PHP 5.x and become the default version of PHP in the official Ubuntu application repository. Even so, you need some manipulation to install the last stable version of PHP: 7.2. You probably need to install common PHP modules for WordPress too.

You need to add specified PPA to the system apt repository by Ondřej Surý

apt-get install python-software-properties

add the PHP repository

add-apt-repository ppa:ondrej/php
apt-get update

and install PHP 7.2 and modules

apt-get install php7.2 php7.2-fpm php7.2-cli php7.2-common php7.2-mbstring php7.2-gd php7.2-intl php7.2-xml php7.2-mysql php7.2-zip php7.2-curl php7.2-xmlrpc php7.2-zip -y

Check the PHP version

php -v

and restart php7.0-fpm, just to be sure that all modules included

sudo systemctl restart php7.2-fpm

the next step is PHP configuration
The main config file of PHP 7.2 was saved as /etc/php/7.2/fpm/php.ini

vim /etc/php/7.2/fpm/php.ini

Change the insecure setting to

cgi.fix_pathinfo=0

and restart the service and Nginx

systemctl restart nginx.service php7.2-fpm.service

Create virtual host catalogs

I prefer that my websites are in /var/www/my_syte_name and contain http and logs folders

mkdir /var/www/blog_galkinc_ru
mkdir /var/www/blog_galkinc_ru/html
mkdir /var/www/blog_galkinc_ru/logs

Configure Nginx to handle WordPress

My blog is the default host for the server, so I use appropriated block of the configuration

vim /etc/nginx/sites-available/default

delete any unuseful comments, add your configuration and make something like this

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/blog_galkinc_ru/html;
        error_log /var/www/blog_galkinc_ru/logs/error_log.log error;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name blog.galkinc.ru galkinc.ru;
        location / {
                #permalinks
                try_files $uri $uri/ /index.php?$args;
                #try_files $uri $uri/ =404;
        }
        #enable gzip
        gzip on;
        gzip_disable "msie6";
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

        location ~ \.php$ {
                #try_files $uri = 404;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
        #For upload scripts
        location ~* /(?:uploads|files)/.*\.php$ {
                deny all;
        }
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|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; # static cashing
        }
}

Test the configuration

sudo nginx -t

Reload Nginx to make the necessary changes

systemctl reload nginx

Create info.php to check PHP by your web URL

<?php 
phpinfo();


Don’t forget to remove the info.php file

rm -r info.php

Install WordPress

Download the latest WordPress package and extract

cd /var/www/blog_galkinc_ru/html
sudo wget -c http://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz

Move the WordPress files from the extracted folder and delete the archive

mv wordpress/* ./
rm wordpress/ -R
rm latest.tar.gz -R

Make the right permissions

sudo chown -R www-data:www-data /var/www/blog_galkinc_ru/html/
sudo chmod -R 755 /var/www/blog_galkinc_ru/html/

Create WordPress Database

mysql -u root -p 
CREATE DATABASE wp_blog;
GRANT ALL PRIVILEGES ON wp_blog.* TO 'your_username_here'@'localhost' IDENTIFIED BY 'your_chosen_password_here';
FLUSH PRIVILEGES;
EXIT;

Configure WordPress

Go the /var/www/html/ directory and rename existing wp-config-sample.php to wp-config.php:

sudo mv wp-config-sample.php wp-config.php

Open WordPress configuration file

sudo vim wp-config.php

and update it with your database information under the MySQL settings section

// ** MySQL settings - You can get this info from your web host ** // 
/** The name of the database for WordPress */ 
define('DB_NAME', 'wp_blog'); 

/** MySQL database username */ 
define('DB_USER', 'your_username_here'); 

/** MySQL database password */ 
define('DB_PASSWORD', 'your_chosen_password_here'); 

/** MySQL hostname */ 
define('DB_HOST', 'localhost'); 

/** Database Charset to use in creating database tables. */ 
define('DB_CHARSET', 'utf8'); 

/** The Database Collate type. Don't change this if in doubt. */ 
define('DB_COLLATE', '');

After that, open your browser and browse to your domain name to launch WordPress configuration wizard.

You’ve successfully installed WordPress and can now enjoy WordPress on Ubuntu with NGinx.
It’s time to configure your blog, install a backup system/module and system monitor.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

1 COMMENT

  1. […] In the previous post, I installed WordPress on Nginx. Then I configured the site backup module and noted that WordPress uses a file called wp-cron.php as a virtual cron-job to automate things like checking for plugin or theme updates, sending email notifications and more. I also found Xclonner Team’s recommendation to disable wp-cron configuration in WordPress and setting up wp-cron.php to run manually. […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here