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.

Categories
Tutorials

How to create a new user & configure a firewall on Ubuntu 18.04

This tutorial will guide you to set up a new user and to configure the firewall with UFW on Ubuntu 18.04.

First, log into your Ubuntu as root user and then go to your SSH or Ubuntu terminal.

Adding a new user

Add the user by using adduser command as shown below. Use any preferred name you like after adduser command and you will be asked to create and verify a password for the user.

adduser bishrulhaq

Next youโ€™ll be asked to fill in some information about the new user.

Changing the user information for bishrulhaq
Enter the new value, or press ENTER for the default
    Full Name []: bishrulhaq
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n]
Code language: CSS (css)

Next use the usermod command to add the user to the sudo group. On Ubuntu, all members of the sudo group have full sudo privileges.

usermod -aG sudo bishrulhaq
Enabling SSH access for newly added User

If you’re using SSH to access the server and password authentication is enabled then you need to copy your local public key to the new userโ€™s ~/.ssh/authorized_keys file to log in successfully.

To copy the files with the correct permissions is by using the rsync command. This helps to copy the root userโ€™s .ssh directory and to copy the permissions, modify the file owners with a single command. 

rsync --archive --chown=bishrulhaq:bishrulhaq ~/.ssh /home/bishrulhaq
Code language: JavaScript (javascript)
Setting Up a Firewall with UFW

Uncomplicated Firewall (UFW) is an interface to iptables that is designed to simplify the process of configuring a firewall and it is used to allow connections to specified services.

Type the following command to check the list of profiles registered with UFW.

ufw app list
Code language: PHP (php)

It will return with the list of available applications and I’m going to allow OpenSSH by typing the following command.

ufw allow OpenSSH

Then, You can enable the firewall by typing the command.

ufw enable

Check the status of allowed connections by typing:

ufw status

The firewall will block all the connections except the one you have allowed. If you install any new services you need to allow the connection in order to use it.

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