Categories
Tutorials

How to install WordPress with Nginx on Ubuntu 18.04

In this tutorial you’ll learn to set up WordPress the most popular CMS (content management system) on ubuntu 18.04.

Please Note :

In order to complete this tutorial, You probably have installed Nginx, MySQL, and PHP already on your Ubuntu 18.04.

If you haven’t installed them already then refer Install PHP, MYSQL and PHPMyAdmin to configure them before installing WordPress.

Configure Nginx

Open up the Nginx configuration file using the following command.

nano /etc/nginx/sites-available/default
Code language: JavaScript (javascript)

Add the following lines of code inside the server block to avoid log requests for favicon.ico , robots.txt and the regular expression location to match any requests for static files (.css, .gif, .ico, .jpeg, .jpg, .js, .png)

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)$ {
        log_not_found off;
    }
    . . .
}

Change the existing try_files under location blocks to index.php file with the request arguments as shown below.

server {
    . . .
    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    . . .
}
Code language: PHP (php)

Check the configuration file for syntax errors by typing

sudo nginx -t

Now, restart your Nginx Server using the following command

sudo systemctl reload nginx
Creating a MySQL database along with a User for WordPress

Log into the MySQL account by the following command.

mysql -u root -p

Now, We’ll create the database known as ‘wordpress’ by typing:

mysql> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Code language: PHP (php)

Next, we will be creating a separate MySQL user account with password that we will grant access to the newly created ‘wordpress’ database by typing the following command:

mysql> GRANT ALL ON wordpress.* TO 'bishrulhaq'@'localhost' IDENTIFIED BY 'password';
Code language: JavaScript (javascript)

Note :

I have created the user with the name bishrulhaq and you can change as you like by specifiying any name you want.

Now it’s time to flush the privileges so that the current instance of MySQL knows about the recent changes we’ve made by entering the following command:

mysql> FLUSH PRIVILEGES;

To check the databases type the following command :

mysql> SHOW DATABASES;

Exit out of MySQL by typing the command below which will exit from the MySQL session and return to the regular Linux shell.

mysql> EXIT;
Code language: PHP (php)
Download and install WordPress

Now that we have configured the database and user, It’s time to install the latest WordPress from their site.

Go to WordPress Download and you’ll see an option to download the .tar file. copy the download link of .tar file and extract the compressed file by using the following commands.

wget -c https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
Code language: JavaScript (javascript)

Copy the sample configuration file to the wp-config.php file by typing the following command:

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

Now, move the entire contents of the directory into the html directory by typing the following command. 

sudo mv wordpress/* /var/www/html

Next, assign ownership to html directory by the www-data user and group

sudo chown -R www-data:www-data /var/www/html
Code language: JavaScript (javascript)
Configure WordPress

Open the WordPress configuration file and alter the database values and add the define('FS_METHOD', 'direct')method which allows the ‘direct‘ method to install wordpress plugins, themes, or updates.

nano /var/www/html/wp-config.php
Code language: JavaScript (javascript)
. . .

define('DB_NAME', 'wordpress');

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

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

. . .

define('FS_METHOD', 'direct');
Code language: JavaScript (javascript)

Now grab the secure values from the WordPress secret key generator to set the keys at configuration file to provide extra layer of security to the cookies and passwords.

Grab the secret keys by entering the following command:

curl -s https://api.wordpress.org/secret-key/1.1/salt/
Code language: JavaScript (javascript)

Alter the values in the configuration file by copying the generated secret keys.

define('AUTH_KEY',         'PASTE THE GENERATED KEY HERE');
define('SECURE_AUTH_KEY',  'PASTE THE GENERATED KEY HERE');
define('LOGGED_IN_KEY',    'PASTE THE GENERATED KEY HERE');
define('NONCE_KEY',        'PASTE THE GENERATED KEY HERE');
define('AUTH_SALT',        'PASTE THE GENERATED KEY HERE');
define('SECURE_AUTH_SALT', 'PASTE THE GENERATED KEY HERE');
define('LOGGED_IN_SALT',   'PASTE THE GENERATED KEY HERE');
define('NONCE_SALT',       'PASTE THE GENERATED KEY HERE');
Code language: JavaScript (javascript)

Save and close the configuration file when you are finished editing.

Now, navigate to your browser and complete the installing through the web interface.

http://server_domain_or_ip_address
Code language: JavaScript (javascript)

Hope this article helped you 😊. If you like this please share with others and drop your ideas and suggestions at the comment section.

Leave a Reply

Your email address will not be published.