Installing WordPress and Nginx

For an example of a completed installation of WordPress with Nginx in the operating system Ubuntu Server 18.04.

Switch to the root user:

sudo -i

Install nginx, php-fpm and other necessary components:

apt install nginx php-curl php-gd php-xml php-xmlrpc php-intl php-mbstring php-soap php-zip php-mysql php-fpm php-json php7.2-opcache php7.2-mbst

Install the database server:

apt install mysql-server mysql-client

Connect to the database server:

mysql -u root -p

Create a database and user:

CREATE DATABASE ixnfo;
CREATE USER test@localhost IDENTIFIED BY 'test';
GRANT ALL PRIVILEGES ON ixnfo.* TO test@localhost;
FLUSH PRIVILEGES;
exit

Download the archive with the latest version of WordPress from the official site, unzip it, create a configuration file and open it in a text editor (for example nano):

cd ~
wget https://ru.wordpress.org/latest-ru_RU.zip
unzip latest-ru_RU.zip
cd ~/wordpress
cp wp-config-sample.php wp-config.php
nano wp-config.php

Specify the parameters for connecting to the database:

define('DB_NAME', 'ixnfo');
define('DB_USER', 'test');
define('DB_PASSWORD', 'test');
define('DB_HOST', 'localhost');

Also, via the link https://api.wordpress.org/secret-key/1.1/salt/ we will generate unique keys and indicate them in the configuration file.

Move WordPress to the web server directory and specify the rights and owner of the web server (Nginx in Ubuntu works by default from the www-data user):

sudo mv ~/wordpress/ /var/www/test
cd /var/www/test/
mkdir wp-content/uploads
sudo chown -R www-data:www-data /var/www/test/

Create a configuration file for the new site:

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

I will give an example of the content:

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

   root /var/www/test;
   index index.php index.html index.htm;
   access_log /var/log/nginx/test_access.log;
   error_log /var/log/nginx/test_error.log;
   server_name test.ixnfo.com;

        gzip on;
        gzip_comp_level 4;
        gzip_disable "msie6";
        gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;

   location / { 
    try_files $uri $uri/ /index.php$is_args$args;
   }

   location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php7.2-fpm.sock;
   }

   location ~ /\.ht {
      deny all;
    }

   location ~ /\. {
    deny all;
   }

   location = /favicon.ico {
    log_not_found off;
    access_log off;
   }

   location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
   }

   location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
   }

   location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
   }

   location ~* /(wp-config.php|readme.html|license.txt) {
    deny all;
   }

}

If php7.2-fpm is installed, then make sure that the following is specified in the /etc/php/7.2/fpm/pool.d/www.conf file:

listen = /run/php/php7.2-fpm.sock

Create a link to the configuration file in the “sites-enabled” directory, thereby activating it:

ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled/test

Check the configuration is correct and load it:

nginx -t
service nginx reload

If necessary, you can restart Nginx completely:

service nginx restart

Make sure that Nginx starts up at system startup:

sudo systemctl is-enabled nginx
sudo systemctl enable nginx

If you edit the PHP parameters in the /etc/php/7.2/fpm/php.ini file, then to apply the changes you need to restart php-fpm:

service php7.2-fpm status
service php7.2-fpm restart

To complete the installation, open WordPress in a browser, for example
http://test.ixnfo.com/wp-admin/install.php
and follow the instructions. After installation, open the page “Site Health” /wp-admin/site-health.php and follow the recommendations.

If, for example, you often need to specify “unix: /run/php/php7.2-fpm.sock”, then this can be done like this:

upstream php {
        server unix:/run/php/php7.2-fpm.sock;
}

...

   location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass php;
   }

...

See also my articles:
Installing and Configuring WordPress in Ubuntu
Install and configure Nginx
Error solution with REST API “The response is not a valid JSON response” WordPress+Nginx

Leave a comment

Leave a Reply