Install WordPress with LEMP Stack on Ubuntu 20.04

WordPress is a very popular content management system that is free and open source. Using WordPress, you can easily create and manage websites and blogs with little or no knowledge of coding.

LEMP is one of the popular open source development stacks used to deploy WordPress. LEMP stack contains Linux, Nginx, MySQL, and PHP. LEMP provides high performance for high traffic websites.

This tutorial describes how to install WordPress on Ubuntu 20.04 with the LEMP stack.

Prerequisite

To follow along, you would require:

Prepare MySQL for WordPress

To store and manage site and user data, WordPress requires a database as well as a user account. Let us create one as follows.

First, login to MySQL with the command below.

$ sudo mysql -u root -p 

Once you are logged in to MySQL, run the queries below to create a database and a user account for WordPress respectively. Replace wpuser and WP@ssw0rd with your own values.

mysql> CREATE DATABASE wordpress;
mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'WP@ssw0rd';
Create MySQL database and user for WordPress
Create MySQL database and user for WordPress

Next, grant the WordPress user account permissions on the wordpress database with the query below.

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';

After that, reload the grant tables with:

mysql> FLUSH PRIVILEGES; 

Type QUIT and press enter to quit MySQL.

Download Latest WordPress

Download the latest version of WordPress with the command below.

$ wget https://wordpress.org/latest.tar.gz

The next step is to extract the downloaded WordPress archive and place it in your website root directory.

Note: Normally, the default website root is /var/www/html on a new installation of NGINX. But, if you have previously changed the default website root to something else, then you would need to specify the correct path accordingly.

If you are not sure, run the command below first to check your website root directory. Replace cloudindevs.com with the name of your server block file.

$ sudo grep root /etc/nginx/sites-available/cloudindevs.com
Check NGINX website root directory
Check NGINX website root directory

My website root is pointing to /var/www/cloudindevs; therefore, I can run the command below to extract the content of the downloaded WordPress archive accordingly.

$ sudo tar -zxvf ./latest.tar.gz -C /var/www/cloudindevs

You should now have a directory named wordpress in your website root directory.

Install WordPress

In the extracted wordpress directory, there is a sample configuration file from which we would need to create the main configuration file. Run the commands below to change into the wordpress directory and copy this file respectively.

$ cd /var/www/cloudindevs/wordpress
$ sudo cp wp-config-sample.php  wp-config.php

After that, open the wp-config.php file for editing with the following command.

$ sudo nano wp-config.php

Press the down arrow key on your keyboard until you get to the MySQL settings section.

Configure WordPress database settings
Configure WordPress database settings

Here, you need to provide the appropriate MySQL database name, username and password which you created earlier for WordPress. Leave everything else as is. Save and close this file.

Next, we are going to use the web-based installer to complete the WordPress installation. Open a web browser and then enter your ServerIPaddress/wordpress to launch the WordPress web installer. For example, 192.168.0.100/wordpress

On the WordPress installation page, fill in the required information.

Launch WordPress web installer
Launch WordPress web installer

Once done, click Install WordPress.

Install WordPress
Install WordPress

You should see a message letting you know that WordPress was successfully installed.

WordPress successfully installed
WordPress successfully installed

You may now login to WordPress and create your first blog post, customize your site, etc.

Login to WordPress dashboard
Login to WordPress dashboard

Access WordPress with your Registered Domain

If you would like to access your WordPress site through your registered domain, then proceed as follows.

Firstly, make sure you have updated your domain's DNS records to point to the IP address of your Ubuntu server. If you are not sure, please consult with your domain registrar.

Secondly, run the command below to create an NGINX server block file for your domain. You need to replace www.yourdomain.com with your domain name.

$ sudo nano /etc/nginx/sites-available/www.yourdomain.com

Thirdly, copy the configuration information below and paste it in the text editor. Replace yourdomain.com and /var/www/webroot with your domain name and the website root path where the extracted wordpress folder is respectively.


server {
       listen 80;
       listen [::]:80;
#
       server_name yourdomain.com;
#
       root /var/www/webroot;
       index index.php;
#
       location / {
               try_files $uri $uri/ =404;
       }
# pass PHP scripts to FastCGI server

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

}

Save changes and close the server block file.

Next, activate the new server block file with the following command.

$ sudo ln -s /etc/nginx/sites-available/www.yourdomain.com /etc/nginx/sites-enabled

Finally, restart NGINX with:

$ sudo systemctl restart nginx

You may now open a web browser and enter yourdomain.com/wordpress to access your WordPress site.

Related Read:

Conclusion

In this guide, we described the steps to download, install and configure WordPress on Ubuntu 20.04. We also showed you how you can connect your registered domain to your WordPress site. As always, please reach out to us with your comments or questions regarding this WordPress tutorial.

Leave a Comment