Database

Host WordPress in Ubuntu 20.04, Mysql 8, Ubuntu 20.04, Nginx

Pinterest LinkedIn Tumblr

In this guide, we will cover the steps necessary to have WordPress up and running in Ubuntu 20.04 server served by php 7.4, php fpm and Mysql 8 – the LEMP stack.

WordPress is a free and open-source content management system written in PHP and paired with a MySQL or MariaDB database. WordPress is one of the most popular CMS available today.

WordPress.com is a hosted software-as-a-service (SaaS) platform that lets you build a website using WordPress building blocks. It will cost you to host your website on the WordPress site.

The WordPress software is free and Open Source but to have it up and running you need to set up hosting. In this guide we are going with this option, setting up WordPress in Ubuntu 20.04.

Prerequisites

To follow along this guide, we need the following:

  • Up to date Ubuntu 20.04 server with internet connection
  • Root access in the server or a user with sudo access
  • Knowledge of Linux terminal usage

Steps needed to have WordPress working in Ubuntu 20.04

  1. Ensure that the system is up to date
  2. Installing mysql 8 database server and creating WordPress user
  3. Install the Nginx Web Server
  4. Install PHP with dependencies
  5. Configure PHP and PHP FPM Settings
  6. Downloading and Configuring WordPress
  7. Set up Nginx to Serve wordpress content
  8. Test WordPress Installation

1. Ensure that the system is up to date

Before proceeding, let us ensure that our server is updated. Use these commands to 1. Update the server repositories, 2. Upgrade server packages to the latest releases:

# Update repos
sudo apt update

# Upgrade packages
sudo apt -y upgrade

2. Installing mysql 8 database server and creating WordPress user

The mysql-server package required is not present in the default Ubuntu repositories. So we need to set it up.

The mysql team provides a downloadable .deb file that will configure the repositories for mysql server 8 installation. Download it with this command:

curl -LO https://dev.mysql.com/get/mysql-apt-config_0.8.19-1_all.deb

Once the download is done, we need to install the downloaded deb file. Use this command to install:

sudo dpkg -i ./mysql-apt-config_0.8.19-1_all.deb

This will open a configuration window prompting you to choose mysql server version and other components such as cluster, shared client libraries, or the MySQL workbench

Citizix Configure Mysql 8 On Ubuntu 20.02
Citizix Configure Mysql 8 On Ubuntu 20.02

For now since we are only interested in the Mysql Server Installation, leave the default settings and click OK to proceed.

Installing mysql server

Now that the repos have been added to include mysql server, we can now install. Let us refresh the Repositories to get the latest from the added repo using this command:

$ sudo apt update

Then Install Mysql 8 Server using this command:

sudo apt install -y mysql-server

Enter your administrator credentials, and the system will install the MySQL server package, client packages, and database common files.

The installation will prompt you to enter and confirm a root user and password for the MySQL database.

Citizix Set Root Password on Mysql 8 Ubuntu-20.04
Citizix Set Root Password on Mysql 8 Ubuntu-20.04

After that you will be asked to select Authentication plugin. It is recommended that you choose to use a strong password:

Citizix Auth Plugin Mysql 8 Ubuntu 20.04
Citizix Auth Plugin Mysql 8 Ubuntu 20.04

After that the new password will be set and service reloaded.

Confirm that mysql server is up and running with this command:

$ sudo systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-09-28 13:08:22 UTC; 2min 23s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 45123 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 45173 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4710)
     Memory: 353.0M
     CGroup: /system.slice/mysql.service
             └─45173 /usr/sbin/mysqld

Sep 28 13:08:21 ip-172-26-11-229 systemd[1]: Starting MySQL Community Server...
Sep 28 13:08:22 ip-172-26-11-229 systemd[1]: Started MySQL Community Server.

The Active: active (running) since ... portion in the above shows that the app is up and running.

Connect to the server and create the user

After successful mysql-server installation, let’s connect to the server and create a WordPress user.

Connect to mysql server supplying the password we supplied earlier:

$ mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Then create a database, a user and a password:

mysql> create database wordpress_db;
Query OK, 1 row affected (0.01 sec)

mysql> create user 'wordpress_user'@'%' identified by 'S0mStrongPa$$word';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on wordpress_db.* to 'wordpress_user'@'%';
Query OK, 0 rows affected (0.00 sec)

3. Install the Nginx Web Server

Nginx will not work if Apache is installed and running. Since we will be using Nginx, let’s use this command to remove apache if it is installed in our system:

$ sudo apt remove apache2 -y

To install Nginx:

sudo dnf -y install nginx

Once the installation is completed, start Nginx and enable it to start automatically after a reboot

