How to Install WordPress With Nginx and PHP on Linux Mint 19

This article will show you to install wordpress with Nginx web server and PHP-FPM/FastCGI on Linux Mint 19.

In this article,we are using WordPress 5.3 ,Nginx and PHP 7.3.

01、Installation PHP

if you want install PHP 7.3 on Linux Mint 19,you need enabling the Ondrej PHP repository, following command:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Run the following command in the terminal to complete the installation of PHP7.3 and some of the most common PHP modules.

sudo apt install php7.3 php7.3-common php7.3-opcache php7.3-cli php7.3-gd php7.3-curl php7.3-mysql

After the installation is complete,run the following command which will print PHP version:

php -v

02、Installating Nginx

Nginx packages are available under default repositories.type following command:

sudo apt update
sudo apt install nginx

03、Configuration Nginx

We will create a Nginx virtual host to run with FPM/FastCGI,typing command:

sudo vim /etc/nginx/sites-available/example.com

Update the configuration as followings:

# Redirect HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

# Redirect WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/html/example.com;
    index index.php;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

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

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

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

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

Save your changes to the configuration file and create an link to site enabled directory.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Restart Nginx service to reload the changes.

sudo systemctl restart nginx.service

04、Installation WordPress 5.3

Download the latest version from WordPress official website.

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar xf latest.tar.gz
sudo mkdir -p /var/www/html/example.com
sudo mv /tmp/wordpress/* /var/www/html/example.com/

Since both Nginx and PHP are running as www-data user and group, to set the correct ownership run the following chown command:

sudo chown -R www-data: /var/www/html/example.com

Enable the server block by creating a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Before restarting the Nginx service make a test to be sure that there are no syntax errors:

sudo nginx -t

Restart nginx by typing command:

sudo systemctl restart nginx

Open your browser, type your domain and a screen similar to the following will appear.

Now you can follow the prompts to install step by step.

05、Conclusion

Congratulations, you have successfully installed WordPress with Nginx and PHP on your Linux Mint 19.

Leave a Reply