How to Install WordPress with Nginx in Ubuntu 20.04

Today, over 36% of the web runs on the WordPress platform, as it is one of the most widely used open-source content management systems for creating a website or blog using its powerful features, beautiful designs, and above all, the freedom to build anything you want.

Read Also: How to Install WordPress with Apache in Ubuntu 20.04

In this article, you will learn how to install WordPress with Nginx webserver in Ubuntu 20.04. To install WordPress, you must have LEMP stack installed on your Ubuntu 20.04 server, otherwise, see our guide:

Installing WordPress in Ubuntu 20.04

1. Once you have LEMP stack in place, move further to download and set up WordPress from its official site using the following wget command.

$ wget -c http://wordpress.org/latest.tar.gz

2. When the package has finished downloading, extract the archived file using the tar command as shown.

$ tar -xzvf latest.tar.gz

3. Now copy the content of the wordpress folder into your website’s folder (e.g mysite.com) which should be stored under the webserver’s web document root (/var/www/html/), as shown.

Note that when using the cp command, the mysite.com directory doesn’t have to exist before, it will be automatically created.

$ ls -l
$ sudo cp -R wordpress/ /var/www/html/mysite.com
$ sudo ls -l /var/www/html/mysite.com/
Host WordPress On Website
Host WordPress On Website

4. Next, set correct permissions on the website directory /var/www/html/mysite.com. The webserver user and group www-data should own it with read, write, and execute permissions.

$ sudo chown -R www-data:www-data /var/www/html/mysite.com
$ sudo chmod -R 775 /var/www/html/mysite.com

Creating a WordPress Database for Website

5. WordPress requires a database for website data storage. To create one for your site, log into the MariaDB shell using the mysql command using the -u option to provide the username and -p for the password and also use sudo if you are accessing as the root database user.

$ sudo mysql -u root -p 
OR
$ sudo mysql -u root		#this also works for root database user

6. Once you have accessed the database shell, issue the following commands to create your website’s database, database user and a password as shown (do not forget to use your values instead of “mysite”, “mysiteadmin” and “SecureP@ssHere!”).

MariaDB [(none)]> CREATE DATABASE mysite;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysite.* TO 'mysiteadmin'@'localhost' IDENTIFIED BY  'SecureP@ssHere!';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
Create WordPress Database
Create a WordPress Database

7. At this point, you need to create a wp-config.php file for your new WordPress installation, where you will define the database connection and some other parameters as well. Move into the website’s document root /var/www/html/mysite.com and create a wp-config.php file from the sample file provided by default.

$ cd /var/www/html/mysite.com
$ sudo mv wp-config-sample.php wp-config.php

8. After creating a wp-config.php file, open it for editing.

$ sudo vim wp-config.php

Now modify the database connection settings (the name of the database for WordPress, MariaDB database username, and the user’s password) as highlighted in the following screenshot, so that your new WordPress site will connect to the database you created for it.

Set Database Settings in WordPress Config
Set Database Settings in WordPress Config

Creating an NGINX Virtual Server Block (VirtualHost) for WordPress Website

9. For NGINX to serve your website to clients using your domain name (e.g mysite.com), you need to configure a virtual server block (analogous to virtual host under Apache) for your site in the NGINX configuration.

Create a file called mysite.com.conf under the /etc/nginx/conf.d/ directory as shown.

$ sudo vim /etc/nginx/conf.d/mysite.com.conf

Copy and paste the following configuration in the file. Remember to replace the mysite.com and www.mysite.com with your domain name.

server {
        listen 80;
        listen [::]:80;
        root /var/www/html/mysite.com;
        index  index.php index.html index.htm;
        server_name mysite.com www.mysite.com;

        error_log /var/log/nginx/mysite.com_error.log;
        access_log /var/log/nginx/mysite.com_access.log;
        
        client_max_body_size 100M;
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}
Create Nginx Server Block for WordPress Website
Create an Nginx Server Block for WordPress Website

