HostUp

How to Install WordPress on Latest Nginx Mainline Version on Ubuntu 16.04 / 18.04

If you do not already have Nginx installed, then please follow this tutorial.

Download & Extract WordPress

First of all, we need to download and extract WordPress to your website HTML folder. (Default /usr/share/nginx/html).

cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz

IF you want your blog to be installed in a sub directory (example.com/blog) then run the following command:

sudo cp -a wordpress /usr/share/nginx/html/blog/

IF you want your WordPress to be installed in your main directory instead (example.com) then run the following command:

sudo cp -a wordpress /usr/share/nginx/html/

Now that we have downloaded WordPress and copied it over to our Nginx website directory we need to edit your website config, default.conf. You can edit it by running the following:

sudo nano /etc/nginx/conf.d/default.conf

If you want your blog to be installed in a subdirectory like blog, example.com/blog/ then put the following config in

server {
    listen       80;
    listen       [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;

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

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    if (!-e $request_filename) {
    rewrite ^.*$ /blog/index.php last;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

You can change ‘/blog’ to whatever subdirectory you want. If you want your WordPress to be installed at your main directory instead then simply remove ‘blog/’ so your configuration file will look like this:

server {
    listen       80;
    listen       [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

Install MariaDB

sudo apt-get install mariadb-server mariadb-client
sudo systemctl enable mysql.service
sudo systemctl start mysql.service
sudo mysql_secure_installation

You will now be prompted with a configuration setup file. Please read through it and configure it to your liking. It is recommended to disable logins that are not from localhost.

After this you may create your database for wordpress. Login by:

sudo mysql -u root -p

Create your database. We will call it ‘blog’ although you are free to call it whatever you want.

CREATE DATABASE blog;
FLUSH PRIVILEGES;
EXIT;

Configure wordpress configuration file

sudo mv /usr/share/nginx/html/blog/wp-config-sample.php  
/usr/share/nginx/html/blog/wp-config.php

Now let’s open it!

sudo nano /usr/share/nginx/html/blog/wp-config.php

Change your database name, username and password.

define('DB_NAME', 'blog');

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

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

If you set any password to your MariaDB then please specify it in the correct field.

Install PHP 7.3

To install the repositories for php:

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

We can now install PHP now that we have all of the required repositories.

sudo apt-get install php7.3 libapache2-mod-php7.3 php7.3-cli php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc -y

Your PHP configuration is located in/etc/php/7.3/cli/php.ini

That’s it!

Congratulations you have now installed & configured WordPress for Nginx web-server. You can visit your website at http://yourdomain.tld and start the final configuration. If you want to add an SSL certificate so you can visit your website from ‘https://’ and other security options then please do follow this tutorial.

Add comment

Most popular

Most discussed