systemctl start nginx
systemctl enable nginx

Check Nginx status:

$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-09-28 16:28:30 UTC; 1min 2s ago
       Docs: man:nginx(8)
   Main PID: 56990 (nginx)
      Tasks: 3 (limit: 4710)
     Memory: 3.7M
     CGroup: /system.slice/nginx.service
             ├─56990 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─56991 nginx: worker process
             └─56992 nginx: worker process

Sep 28 16:28:30 ip-172-26-11-229 systemd[1]: Starting A high performance web server and a reverse pr>
Sep 28 16:28:30 ip-172-26-11-229 systemd[1]: Started A high performance web server and a reverse pro>

To confirm that Nginx is actually running and is reachable, use this command:

curl http://server_ip/

Where server_ip is the ip of your server. If by any reason you do not know the public ip of your server use this to check

curl -4 icanhazip.com

You should see the Nginx default page.

4. Install PHP with dependencies

Since the default repositories contain php version 7.4, let’s install it with this command:

sudo apt install php -y

Confirm the installed version with the command php -v:

~$ php -v
PHP 7.4.3 (cli) (built: Aug 13 2021 05:39:12) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Next we install php dependencies. The php dependencies are available as os level packages prefixed php-. Lets install the dependencies WordPress needs with this command:

sudo apt install -y \
    php-common \
    php-mbstring \
    php-gd \
    php-intl \
    php-xml \
    php-json \
    php-mysqlnd \
    php-fpm

5. Configure PHP and PHP FPM Settings

Global php settings for the version of php installed are stored in the /etc/php/7.4/cli/php.ini file. Let’s update these settings.
Open php.ini with your favourite text editor, I will use vim

sudo vim /etc/php/7.4/cli/php.ini

Then Search for the following variables and update the values like shown below:

post_max_size = 64M
memory_limit = 256M
max_execution_time = 300
upload_max_filesize = 32M
date.timezone = Africa/Nairobi

6. Downloading and Configuring WordPress

Now that we have the servers set up, let’s download and install WordPress on the server. The latest release of WordPress can be downloaded from their official website.
Let’s create a directory where our WordPress content will be serve in this path /var/www/example.citizix.com.

sudo mkdir /var/www/example.citizix.com

Ensure that the web user owns the content:

sudo chown -R www-data.www-data /var/www/example.citizix.com

Download the latest WordPress content:

cd /tmp
curl -LO https://wordpress.org/latest.zip

Sample Output:

$ curl -LO https://wordpress.org/latest.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15.7M  100 15.7M    0     0  6650k      0  0:00:02  0:00:02 --:--:-- 6647k
$ ls
latest.zip

Extract and move the content to our server directory. Ensure you have unzip command or install with sudo apt install unzip.

unzip latest.zip
sudo mv wordpress/* /var/www/example.citizix.com

# Confirm content with
ls /var/www/example.citizix.com

Configuring WordPress

Now that the WordPress content has been copied to the site directory, lets update the db configuration using the sample file provided.
Switch to the site directory:

cd /var/www/example.citizix.com/

Copy your wp-config-sample file to wp-config.php.

cp wp-config-sample.php wp-config.php

Open the config file in a text editor:

vim wp-config.php

Update these vars: DB_NAME, DB_USER, DB_PASSWORD, DB_HOST:

<?php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'xxx' );
/** MySQL database username */
define( 'DB_USER', 'xxx' );
/** MySQL database password */
define( 'DB_PASSWORD', 'xxx' );
/** MySQL hostname */
define( 'DB_HOST', 'xxx' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

Save the text file once that is done.

Make the web user as the owner of the WordPress directory, so that it has access and is able to serve the files.

sudo chown -R www-data:www-data /var/www/example.citizix.com

7. Set up Nginx to Serve wordpress content

Now that the config is in place, let’s configure an Nginx virtual host to serve the content we just added to /var/www/example.citizix.com
Let’s create virtual site in the directory that Nginx serves content here /etc/nginx/conf.d:

sudo vim /etc/nginx/conf.d/example.citizix.com.conf

Add the following content:

server {
    listen 80;

    server_name example.citizix.com;
    root /var/www/example.citizix.com;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Ensure that the added config is good by using this command to test:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once you confirm that everything is OK, restart Nginx for the changes to take effect:

sudo systemctl restart nginx

8. Finally, Test WordPress Installation

Once all steps above are done, the installation from the command line is completed. You can open your favourite web browser and point it to http://example.citizix.com/. You should get the following screen prompting you to get started with WordPress installation set up:

[Screen here]

It should be easy to follow the prompts to set up WordPress for the first time.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment