Run wordpress on Nginx server

How to run wordpress on Nginx (Mariadb databse) on  AWS EC2 Ubuntu instance

Nginx is an open-source webserver that powers more than 400 million websites. It is also used as a load balancer and reverse proxy. Compared to the Apache server, it can be more efficient and faster. This tutorial will show you how to install wordpress on an Nginx webserver. We are going to run wordpress on Nginx webserver using an AWS EC2 instance.

Let’s first start with instance creation. I am using an AWS EC2 instance with Ubuntu 22.04. To create the instance just go to your EC2 dashboard in the AWS console and launch the instance of desired size. Select Ubuntu for AMI and then add keypair and security group. Increase the volume of disk according to your choice. Increase it to at least 20gb or more.

After having created the EC2 instance, SSH to the instance and run the necessary updates using:

$ sudo apt update -y
$ sudo apt upgrade -y

Install Nginx webserver on Ubuntu 22.04

Nginx installation does not take much time and with just a few commands, you will have Nginx installed on your instance.  To install NGINX on your EC2 instance,  run the following command:

$sudo apt install nginx

The installation takes only a few minutes. You can use the following commands to start, stop, reload and restart NGINX web server:

Start: $ sudo systemctl start nginx

Stop: $ sudo systemctl stop nginx

Reload: $ sudo systemctl reload nginx

Restart: $ sudo systemctl restart nginx

Check Nginx status after the web server installation:-

$ sudo systemctl status nginx

When the nginx server is active and running on your instance, the output looks like the following:

  • 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 Mon 2023-03-20 10:32:56 UTC; 1h 33min ago

    Docs: man:nginx(8)

Process: 17474 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)

   Main PID: 15964 (nginx)

   Tasks: 3 (limit: 4689)

  Memory: 6.5M

     CPU: 305ms

  CGroup: /system.slice/nginx.service

          ├─15964 “nginx: master process /usr/sbin/nginx -g daemon on; master_process on;”

          ├─17494 “nginx: worker process” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “”

          └─17495 “nginx: worker process” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “” “”

Mar 20 10:32:56 ip-172-31-23-95 systemd[1]: Starting A high performance web server and a reverse proxy server…

To start nginx and enable it to start at every system boot, run :

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

When you enable nginx webserver, it will provide output like the following:

Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.

Executing: /lib/systemd/systemd-sysv-install enable nginx

Now, allow Nginx through the firewall.

$ sudo ufw app list

It will output the list of available applications:

Available applications:

  Nginx Full

  Nginx HTTP

  Nginx HTTPS

  OpenSSH

Run the following command to allow nginx through the firewall:

$ sudo ufw allow ‘Nginx Full’

Check Nginx status using:

$ sudo systemctl status nginx

Copy your public ip and paste it into your browser You will see the nginx welcome page showing NGINX server has been successfully installed. As a part of out wordpress installation, we need to install mariadb (or mysql) database server and php8.1. However, first we will configure the nginx server to serve content. For this purpose, we will need to set up the server block for our wordpress site in nginx.  We also need to create a directory from where we will run wordpress.

Create directory /var/www/domain_name/html

$ sudo mkdir -p /var/www/domain_name/html

Assign ownership to the user –

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

The directory permissions need to be set at 755 to allow read, write and execute permissions to the owner and read and execute permissions to group and others.

$ sudo chmod -R 755 /var/www/domain_name

If you want, you can check by uploading an index.html page to see how a page loads on your server.  Add an index.html file to your root foder.

$ sudo nano /var/www/domain_name/html/index.html

And paste the following  content to it:

<html>  

 <head>    

<title>Welcome to my_blog!</title>    

</head>    

<body>    

<h1>It’s running on NGINX webserver. Thanks for visiting!</h1>  

 </body>

</html>

Now, when you visit the public ip of your instance, you will see the above content.

Just as we have vhost files in Apache web server, there are server blocks in NGINX server with the directives to serve content.

You can create a new one instead of modifying the default configuration file by adding the new file at

/etc/nginx/conf.d/domain.com.conf

Or you can set it inside the directory /etc/nginx/sites-available

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