Note: In the above configuration, the value of fastcgi_pass parameter should point to the socket PHP-FPM is listening on, as defined by the value of the listen parameter in the /etc/php/7.4/fpm/pool.d/www.conf pool configuration file. The default is a UNIX socket /run/php/php7.4-fpm.sock.

10. Importantly, NGINX normally routes all requests to the default server. Therefore, remove the default server block file to enable your new site and other sites you intend to set up on the same server to load well.

$ sudo rm /etc/nginx/sites-enabled/default
$ sudo rm /etc/nginx/sites-available/default

11. Next, check the NGINX configuration syntax for any errors before you can restart the Nginx service to apply the above changes.

$ sudo nginx -t
$ sudo systemctl restart nginx
Check Nginx Configuration
Check Nginx Configuration

Completing the WordPress Installation via the Web Installer

12. Next, you need to complete the WordPress installation using the web installer. Open a web browser and use your domain name to navigate:

http://mysite.com/
OR
http://SERVER_IP/

When the web installer loads, choose the language you prefer for the installation process and click Continue.

Select WordPress Installation Language
Select WordPress Installation Language

13. Then fill in the required information about your new website. That is the site title, administrative username, the user’s password, and email address. Then click Install WordPress. Note that you can always edit this information later on.

Add WordPress Site Details
Add WordPress Site Details

14. After WordPress is successfully installed, proceed to access the website administrator’s dashboard by clicking on the login button as highlighted in the following screen.

WordPress Installation Complete
WordPress Installation Complete

15. At the website admin’s login page, provide your username and password created above and click login, to access your site’s admin dashboard.

WordPress Login
WordPress Login
WordPress Dashboard
WordPress Dashboard

Congratulations! You have successfully installed the latest version of WordPress with NGINX in Ubuntu 20.04, to start building your new website or blog.

To run a secure site, you need to enable HTTPS by installing an SSL/TLS certificate for encrypted communication with clients. In a production environment, it is recommended to use Let’s Encrypt certificate is free automated, open, and trusted by most if not all modern web browsers. Alternatively, you can buy one from a commercial certificate authority (CA).

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

21 Comments

Leave a Reply
  1. Hello, thanks for the great article!

    It went almost fine until the last step for me. I am configuring everything with the “final” domain in mind, but I would like to keep it as a staging environment for a few days to have everything set up. Ping works fine with the server’s IP but I have an “unable to connect/problem loading page”, even with the hostname of the server (trying IP and hostname provided from provider).

    The only thing I did wrong was that I forgot to put the site’s name at mysite.com.conf and used !mv to rename it in vim afterward, and nginx returned successfully. Any ideas for that?

    Also, it would be great to include the secure configuration (https) and the installation steps for ssl cert (letsencrypt maybe?)

    Reply
  2. I have a strange issue. My setup went correctly, but my site note opening up well from the Internet, but on my local network it opens up okay. Not sure what to do to resolve this.

    Reply
    • @Emmanuel,

      You need to create a DNS A record for your domain on the DNS domain hosting provider panel and point the domain to your IP address to resolve this…

      Reply
  3. After removing the default sites from nginx with:

    $ sudo rm /etc/nginx/sites-available/default
    

    I was unable to get to phpmyadmin!

    Doh! no backup made before they were deleted.

    Reply
  4. @victor101010001010

    Have you taken a look at the NGINX error log file (/var/log/nginx/error.log), I believe it has entries relating to this issue you are facing.

    Reply
  5. Hi, I have followed your instructions to the letter but at the end when I have to type http://mysite.com OR http://SERVER_IP/ the web installer doesn’t load on my localhost site and the page times out.

    Any help would be appreciated

    Reply
  6. Hi, I followed every step until 11. Then at 12, I get an error message: Error establishing a database connection.

    How can I search for a solution?

    Reply
    • @Felipe,

      Please create the wordpress database with right user privileges as shown in step #6 and enter the correct database details in the wp-config.php file as shown at step #8.

      Reply
  7. Just a note. You don’t need Flush privileges if you use the GRANT commands. You only need to flush privileges if you update the privileges tables directly!

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.