Save 50% for your first year of web hosting!

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;

        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   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;
    }
}

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;

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

   location / {
    if ($request_uri ~ ^/(.*)\.html$) {
        return 302 /$1;
    }

    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;
    }

}

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

To install the 2nd repository:

sudo add-apt-repository ppa:ondrej/php

Run this to download your added repositories:

sudo apt update

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

sudo apt install php7.3-fpm php7.3-common php7.3-mysql php7.3-gmp php7.3-curl php7.3-intl php7.3-mbstring php7.3-xmlrpc php7.3-gd php7.3-xml php7.3-cli php7.3-zip

It is recommended to raise the memory limit and disable cgi.fix_pathinfo. Your PHP configuration is located in/etc/php/7.3/cli/php.ini.

sudo nano /etc/php/7.3/cli/php.ini

Press CTRL + W and search for memory_limit.

Replace it with memory_limit = 256

Press CTRL + W and search for ;cgi.fix_pathinfo=1

Replace it with cgi.fix_pathinfo=0

Save & exit by pressing CTRL + X followed by Y

Add nginx to www-data group

sudo usermod -a -G www-data nginx

Change owner of directory to www-data

sudo chown -R www-data /usr/share/nginx/html

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.

If you don’t already have a VPS, or want to reward us for hard work and ad free experience you can buy a VPS from us. We would really appreciate it as we don’t have any donation buttons and this is our only source of income. Our VPSes are located in Stockholm, and we even have a Swedish branded site if you’re from Sweden.