Install WordPress on Ubuntu 18.04 with Nginx, MariaDB, PHP7.2 (LEMP)

This tutorial is going to show you how to install WordPress on Ubuntu 18.04 with Nginx, MariaDB and PHP7.2 (LEMP Stack). WordPress is the most popular CMS (Content Management System) in the world. It is estimated that more than a quarter of websites today are powered by WordPress. PHP7.2 made into the Ubuntu 18.04 repository and WordPress runs perfectly with it.


To follow this tutorial, you need to have an Ubuntu 18.04 server with at least 1GB RAM. If you are looking for a VPS (Virtual Private Server), then you can click this special link to get $50 free credit on DigitalOcean. (For new users only). If you are already a DigitalOcean user, then you can click this special link to get $50 free credit on Vultr (for new users only).

This tutorial assumes that you have already set up a LEMP stack on Ubuntu 18.04. If not, please check out the following tutorial.

After finishing LEMP installation, come back here and read on.

Step 1: Download WordPress

SSH into your Ubuntu 18.04 server and update existing software.

sudo apt update && sudo apt upgrade

Next, go to download page and download the zip archive. You can acquire the direct download link by right-clicking the download button and select copy link location.

install wordpress on ubuntu 17.10

Then at the command line prompt, type in wget followed by the direct download link to download WordPress to your Ubuntu 18.04 server.


Next, extract the zip archive using the command below.

sudo apt install unzip

sudo unzip -d /usr/share/nginx/

The archive will be extracted to /usr/share/nginx/ directory. A new directory named wordpress will be created (/usr/share/nginx/wordpress). Now we can rename it like below. Replace with your real domain name.

sudo mv /usr/share/nginx/wordpress /usr/share/nginx/

Step 2: Create a Database and User for WordPress Site

Log into MariaDB shell as root with the following command.

sudo mariadb -u root


sudo mysql -u root

Once you are logged in, create a database for WordPress using the following command. I named it wordpress, but you can use whatever name you like such as you site name. (Don’t leave out the semicolon.)

create database wordpress;

Then enter the command below to create a database user for WordPress. This command also grant all privileges of WordPress database to the user. Replace wpuser and your-password with your preferred username and password.

grant all privileges on wordpress.* to wpuser@localhost identified by 'your-password';

Flush the privileges table for the changes to take effect and then get out of MariaDB shell.

flush privileges;


Step 3: Configure WordPress

Go to your WordPress directory.

cd /usr/share/nginx/

Copy the sample configuration file and rename it to wp-config.php.

sudo cp wp-config-sample.php wp-config.php

Now edit the new config file.

sudo nano wp-config.php

Find the following lines and replace the red texts with the database name, username and password you created in the previous step.

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

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

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

Save and close the file. We also need to set the Nginx user (www-data) as the owner of the WordPress site directory by using the following command.

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

Step 4: Create a Nginx Server Block for WordPress

We will create the server block file in /etc/nginx/conf.d/ directory. The file name must end with .conf.

sudo nano /etc/nginx/conf.d/

Put the following texts into the file. Replace the red texts with your own domain name. Don’t forget to create A records for your domain name.

server {
  listen 80;
  root /usr/share/nginx/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ /index.php;

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;

  #enable gzip compression
  gzip on;
  gzip_vary on;
  gzip_min_length 1000;
  gzip_comp_level 5;
  gzip_types application/json text/css application/x-javascript application/javascript image/svg+xml;
  gzip_proxied any;

  # A long browser cache lifetime can speed up repeat visits to your page
  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;

  # disable access to hidden files
  location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

The Final Step: Run WordPress Install Wizard

Enter your domain name in browser address bar. You shall see the WordPress installation wizard. Select a language.



If the installation wizard isn’t displayed,  you probably need to install some PHP7 extensions.

sudo apt install php-imagick php7.2-mbstring php7.2-bcmath php7.2-xml php7.2-mysql php7.2-common php7.2-gd php7.2-json php7.2-cli php7.2-curl

Then reload PHP-FPM and Nginx. The wizard should now be displayed.

sudo systemctl reload php7.2-fpm

sudo systemctl reload Nginx

Create an admin account and click install WordPress button.


And now your new WordPress site is installed.


How to Redirect www to non-www (Or Vice Versa)

The Nginx configuration we created allows site visitors to access the website via both www and non-www domain. You can redirect one version to another by going to WordPress Dashboard > Settings > General. Then set WordPress Address and Site Address to your preferred version.


Prevent Malicious Requests

After WordPress is installed, there’s no need to access the /wp-admin/setup-config.php and /wp-admin/install.php URL any more. However, bad actors may access these two URLs and if a vulnerability is found, the bad actor could get into your WordPress back end. To prevent access to these two URLs, add the following lines in the Nginx server block. Add them before the location ~ \.php$ {  line.

location ~* ^/wp-admin/(setup-config|install)\.php$ {
       deny all;

If you don’t want other people to register account on your WordPress site, you can add the following lines to restrict access to the /wp-login.php URL, so only your IP address can access this URL. Replace with your own IP address.

location = /wp-login.php {
    fastcgi_pass unix:/run/php/php7.2-fpm.sock; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include fastcgi_params; 
    include snippets/fastcgi-php.conf; 
    deny all;

Don’t forget to reload Nginx for the changes to take effect.

sudo systemctl reload nginx

If you don’t have a static IP address on your home network, you can set up a VPN server in a data center.

Next Step

I hope this tutorial helped you install WordPress on Ubuntu 18.04 with Nginx, MariaDB and PHP7.2 (LEMP stack). As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 6 Average: 5]

11 Responses to “Install WordPress on Ubuntu 18.04 with Nginx, MariaDB, PHP7.2 (LEMP)

  • I followed this tutorial and wanted to say thanks for putting this altogether. It was very easy to follow and helped me out. I wanted to add an issue I ran across as it may help other people. After I got WordPress installed the Post New function was broken. I would keep getting this error message: “The editor has encountered an unexpected error.” I spent a while going through each line of the nginx configuration block but didn’t understand the issue. After some googling I found this:

    What I did to fix this was changing:

      location / {
        try_files $uri $uri/ /index.php;


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

    I hope that this can help others and save them some time if they run across the issue.

    • I remember using

      try_files $uri $uri/ /index.php$is_args$args;

      might cause problems when you want to change the WordPress permalinks.

      I don’t use the Gutenburg editor. I Installed a plugin to enable the classic editor in WordPress.

      • I do use this one and does not break permalinks.

        “try_files $uri $uri/ /index.php?$args;”

        Hope that helps.

    • cryptopher
      5 months ago

      @adam, I would like to thank you for the fix you posted. I had the exact same issue using elementor as an editor. Even Divi wasnt allowing me to edit pages when I changed my permalinks to post only. My editors would only work if I had my permalinks on plain. After editing my nginx config with what you suggested my editor is working after changing my permalinks. You are a legend. And thanks guys for the detailed guides, I have found them super helpful!! Very helpful and detailed!!!!

  • When I try to run the install wizard I get a 404 not found error. Any ideas what I did wrong?

    • Nevermind… I figured it out. I followed the LEMP tutorial first but did not delete the default.conf file in conf.d, lol.

  • finally, I found a working manual, everything works according to your instructions Thank you very much you’re the best

  • The most beautiful and descriptive article I have seen on this subject.

    It was very beautiful and successful, thank you.

  • How do I configure WordPress to send emails?

    Is there any article on how to integrate the wordpress with your “email server” – the one you have on your 9 steps ?

    Thank You.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community ( for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB.
You can upload: image.