[ Note: If you look inside your main Nginx configuration file located at /etc/nginx/nginx.conf, you will find these directives at the bottom:

include /etc/nginx/conf.d/*.conf;

include /etc/nginx/sites-enabled/*; ]

We can add the server block inside the sites-available directory and then link it from sites-enabled directory.

Now add the server block to this file with the following configurations (paste the content below into the file) :

server {

     listen 80;

     listen [::]:80;

     root /var/www/your_domain/html;

     index index.html index.php index.htm index.nginx-debian.html;

     server_name example.com www.example.com;

     location / {

            try_files $uri $uri/ /index.php?$args;

     }

}  

Save the file and exit. Ctr + X, Y and enter.

When using the Apache server, you have a ServerName and a ServerAlias in the vhost file. In the NGINX server block, you can add both to the ServerName separated by a space. Check the document root in the above server block configuration and update it to your document root and replace example.com with your domain name.

Nginx reads from the sites-enabled directory at startup so we can link the server block configuration file to it with the following command:

$ sudo ln -s /etc/nginx/sites-available/domain_name  /etc/nginx/sites-enabled

Now, check Nginx configuration:

$ 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 also need to install php8.1 and php 8.1-fpm. Install php8.1, php8.1-fpm and php8.1-mysql extension.

$ sudo apt install php8.1 php8.1-fpm php8.1-mysql

Note: If you have not already added a new user to your server, you can add it using the adduser command:

 $ sudo adduser user_name

In the above command, you can replace user_name with the username you want to add. Add a password for the user and then add it to the sudo group:

$ sudo usermod -aG sudo user_name

Replace  user_name with the username you just created. Later, you can also use this user to SFTP to your instance. To use your account as the new user and not the root user, just run the following command:

$ su username

Suppose, you have added the user Sammy, you can switch from root user to Sammy with:

$ su sammy  

Install the Mariadb database server:

You need a database to store wordpress files including posts, user information, comments and other information. Mariadb is a fast database server. You can install it using the following command:

$ sudo apt install mariadb-server

During the installation process, you will be asked if you want to change root password, you can safely answer no. When prompted if you want to remove remote login and the test database, answer yes. Once the mariadb installation is complete, you can check its status using:

$ sudo systemctl status mariadb

Login to mariadb:

$ mysql -u root -p

Create a new database for wordpress named WordPressdb with the following command:

MariaDB [(none)]> $ CREATE DATABASE WordPressdb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Output: Query OK, 1 row affected (0.000 sec)

Assign database permissions to a database user named Dbuser:- 

$ GRANT ALL ON WordPressdb.* TO Dbuser @'localhost' IDENTIFIED BY 'strong_password'; 

In the above command replace WordPressdb with the database you created and Dbuser with the database user you want to create and then add a strong password. 

Following that: 

$ FLUSH PRIVILEGES;

So that changes may take effect and then:

$ EXIT

 We have our Nginx server ready to host wordpress and also the database that wordpress will connect to. We can move on to installing wordpress latest version. Change from root user to the other user with sudo permissions. 

Download and Install wordpress: 

To install wordpress, we first need to download the latest version of wordpress and extract the files. One particular thing to note here is that, we will not download wordpress installation files directly to the root folder. Instead, we can download them to a different folder and extract files from the package and move files to the root folder /var/www/domain_name/html.

The latest version of wordpress is always available at the following link: https://wordpress.org/latest.tar.gz.

You can download it from here using wget or curl. Now, we are going to download and extract the latest wordpress package to the /tmp directory.

$cd /tmp
Now, you are inside the directory /tmp.

Download the latest version of wordpress :

$ wget ;

Extract the files:

$ tar xf latest.tar.gz

Now, that you have extracted the files, you can just move them to the root folder:

$ sudo mv /tmp/wordpress/* /var/www/domain_name/html 

The web server requires full access to the files and so you will need to change permissions for user www-data. If you check in your NGINX configuration file located at /etc/nginx/nginx.conf, you will see that the user is www-data (nginx and php run as user and group www-data). Change permissions using the following command:

$ sudo chown -R www-data:www-data /path/to/root folder

In the above case, we have set our root folder as /var/www/domain_name/html. So, the above command will look like this:

$ sudo chown -R www-data:www-data /var/www/domain_name/html

Now, most of your work is complete and you can move towards the final installation of wordpress. First add the DNS records for your blog. You will need to create the A records for both www and non www version and point them to the public ip. Initially, use a low TTL like 300 seconds.

To start the final installation of wordpress, enter your blog’s domain name in the browser like http://www.example.com.

We have not assigned a ssl certificate yet and so the site will be available on http only. Now, when you will load your domain for the first time, you will be directed to the wordpress set up and configuration page.

First, you will be prompted to select the language. The default language is en-us and if you are going to run a blog in English, you can continue with the default selection. On the next page, you will be asked to provide a title for your blog, and add a user and password for login into wordpress dashboard. You will also need to add an admin email. (In this stage, you set your wordpress admin credentials)

We did not set up the wp-config.php page for our wordpress initially. However, it will be done automatically with the final installation. Once you have completed creating the admin user, click on continue and then on the next page, you will asked to provide the details that will be filled into the wp-config.php file.The wp config file is the wordpress configuration file that contains the database credentials, wordpress needs to connect to the database. If the credentials are wrong, wordpress will not connect to the database and you will get a database connection error.

In this step, you will fill the database name, database user and database password you have created. You can add a prefix you want to use with your database tables or continue with the default wp_ prefix. You need to fill in the correct database and user names and the password you created when assigning permissions to the database user. If you have filled correctly, wordpress will connect to the database and you will get a congratulations message. Click on run the installation, following which you will be directed to the wordpress login page.

You can now login to your wordpress admin dashboard using the new admin user and password you created.

After you are logged in, add themes, plugins and content to your wordpress installation. You can also change the permalink structure for wordpress to use post name in the permalink by going to settings and then permalinks.

Since, we have installed the Nginx server, we cannot use .htaccess. However, you can enable the use of gzip on Nginx by adding gzip directives in the nginx main configuration file.

 Set gzip on; and uncomment the rest gzip related directives in the configuration file located at /etc/nginx/nginx.conf.

SSL for your wordpress site on nginx server:

Since, you cannot run your site without https, you will need to add ssl certificates.To add ssl to your website, you only need to install and run certbot for nginx:

$ sudo apt install certbot
$ sudo apt install python3-cerbot-nginx 

Now, reload nginx after installing certbot.

$sudo systemctl reload nginx 

Finally, run certbot to install ssl on your blog:

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

Enter your email and consent to the terms of service. Certbot will install ssl and also create the redirect to https inside the server block of your configuration file.Now check out your wordpress blog in the browser and see it loading securely on https.

Optional: Install phpMyAdmin

If you want to take a look inside the database and check out the tables, you will need to install phpMyAdmin for it and optionally you can also install the wp-cli to manage your wordpress installation and database.

$ sudo apt install phpMyAdmin

While installing phpMyAdmin, you will be prompted to select one of the two servers, apache and lighttpd. Since you are using neither of the two servers, you must use the Tab button to highlight Ok and then click enter. Next, the system will prompt you to select if it can use dbconfig-common to configure the application database. Click on OK and then in the next step select yes. The system will prompt you to add a password for phpMyAdmin. However, you can leave it blank and the system will generate a random password. You do not need this password since you will login using the credentials you have set for your database. PHPmyadmin is installed now. However, we need to create a symbolic link from the installation files to the root folder so that Nginx can serve the phpMyAdmin files correctly. If you remember, we have set our root folder as /var/www/domain_name/html

Create the symbolic link using the following command:

$ sudo ln -s /usr/share/phpmyadmin /var/www/aim-blog/html/phpMyAdmin

Now, to open the phpMyAdmin page, you need to add /phpMyAdmin at the end of your domain name:

https://www.example.com/phpmyadmin

Use the credentials you have created for the database. You entered that information during the final installation for setting up the wp-config.php file automatically. If you have not saved this information, go to the wp-config.php file located inside your root folder to find out. When you login using the username and password, you will see the database you created listed in the left sidebar and more information related to your database server and web server on the right. I have created a database named WordPressdb

Click on your database name to check out the tables or on the + sign to expand it. Go to Wp_options table and at the top, you will see the site url and home url. Change them from http to https.

At the end:

Now, you have set everything that you would need to run a wordpress blog. Add a little content and try loading and reloading pages to check the speed. With Nginx webserver and mariadb database, you will find that your wordpress site is loading very fast.  You can enable gzip and brotli for more speed and also add a cdn.

You can configure your Nginx server for wordpress and have a very fast server with fastcgi cache enabled.

You can get detailed information about configuring nginx and FastCGI cache for wordpress in this article.

If you install and configure the WP-CLI, you can use it for many things like updating themes, plugins and core wordpress as well as installing and uninstalling themes and plugins and for many more tasks including creating new posts. You can also use the wp-cli for maintaining your database and optimizing and repairing tables.