Wordpress on Nginx Server

Page content

How to Install and Run Wordpress on Nginx Server

Nginx is a good alternative to Apache and can be used to run Wordpress. Diue to its low hardware requirements, Nginx is considered a good alternative to Apache since they can handle a lot of traffic and because of their low memory needs. You can easily Install Nginx server of Ubuntu 22.04 and then install and run Wordpress on it.

In this tutorial, we will show you how to run Wordpress on the Nginx server. As a part of the complete Installation, we will install:

  • Nginx server

  • Wordpress

  • Mysql database

  • Php 8.1

Please note that there are several things that are different about the Nginx server. It can provide faster performance compared to Apache server. However, you cannot use a .htaccess file on nginx server. So, there are several types of changes that you could easily make inside the .htaccess file which will need to be changed inside server configurations like enabling gzip and other configurations. Otherwise, if you can edit server configurations, Nginx can be highly suited for running Wordpress in terms of performance.

Installing Wordpress does not take very long. However, before installing Wordpress, we will need to install the Nginx server and also create a database for the Wordpress setup to hold all content. Apart from that, we will need to install php. We can start by installing the Nginx server.

Install Nginx server on Ubuntu 22.04

You need ssh access to your virtual machine or instance. It is easy to get started using an Amazon Ec2 Instance or a DigitalOcean Droplet. Create an instance and then ssh to it. We will first run the updates before installing the Nginx server. Once you have connected to your server, run the following commands from the terminal:

sudo apt update
sudo apt upgrade

It will take only a few minutes to update and upgrade, following which you can install the Nginx server.

To install the Nginx server, run the following command:

sudo apt install nginx -y

This will install the Nginx server on your instance. We can verify that Nginx has been installed and is running using the following command:

Sudo systemctl status nginx

If you get an output like the following, it means Nginx server is active and running:

$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-05-27 04:09:57 UTC; 12s ago
       Docs: man:nginx(8)
    Process: 7445 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 7447 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 7448 (nginx)
      Tasks: 2 (limit: 1130)
     Memory: 1.7M (peak: 1.9M)
        CPU: 12ms
     CGroup: /system.slice/nginx.service
             ├─7448 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─7449 "nginx: worker process"

May 27 04:09:57 ip-172-31-23-122 systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
May 27 04:09:57 ip-172-31-23-122 systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.

Here are a few commands to start, enable, restart and reload nginx server.

Restart Nginx server:

Sudo systemctl restart nginx

Reload Nginx server:

sudo systemctl reload nginx

To stop Nginx server:

sudo systemctl stop nginx

To enable nginx:

sudo systemctl enable nginx

Install Mysql database and create database user

Now that the Nginx server is installed and running, we can move on to the next steps which include installing a mysql database and creating a user for the database. You can use either mysql or mariadb since both work well with Wordpress. We are going to install mysql in this tutorial and create a mysql database and user.

To install mysql:

sudo apt install mysql-server -y

Now, check the status of mysql server to ensure it is active and running:

sudo systemctl status mysql

You will receive an output like the following that shows mysql server is active and running:

$ sudo systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-05-27 04:17:29 UTC; 18s ago
    Process: 8217 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 8226 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1130)
     Memory: 353.7M (peak: 379.9M)
        CPU: 908ms
     CGroup: /system.slice/mysql.service
             └─8226 /usr/sbin/mysqld

May 27 04:17:28 ip-172-31-23-122 systemd[1]: Starting mysql.service - MySQL Community Server...
May 27 04:17:29 ip-172-31-23-122 systemd[1]: Started mysql.service - MySQL Community Server.

Otherwise, you can start the mysql-server with the following command”

sudo systemctl start mysql

To restart mysql:

sudo systemctl restart mysql

To stop mysql:

sudo systemctl stop mysql

We have installed the mysql server but we will also need to create a database and a database user for running Wordpress. All the wordpress content is stored inside the msysql server database. We need to login to mysql to create the database and user.

sudo mysql -u root -p

Hit enter when asked for password.

CREATE DATABASE Wordpressdb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

We have created a database named Wordpressdb for our Wordpress site. You will receive an output like the following:

Query OK, 1 row affected (0.01 sec)

Now, we will create a user and grant him all the privileges:

CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'strong_password';

Replace password with a strong password. Note the username and password as well as the name of the database, which will be required for the final Wordpress installation.

Now, we need to grant all privileges on the Wordpressdb to the user we created:

GRANT ALL PRIVILEGES ON Wordpressdb.* TO 'dbuser'@'localhost';

Following that:

