Site icon DesignLinux

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
$ 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 “[email protected]!”).

MariaDB [(none)]> CREATE DATABASE mysite;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysite.* TO 'mysiteadmin'@'localhost' IDENTIFIED BY  '[email protected]!';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

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

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

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

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

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

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 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).

Sharing is Caring…
Share on FacebookShare on TwitterShare on LinkedinShare on Reddit
Exit mobile version