Home » DevOps » Install WordPress with Nginx on Ubuntu 22.04

Things you should know about WordPress

WordPress is a content management system (CMS) that is used to create and manage websites. It is open-source software, meaning that it is free to use and customize. WordPress is built on PHP and MySQL, and it allows users to easily create and manage website content through a simple interface. It is the most popular CMS in the world, and it is used by millions of websites, from small personal blogs to large corporate sites. Additionally, WordPress has a large community of developers who contribute to the platform and create themes and plugins to extend its functionality. So in this tutorial, we will walk you through how to Install WordPress with Nginx on Ubuntu 22.04.

Update your Ubuntu 22.04

Before starting the actual setup, we will first update and upgrade our Ubuntu OS.

sudo apt-get update
sudo apt-get upgrade

Here’s what above commands will actually do,

  • The first command will update the installed package/library in the OS
  • The second command will upgrade the available packages/library with the latest version in the marketplace

We recommend you not upgrade OS after installing the required services for an uninterrupted platform experience. Before starting other installations, check your OS’s release version by below command.

# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy

How to Install WordPress with NGINX on Ubuntu 22.04?

Simply follow the below steps to install WordPress with Nginx on your Ubuntu OS

Install Nginx

Nginx (pronounced as “Engine-X”) is an open-source web server that is often used as a reverse proxy or HTTP cache.

sudo apt-get install nginx

Above single line will do everything you need. In addition to that you can check the status of the Nginx service using below command

# sudo systemctl status nginx.service
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 Sat 2023-01-21 12:40:33 UTC; 20h ago
       Docs: man:nginx(8)
    Process: 459 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 522 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 543 (nginx)
      Tasks: 2 (limit: 1143)
     Memory: 4.8M
        CPU: 4.074s
     CGroup: /system.slice/nginx.service
             ├─ 543 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─6489 "nginx: worker process

If you are getting above output as a result, it clearly says that your Nginx service is working fine and as a status, you will see active (running) with PID 543. Make sure you do not have other web servers running inside your Ubuntu OS.

Install MySQL

On Ubuntu 22.04, you can install MySQL using the APT package repository. At the time of this writing, the version of MySQL available in the default Ubuntu repository is version 8.0.28.

To install it, update the package index on your server if you’ve not done so recently and then install MySQL server:

# sudo apt update
# sudo apt install mysql-server 
# sudo systemctl start mysql.service

You can run below command additionally if required
# sudo mysql_secure_installation

By default, the Password for the MariaDB root user is blank. You can simply run below commands in your terminal to update the password.

$ mysql -u root -p
//enter your root password

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> exit;

Create your own password before running above command.
That’s it. You are done with your MariaDB installation in your Ubuntu System.

Install PHP

PHP is a server scripting language and a powerful tool for making dynamic and interactive Web pages. The latest release of PHP (8.1) is readily available in Ubuntu Repository. Run the following command to install PHP 8.1 and its required packages.

sudo apt-get install -y php8.1-fpm php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath

By running above command, We are installing some common packages like MySQL, fpm, zip, curl, mbstring, etc. For your information, php-fpm(FastCGI Process Manager) is required by Nginx to run your WordPress installation or to run your PHP pages. It will automatically get triggered and come in a running state just after installation.

Note that till now we are done with Nginx, MariaDB, and PHP along with common packages. Now before moving to WordPress installation, We will set up a database for our WordPress Application by running below commands in your terminal.

$ mysql -u root -p
Enter password: "ADD YOUR root USER PASSWORD HERE"

MariaDB [mysql]> CREATE DATABASE wordpress_db;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> GRANT ALL ON wordpress_db.* TO 'wpuser'@'localhost' IDENTIFIED BY 'Passw0rd!' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit

We are now ready with Database with the name wordpress_db along with the required permission.

Download WordPress & Configure it with Nginx

The first thing you need to know when working with Nginx is that its default working directory is /var/www/html. So any PHP or WordPress Application you want to serve on your domain, get it inside /var/www/html.

Now time to clone WordPress with the latest available version. Just run the below commands in your terminal.

# cd /var/www/html/
# wget https://wordpress.org/latest.tar.gz
# tar -zxvf latest.tar.gz

It will create a new directory with the name WordPress. You can change it with your project/application name.

# sudo mv wordpress APP_NAME //Replace APP_NAME with your actual project/application name

Let’s configure Nginx for recently cloned WordPress in /var/www/html.

Here we will use our custom Nginx Configuration file which we are going to create inside /etc/nginx/conf.d/ with the name of wordpress.conf.

# sudo touch /etc/nginx/conf.d/wordpress.conf

After creating a custom configuration file using above command, copy and paste below configuration in wordpress.conf.

# Nginx server configuration for WordPress

server {
        listen 80 default_server;
	listen [::]:80 default_server;
        server_name example.com;

	client_max_body_size 256M;
	error_log /var/log/nginx/wordpress_error.log;
        access_log /var/log/nginx/wordpress_access.log;

        root /var/www/html/wordpress/;
        index index.php index.html index.htm index.nginx-debian.html;

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

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

    location ~ /\.ht {
        deny all;
    }

}

server {
    listen 80;
    return 301 https://example.com$request_uri;
}

Now the question is how Nginx will identify our custom config file. It won’t happen automatically, We need to modify the default nginx.conf file.

# sudo nano /etc/nginx/nginx.conf

find for below line and comment it out so that it will not create any conflicts in configuration.
#include /etc/nginx/sites-enabled/*;

If you want to create multiple configuration files, then go to /etc/nginx/conf.d and create n number of config files based on your requirements.

After making all above changes, we have to be very sure that the syntax & configuration are written in wordpress.conf file is correct or not.

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

If you are getting above result then your configuration file is totally fine or else check the syntax and configuration again.

# sudo nginx -s reload

Without testing Nginx Configuration, Do not run the last command, or else your website gets down.

Voila!!! We are done with the installation of WordPress with Nginx on Ubuntu 22.04. Now you can open your server address or domain and you will get the WordPress installation screen.

Thank you for spending your valuable time

If you are reading this, That means you found this article interesting/useful.
Please share this as much as you can so that we can help others by sharing knowledge.

Pin It on Pinterest

Share This