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

Installing Nginx, PHP, MySQL and PHPMyAdmin on Ubuntu 18.04

This tutorial is created to set up Nginx and PHPMyAdmin along with PHP 7.4 on ubuntu 18.04 with simple and easy steps.

Update via the command line

Before beginning the whole set up process make sure you’re at the command line and type the following commands.

sudo apt update
 Install NGINX

Now let’s install NGINX and check whether it’s running or not.

sudo apt install nginx -y
systemctl status nginx

The β€œ-y” in the end automatically enters β€œyes” when the command β€œsudo apt install nginx -y” ask for your confirmation before installing. After the installation. Check your Server by navigating into your IP ADDRESS as shown below.

http://IP_SERVER
Code language: JavaScript (javascript)
Installing PHP and MariaDB

Install PHP 7.4 and all the important plugins and check the server status.

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt install php7.4-fpm php7.4-common php7.4-dom php7.4-intl php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-soap php7.4-zip php7.4-bcmath -y

Run the following command to check the status of php 7.4

systemctl status php7.4-fpm
Code language: CSS (css)

Import MariaDB gpg key by running the command below to add it to your system.

Visit MariaDB website to obtain the gpg key

sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
Code language: JavaScript (javascript)

Once the key is imported add the apt repository URL

sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirror.nodesdirect.com/mariadb/repo/10.4/ubuntu bionic main'
Code language: JavaScript (javascript)

After that Install the MariaDB Server on ubuntu by following commands.

apt install mariadb-server -y
systemctl status mariadb
mysql_secure_installation
Install PHPMyAdmin

Install PHPMyAdmin 5.0.2 as shown below by extracting it to the /usr/share/phpmyadmin directory.

To install the latest version of phpMyAdmin visit the phpMyAdmin page

wget -c https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-english.tar.gz
tar xzvf phpMyAdmin-5.0.2-english.tar.gz
sudo mv phpMyAdmin-5.0.2-english /usr/share/phpmyadmin
ln -s /usr/share/phpmyadmin /var/www/html
Code language: JavaScript (javascript)
Configure NGINX

Open the NGINX configuration file by entering the following command.

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

Add the following lines of code to the server block

index index.php index.html index.htm;

location ~ \.php$ {
  try_files $fastcgi_script_name =404;
  include fastcgi_params;
  fastcgi_pass  unix:/run/php/php7.4-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param DOCUMENT_ROOT  $realpath_root;
  fastcgi_param SCRIPT_FILENAME   $realpath_root$fastcgi_script_name; 
}

Code language: PHP (php)

Test the configuration and restart the server.

nginx -t
systemctl restart nginx
systemctl status nginx

Hoorey! Now it’s time to test PHPMyAdmin by typing following URL.

http://IP_SERVER/phpmyadmin
Code language: JavaScript (javascript)

If you encounter an error The $cfg'TempDir' is not accessible... on PHPMyAdmin.

Create a tmp Directory in PHPMyAdmin

mkdir /usr/share/phpmyadmin/tmp

And then add permission to that created directory

chmod 755 -R /usr/share/phpmyadmin/tmp

If you are see an error β€œThe secret passphrase in configuration (blowfish_secret) is too short.”

config.inc.php

If your PHPMyAdmin directory doesn’t contain any config.inc.php file then copy config.sample.inc.php and create config.inc.php as shown below.

mv /usr/share/phpmyadmin/config.sample.inc.php /usr/share/phpmyadmin/config.inc.php

Open the PHP Config file by entering the following command

sudo nano /usr/share/phpmyadmin/config.inc.php

Enter blowfish secret code with 32 characters random phrase.

$cfg['blowfish_secret'] = '32_char_random_phrase_here';
// KLS$vbc91Lkja$vc@opGbxA278EWopdc
Code language: PHP (php)
Watch the whole tutorial from the video below,

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