FLUSH PRIVILEGES;

And

EXIT;

We have exited the mysql server after having created the database and user.

Install PHP

In the previous steps, we have successfully installed Nginx server and mysql server. However, we will need to install php before we can install Wordpress on our server. Install php with the following command:

sudo apt install php-fpm -y

We have successfully installed php and can verify the installation:

sudo systemctl status php8.3-fpm

You will receive an output like the following showing Php8.3 is active:

$ sudo systemctl status php8.3-fpm
● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-05-27 04:32:26 UTC; 54s ago
       Docs: man:php-fpm8.3(8)
    Process: 14761 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.3/fpm/pool.d/www.conf 83 (>
   Main PID: 14758 (php-fpm8.3)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1130)
     Memory: 7.6M (peak: 8.9M)
        CPU: 55ms
     CGroup: /system.slice/php8.3-fpm.service
             ├─14758 "php-fpm: master process (/etc/php/8.3/fpm/php-fpm.conf)"
             ├─14759 "php-fpm: pool www"
             └─14760 "php-fpm: pool www"

May 27 04:32:26 ip-172-31-23-122 systemd[1]: Starting php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager...
May 27 04:32:26 ip-172-31-23-122 systemd[1]: Started php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager.

We must all install the php-mysql extension to make sure that php can work with mysql:

sudo apt install php-mysql -y

Now, we can move on to the final Wordpress installation.

Download and install Wordpress

Now comes the time to download and install Wordpress. The default root directory is /var/www/html. (When we download wordpress into this directory, and extract its contents, it will create a new directory inside it called ‘Wordpress’ which will act as the root directory for our wordpress website)

cd /var/www/html

Now we can download and install Wordpress:

sudo wget https://wordpress.org/latest.tar.gz

Now, we need to unzip the files we downloaded:

sudo tar -xvzf latest.tar.gz

This will unzip all the contents of the zip file we downloaded to a folder named Wordpress (which will act as the root folder for our Wordpress website).

Now, give nginx appropriate permissions so that it can write to the root folder:

sudo chown -R $USER:$USER /var/www/html/wordpress

At this stage, we first need to configure the wp-config.php file before running the final wordpress installation on our server. The wp-config.php is an important file containing all the necessary wordpress configurations including the database user name and password required by Wordpress for connecting to the mysql database. The Wordpress download includes a sample wp-confog.php which we can copy and use.

cd /var/www/html/wordpress
sudo cp wp-config-sample.php wp-config.php

If you cannot find the wp-config-sample.php in your installation, you can copy its contents from here and paste them into wp-config.php: https://github.com/WordPress/WordPress/blob/master/wp-config-sample.php

sudo nano wp-config.php

Change the values for database name, username and password. The DB_Host is localhost and you can keep the default host. Replace the database name, user and password with the ones you created previously.

Now, go to https://api.wordpress.org/secret-key/1.1/salt/

Copy all the salt values from here and scroll down inside your wp-config.php file. Replace the keys and save and exit the file. Our preinstallation set up for Wordpress is nearly complete and we need to set up our Nginx server so we can connect to the domain using the url.

Setup Nginx Server

Just like we have a virtual hosts file in Apache server, we will need to create a server configuration on Nginx server to run our Wordpress website.

We will first create a new configuration file for our website:

sudo nano /etc/nginx/sites-available/example.com.conf

Now, add the following content to this file:


server {
    listen 80;
    root /var/www/html/example/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;
    client_max_body_size 500M;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

Make appropriate changes to the above code like adding the right root path and replacing the domain name in server_name with your domain.

Create a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

Now, you must test the nginx configuration:

sudo nginx -t
``
You will receive an output like the following indicating the configuration is ok.

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

We have made changes to the server configuration and therefore, we will need to restart it once. 

sudo systemctl restart nginx ``

Install Free SSL (Optional)

We have set up our NGINX server and we are ready to complete the setup of our Wordpress website. However, you might want to access your website url using https. If you want to install free ssl before proceeding, you can do that first:

Install certbot on Nginx:

sudo apt install certbot python3-certbot-nginx -y

Before installing the certificate, make sure that you have setup your DNS and created the A records pointing to your public ip address.

Now, install the free ssl on your nginx server with the following command:

sudo certbot --nginx -d example.com -d www.example.com

Setup Wordpress

Wordpress installation

Now, we are all set up to start designing our wordpress website. Just go the site url and you will see the welcome screen. Enter the website title and create a user name and password for wordpress admin role which you will use to login to your Wordpress installation. Save the username and password in a safe place.