WordPress on Ubuntu 22.04 LTS

Learn how to set up the leading CMS platform WordPress on the latest Ubuntu 20.04 LTS with LEMP stack.

WordPress on Ubuntu 22.04 LTS
UPDATE: 12/10/23: Updated instructions for 22.04
UPDATE: 21/05/20: added notes about to obtaining SSL using certbot

In this article, We will go through the initial setup process for Installing WordPress, a popular publication and content management system. The process is very straightforward, We will use what's essentially called LEMP Stack which consists of Linux (Ubuntu 22.04 in this case) + NGINX (a High Performance web server and reverse proxy) + MySQL (we will use MariaDB) + PHP (The processor for the language that WordPress is written in)

To get everything in order, we will need a VPS. I recommend Hetzner but this tutorial should work on any VPS as long as you follow the steps correctly.

First, You'll need to ssh into Your VPS server. You can use Terminal app on Windows 10/11 or a ssh client like Putty. If you're running a flavour of linux on your PC then you can also use terminal to accomplish this. If you don't have SSH key set up then your VPS provider will send you an email containing a Username, Password and IP address of your VPS. You'll need to enter those as following: ssh <user>@<IP>

> ssh [email protected]
> Using username "root".
> [email protected]'s password:

SSH into Server

When the system prompts, type in (or copy-paste) the password for the user (in our case, we got root user. Once you enter the password, You'll be greeted with a screen like below and You'll perform the first-steps (i.e. updating the system to latest)

root@server:~# sudo apt update; apt upgrade -y

First Steps: Update the Server to latest

Upon entering the update & upgrade command, the system will print some lines in console, once again, upon completion of update you'll get root@server:~#

Step 1: Install all the Packages

sudo apt install -y nginx mariadb-server php-fpm php-gd php-mysql php-curl php-xmlrpc php-zip php-mbstring php-imagick

Install nginx: sudo apt install nginx

The above one-liner will install all the required items (php, nginx, mariadb) on your ubuntu 22.04 server. Now, let's configure everything.

Step 2: Configuration

First, configure NGINX

root@server:~# sudo nano /etc/nginx/sites-available/default

Edit the Nginx Configuration using nano

This will Open the default configuration file in nano. Add the below server block at the end of the file.

server {
    
    listen 80;
    listen [::]:80;
    root /var/www/html;
    
    server_name example.com; # <-- Change this to Your domain name
    
    location / {
    	try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php-fpm.sock;
     }

     location ~ /\.ht {
             deny all;   
     }

}

Server Block for PHP (via Sockets)

Next, Set up mariadb-server:

root@server:~# mysql_secure_configuration

Secure the mariadb server to only trusted connections

After entering the above command, the system will ask you a few questions, Make sure to read them and answer them correctly. Generally, it is safe to set up a super strong password and then answer rest of the questions as Y (or just hit enter ↵)

root@server:~# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 521
Server version: 10.3.22-MariaDB-1ubuntu1 Ubuntu 20.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)]>

Enter the MySQL Shell

MariaDB [(none)]> CREATE DATABASE wordpress_prod;
Query OK, 1 rows affected (0.000 sec)

MariaDB [(none)]> GRANT ALL ON wordpress_prod.* TO wp_dbuser@localhost IDENTIFIED BY 'VeryS3curePa$$w0rd';
Query OK, 0 rows affected (0.000 sec)

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

MariaDB [(none)]> exit;
bye

Creating a database for WordPress, take a note of database name, username and password.

Now that we've done nginx & mysql, our server is ready for installation of WordPress. However, there are a few optional steps that You'd want to do:

  1. If You want to tweak php settings (execution time etc.) Edit the file /etc/php/8.2/fpm/php.ini using nano. Once You're done, make sure You restart php using sudo service php-fpm restart
  2. You'd also want SSL, If You don't already have a SSL certificate, Use certbot for a free, automatic and simple SSL certificate. follow this tutorial.
  3. You'd also want to secure Your server through firewall. Ubuntu comes with UFW preinstalled, To configure UFW, read our other article.

Now that We've done most of it, Let's do the honor of installing WordPress (Finally!)

root@server:~# cd /var/www/html
root@server:/var/www/html# wget https://wordpress.org/latest.tar.gz -P /tmp

root@server:/var/www/html# tar -xf /tmp/latest.tar.gz
root@server:/var/www/html# mv -v wordpress/* /var/www/html
root@server:/var/www/html# chown -R www-data:www-data /var/www/html

root@server:/var/www/html# sudo service nginx restart && service php-fpm restart

Download and Configure WordPress

The above commands will download wordpress archive from wordpress.org, decompress them and move files to /var/www/html then set correct ownership for the files.

At this point, everything is done. You can visit Your domain e.g. http://example.com (if You got SSL, use HTTPS) and proceed with the WordPress instructions. It'll ask You to enter your database details so copy them from the notes You've taken. Then it'll ask some details about site. Once that's done, it'll take you to the page to log in to your WordPress admin. That's all.