How to set up wordpress in Ubuntu 22.04 with Mariadb and Nginx

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 22.04 with Mariadb as a database with Nginx and php-fpm to serve wordpress.

Related Content:

# Prerequisites

To follow along this guide, we need the following:

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

# Step 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 and 2. Upgrade server packages to the latest releases:

# Update repos
sudo apt update

# Upgrade packages
sudo apt -y upgrade

# Step 2 – Installing and setting up Mariadb server

We are going to use Mariadb as a database for our WordPress installation. Mariadb is found in the default repos for Ubuntu 22.04. Use this command to install the database server:

sudo apt install -y mariadb-server

Mariadb will be started by default. Confirm the status using this command:

$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-08-25 13:53:30 UTC; 24s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 14618 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 14620 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 14626 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _W>
    Process: 14670 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 14672 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 14655 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 13 (limit: 2311)
     Memory: 57.1M
        CPU: 377ms
     CGroup: /system.slice/mariadb.service
             └─14655 /usr/sbin/mariadbd

Aug 25 13:53:30 ip-10-2-7-101 mariadbd[14655]: 2022-08-25 13:53:30 0 [Note] /usr/sbin/mariadbd: ready for connections.
Aug 25 13:53:30 ip-10-2-7-101 mariadbd[14655]: Version: '10.6.7-MariaDB-2ubuntu1.1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubuntu 22.04
Aug 25 13:53:30 ip-10-2-7-101 systemd[1]: Started MariaDB 10.6.7 database server.
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14674]: Upgrading MySQL tables if necessary.
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14677]: Looking for 'mysql' as: /usr/bin/mysql
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14677]: Looking for 'mysqlcheck' as: /usr/bin/mysqlcheck
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14677]: This installation of MariaDB is already upgraded to 10.6.7-MariaDB.
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14677]: There is no need to run mysql_upgrade again for 10.6.7-MariaDB.
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14677]: You can use --force if you still want to run mysql_upgrade
Aug 25 13:53:30 ip-10-2-7-101 /etc/mysql/debian-start[14689]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables

# Configuring MariaDB

For new MariaDB installations, the next step is to run the included security script. This script changes some of the less secure default options. We will use it to block remote root logins and to remove unused database users.

Run the security script:

sudo mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MariaDB installation’s security options. This is my server’s output:

$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n]
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

# 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 Mariadb server supplying the password we supplied earlier:

$ mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.6.7-MariaDB-2ubuntu1.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

Then create a database, a user and a password:

MariaDB [(none)]> create database wordpress_db;
Query OK, 1 row affected (0.01 sec)

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

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

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

# Step 3 – Install and configure php and its dependencies

We are going to use the default PHP provided by the default ubuntu repositories. As at the writting of this guide, the version installed is 8. Use this command to install:

sudo apt install php

Confirm the installed version with the command php -v:

$ php -v
PHP 8.1.2 (cli) (built: Aug 15 2022 12:24:10) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, 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

# Configure PHP and PHP FPM Settings

Global php settings for the version of php installed are stored in the /etc/php/8.1/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/8.1/cli/php.ini

Then Search for the following variables and update the values like shown below. Be free to change the values to suit your needs.

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

# Step 4 – Download wordpress

Now that we have the servers set up, let us 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/citizix.

sudo mkdir /var/www/citizix

Ensure that the web user owns the content:

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

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/citizix

# Confirm content with
ls /var/www/citizix

# 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/citizix/

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.

# Step 5 – Install Nginx and configure it to serve wordpress

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 apt 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 Thu 2022-08-25 14:32:18 UTC; 22s ago
       Docs: man:nginx(8)
   Main PID: 28062 (nginx)
      Tasks: 3 (limit: 2311)
     Memory: 4.2M
        CPU: 27ms
     CGroup: /system.slice/nginx.service
             ├─28062 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─28064 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─28065 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Aug 25 14:32:18 website systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 25 14:32:18 website systemd[1]: Started A high performance web server and a reverse proxy server.

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.

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

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

Add the following content:

server {
    listen 80;

    server_name example.citizix.com;
    root /var/www/citizix;
    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/php-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

# Step 6 – 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 the domain you set up, in my case http://example.citizix.com/. You should get the a screen prompting you to get started with WordPress installation set up.

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

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy