Ubuntu 20.0.4: How to Install WordPress with LEMP

Introduction

Thinking of starting a new blog? WordPress is a great place to start, and this article will guide you on how to install WordPress with LEMP on an ubuntu server. WordPress is a full-featured content management system (CMS) with all the necessary tools you need to build a great and powerful website. WordPress uses MySQL for database and PHP as its backend language for processing. According to envisagedigital.co.uk WordPress powers 37% of all websites on the internet in 2021.

What you should Already Know

Before you begin the steps provided in this tutorial, here are a couple of things you should already knowHave access to a server, if you dont have one already, you can create one right now by registering an account with vultr. I have many servers hosted on vultr, there’re trusted, reliable and also provide 24hr/7 days customer care support. I have an article coming soon on how to create an account with vultr and deploy your first server. Click here to register on vultr

Setup a sudo user on your server

You know how to install LEMP stack or already have LEMP stack install to your server. This article is on how to install WordPress and not how to install LEMP stack. However, I have an article coming soon on how to install LEMP stack. Nothing to worry about If you don’t know how to install the LEMP stack, Vultr offers a one-click LEMP server deployment. At the point of deploying a new server, switch to marketplace Apps and pick LEMP on Ubuntu 20.04 x64.

An Nginx server block configured on your server for your site.

If you find it difficult to comprehend all I have said to this point, then this article is probably not for you. You should first learn the basics of server administration before returning to this article.

Secure your site with SSL: WordPress serves dynamic content and handles user authentication and authorization. TLS/SSL is the technology that allows you to encrypt the traffic from your site so that your connection is secure. The way you set up SSL will depend on whether you have a domain name for your site.

If you have a domain name, the easiest way to secure your site is with Let’s Encrypt, which provides free, trusted certificates. Follow our Let’s Encrypt guide for Nginx to set this up.

If you have everything ready, then log in to your server with the sudo user privileges to proceed with this tutorial.

Step 1 — Creating a MySQL Database and User for WordPress

We need a database to store our website information and a user to access our database, so therefore we proceed by creating a MySQL database and a user.

Log in to the MySQL root (administrative) account. Unless you’ve changed the default MySQL auth_socket authentication plugin, you can log in to the MySQL administrative account using sudo:

sudo mysql

And if you did change the default configuration for MySQL authentication to use use a password instead, then use the following command to log in to MySQL instead :

mysql -u root -p

You will be prompted for the password you set for the MySQL root account.

After a successful login, proceed to create a database with a name of your choice. Throughout this tutorial, we will be using WordPress as the database name.

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Note: If you experience an error, then check if you included the semi-colon at the end of the statement. A semi-colon is compulsory at the end of every MySQL statement, if left out, MySQL will generate an error.

Now create a user for our database, for simplicity, we’ll use the name wordpressuser in this tutorial, you can pick any username of your choice.

What the code below does is create an account, set a password, and grant access to the database you created. If you decide to go with something else other than wordpressuser, then, In between the first open and close apostrophe, enter the username of your choice. Remember to choose a strong password here:

CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';

Now that we have a database and a user for the database, exit MySQL using the code below

EXIT;

Step 2 — Installing Additional PHP Extensions

WordPress requires more extensions to communicate between PHP and MySQL, use the code below to download and install the following essential PHP extensions that will enable WordPress to work fine.

sudo apt update
sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip

When the installation of the above extensions is complete, restart PHP processor

sudo systemctl restart php7.4-fpm

Step 3 — Configuring Nginx

As stated above, you should already have an Nginx server block configuration for your site. Now there are a few changes we need to make to this file, open up this file and make the following change:

For this tutorial, we’ll use /etc/nginx/sites-available/wordpress as an example here, but you should substitute the path to your configuration file where appropriate.

Additionally, we will use /var/www/wordpress as the root directory of our WordPress install in this guide. Again, you should use the webroot specified in your own configuration.

Note: If you’re hosting a single site on your server, there is a chance you are using the default config which is totally okay, also your site root folder may be /var/www/html, still fine. However, if you plan to host more than one website on this server, it’s best to split the necessary configuration into logical chunks, one file per site.

Open your site’s server block file with sudo privileges to begin:

sudo nano /etc/nginx/sites-available/wordpress

Within the main server block, add a few location blocks.

server {
    . . .

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
    . . .
}

Inside of the existing location / block, let’s adjust the try_files list. Comment out the default setting by prepending the line with a pound sign (#) and then add the highlighted line. This way, instead of returning a 404 error as the default option, control is passed to the index.php file with the request arguments.

This should look something like this:/etc/nginx/sites-available/wordpress

server {
    . . .
    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    . . .
}

When you are finished, save and exit your editor, now check if everything goes well by using this

sudo nginx -t

If no errors were reported, reload Nginx by typing:

sudo systemctl reload nginx

Next, let’s download and set up WordPress.

Step 4 — Downloading WordPress

With everything now in place and ready to run WordPress, its time to download, install and setup wordpress on our server. We will be downloading the latest version of WordPress.

switch into the following directory

cd /tmp

This changes your directory to the temporary folder. Then, enter the following command to download the latest version of WordPress in a compressed file:

curl -LO https://wordpress.org/latest.tar.gz

What the code does: The -LO flag is used to get directly to the source of the compressed file. -L ensures that fetching the file is successful in the case of redirects, and -O writes the output of our remote file with a local file that has the same name.

Extract the compressed file to create the WordPress directory structure:

tar xzvf latest.tar.gz

These files are still located in the tmp folder, soon you will be moving these files into our document root, copy over the sample configuration file to the filename that WordPress actually reads:

cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

The time is here to copy the entire file into our root folder for our website, what the code below does:

  • -a flag to make sure our permissions are maintained,
  • the dot at the end of our source directory signifies everything including files that are hidden
sudo cp -a /tmp/wordpress/. /var/www/wordpress

Assign ownership to the www-data user and group. You must pay attention to this else you may not be able to upload media on your WordPress site or perform automatic updates.

sudo chown -R www-data:www-data /var/www/wordpress

Files are now in the server’s document root and have the correct ownership, but you still need to complete some additional configuration.

Step 5 — Setup the WordPress config file.

Start by generating the WordPress secret keys, type to generate:

curl -s https://api.wordpress.org/secret-key/1.1/salt/

the above code will generate unique WordPress values for you to use, an example is below, do not copy the examples below instead use the code above to generate your own unique values

Warning: If you’re hosting multiple sites, always request unique values each time. Do NOT copy the values shown below!

Outputdefine('AUTH_KEY',         '98l/vqfs<XhdXaAPz9 DO NOT COPY THESE VALUES c_j{iwqD^<+c9.k<J@4H');
define('SECURE_AUTH_KEY',  'E2N-h2]Dcvp+aS/47X DO NOT COPY THESE VALUES {Ka(f;rv?Pxf})CgLi-3');
define('LOGGED_IN_KEY',    'W(50,{W^,OPB%PB<JF DO NOT COPY THESE VALUES 2;y&,2m%3]R6DUth[;88');
define('NONCE_KEY',        'll,4UC)7ua+8<!4VM+ DO NOT COPY THESE VALUES #`DXF+[$atzM7 o^-C7g');
define('AUTH_SALT',        'koMrurzOA+|L_lG}kf DO NOT COPY THESE VALUES  07VC*Lj*lD&?3w!BT#-');
define('SECURE_AUTH_SALT', 'p32*p,]z%LZ+pAu:VY DO NOT COPY THESE VALUES C-?y+K0DK_+F|0h{!_xY');
define('LOGGED_IN_SALT',   'i^/G2W7!-1H2OQ+t$3 DO NOT COPY THESE VALUES t6**bRVFSD[Hi])-qS`|');
define('NONCE_SALT',       'Q6]U:K?j4L%Z]}h^q7 DO NOT COPY THESE VALUES 1% ^qUswWgn+6&xqHN&%');

Copy the entire code generated above and open your WordPress config file.

Now, open the WordPress configuration file:

sudo nano /var/www/wordpress/wp-config.php

Go over your config file and find the part that has these dummy values as shown below

Your WordPress config file look something like this:/var/www/wordpress/wp-config.php

. . .

define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

. . .

Delete those lines and paste in the values you copied from the command line:/var/www/wordpress/wp-config.php

. . .

define('AUTH_KEY',         'VALUES COPIED FROM THE COMMAND LINE');
define('SECURE_AUTH_KEY',  'VALUES COPIED FROM THE COMMAND LINE');
define('LOGGED_IN_KEY',    'VALUES COPIED FROM THE COMMAND LINE');
define('NONCE_KEY',        'VALUES COPIED FROM THE COMMAND LINE');
define('AUTH_SALT',        'VALUES COPIED FROM THE COMMAND LINE');
define('SECURE_AUTH_SALT', 'VALUES COPIED FROM THE COMMAND LINE');
define('LOGGED_IN_SALT',   'VALUES COPIED FROM THE COMMAND LINE');
define('NONCE_SALT',       'VALUES COPIED FROM THE COMMAND LINE');

. . .

While we’re still here, let’s change a few other settings and configure our database for our WordPress Site. Adjust the database name, the database user, and the associated password that was configured within MySQL.

The other change you should make is to set the method that WordPress uses to write to the filesystem. Since you’ve given the web server permission to write where it needs to, you can explicitly set the filesystem method to “direct”. Failure to set this with our current settings would result in WordPress prompting for FTP credentials when we perform some actions. Add this setting below the database connection settings, or anywhere else in the file:/var/www/wordpress/wp-config.php

. . .

define( 'DB_NAME', 'wordpress' );

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

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

. . .

define( 'FS_METHOD', 'direct' );

Save and close the file when you’re done.

Step 6 — Completing the Installation Through the Web Interface

Having made all necessary installation and setup, it is now time to visit your website or your server’s public IP address to complete the rest of the installation process using the WordPress Web Interface.

http://server_domain_or_IP/wordpress

Select the language you would like to use:

WordPress language selection

Proceed to fill in your information on the next screen, when its time to pick a password, do not use your name, or admin as a password pick a secure and difficult password to guess, you do not have to remember it, as your browser can save it for you and autofill it whenever needed.

Enter your email address and select whether you want to discourage search engines from indexing your site:

WordPress Setup Installation

When you click ahead, you will be taken to a page that prompts you to log in:

WordPress Login promp

Once you log in, you will be taken to the WordPress administration dashboard:

WordPress Admin Dashboard

Conclusion

If you’ve made it this far, then your website should be ready for the next stage which is picking an appropriate theme, setting your permalinks, and installing the necessary plugins.

If this article was helpful please leave a comment below, and if you have trouble following this tutorial, also leave a comment below, I will do my best to reply ASAP

Leave a Reply

Your email address will not be published.

Back to top button