WordPress Nginx: Everything You Need to Know About Installing WordPress on Ubuntu

Nginx is an open-source web server that was first released in 2004 and is written completely in the C programming language. It has many benefits that you’ll discover right here! In this tutorial, we’ll show you how to install WordPress with Nginx!

Download all in one WordPress cheat sheet

Why Use Nginx With WordPress

Nginx has gained immense popularity in the recent past and is commonly sought as an alternative to the Apache web server. Nginx supports reverse proxy, caching, media streaming, load balancing, and much more. That makes it a great fit for a WordPress website powered by a VPS solution.

Few of Nginx’s inbuilt features are:

  • Nginx is built to work on low memory usage
  • It can support extremely high concurrency
  • Is Ipv6 enabled
  • Supports reverse proxy with efficient caching
  • Provides an inbuilt load balancer
  • Supports WebSockets
  • Optimized handling of index files, static files and provides auto indexing
  • Is accompanied with FastCGI for efficient caching

Nginx does much more than a conventional web server, which is one of the reasons it has gained so much popularity. Nginx overshadows a lot of legacy web servers and consistently provides benchmarks surpassing their performance.

Nginx solves a lot of scalability issues and is taken as a solution to the commonly referred C10K problem related to concurrency.

And as you now know, Nginx and WordPress work really well together!

Here, we will walk you through the installation of WordPress using Nginx on the Linux platform.

Similar to LAMP, using Nginx is referred to as LEMP which stands for Linux, Nginx, MySQL/MariaDB, and PHP.

Prerequisites for Installing WordPress With Nginx

  1. You are logged with sudo access
  2. Nginx is preinstalled
  3. You have an SSL certificate installed for your domain
  4. You own a domain name pointing to your server’s public IP. In this example it’ll be sample.com

How to Install WordPress with Nginx

Let’s walk through the entire process:

1. Update Your System

Update the package index using:

sudo apt update

Update system packages to the latest version using:

sudo apt upgrade

2. Install Nginx

Nginx packages are available in the default Ubuntu repository. You can use the below command to install them:

sudo apt install nginx

This will take a while to install. Once the installation is complete, the Nginx service will start automatically. To know the status of the service, use the below command:

sudo systemctl status nginx

3. Configure UFW (Optional)

If you are using UFW (Uncomplicated Firewall) to manage your VPS firewall then you will have to open ports 80 and 443 for HTTP and HTTPS respectively. You can enable the Nginx full profile which contains rules for both ports. This can be done using:

sudo ufw allow 'Nginx Full'

To verify the status, you can use:

sudo ufw status

4. Install and Configure MySQL Database

To store data we will be using MySQL. In case you do not have MySQL installed, then you can get it by using:

sudo apt install mysql-server

Once this is complete, the MySQL Database will be started automatically. You can use the below command to check its status:

sudo systemctl status mysql

Next, you can log in to the MySQL shell by using:

mysql -u root -p

This will switch over to the MySQL console, which you can tell by the mysql> at the start of the line. Here you can create a database and a database user with the names WordPress and WordPressUser respectively.

CREATE DATABASE WordPress CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL ON WordPress.* TO WordPressUser @'localhost' IDENTIFIED BY 'your password';
FLUSH PRIVILEGES;
EXIT;

This creates a basic database configuration that can be used for the WordPress setup.

5. Install PHP

You can install all the required PHP extensions directly with a single command since these are the only ones that WordPress will use. This can be done using:

sudo apt install php7.2-cli php7.2-fpm php7.2-mysql php7.2-json php7.2-opcache php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

On completion of this installation, PHP-FPM will start automatically. This is a Fast CGI process manager which enables caching.

6. Install WordPress with Nginx

To Install WordPress with Nginx, first create a directory to download the WordPress archive:

sudo mkdir -p /var/www/html/sample.com

From the official WordPress website, you can download the latest WordPress installs. Download it to the /tmp directory. You can access said directory by using cd, and download the archive using wget:

cd /tmp
wget https://wordpress.org/latest.tar.gz

Next, you can extract this archive to the directory created earlier. This can be done using:

tar xf latest.tar.gz
sudo mv /tmp/wordpress/* /var/www/html/sample.com/

The web server will require complete access to these files. Change the permission by running the chown command:

sudo chown -R www-data: /var/www/html/sample.com

Note that Nginx and PHP run as the www-data user and group, hence this is used in the above command.

7. Configure Nginx for WordPress

To configure Nginx for WordPress, we have to create a new server block for our WordPress installation. Navigate to /etc/nginx/sites-available. There, create a file with the name sample.com. The name should be the same as your domain.

Add this code to the newly created file:

# Redirect HTTP -> HTTPS
server {
listen 80;
server_name www.sample.com sample.com;

include snippets/letsencrypt.conf;
return 301 https://sample.com$request_uri;
}

# Redirect WWW -> NON-WWW
server {
listen 443 ssl http2;
server_name www.sample.com;

ssl_certificate /etc/letsencrypt/live/sample.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sample.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/sample.com/chain.pem;
include snippets/ssl.conf;

return 301 https://sample.com$request_uri;
}

server {
listen 443 ssl http2;
server_name sample.com;

root /var/www/html/sample.com;
index index.php;

# SSL parameters
ssl_certificate /etc/letsencrypt/live/sample.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sample.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/sample.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;

# log files
access_log /var/log/nginx/sample.com.access.log;
error_log /var/log/nginx/sample.com.error.log;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

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

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

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}

Make sure to use the correct domain name. For easier management, create a symbolic link to the sites-enabled directory.

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

You can validate the Nginx configuration by using:

sudo nginx -t

If this has no errors then you will see a message showing syntax is ok. You can ignore the rest of the message.

Next, you can restart Nginx by using:

sudo systemctl restart nginx

At this stage you have PHP, MySQL and Nginx configured and started. Next, configure and verify the WordPress installation.

8. Configuring WordPress for Nginx

Nearly done! The last step is configuring your very own WordPress!

Open your browser and type the domain name as you would http://sample.com. Replace this with your domain!

You will be able to see a screen showing language selection. Pick your preferred option.

You will see an instruction page that you can read and proceed to the next page. On the next screen, you can configure your Database details.

Provide your database name along with the username and password. In our case, this will be WordPress for the database name and WordPressUser for Username.

You can start the installation by clicking the button.

In the next page, you can provide additional details. At this step, you will set the username for WordPress.

For security, you should change the username from admin to something else. Click on the Install WordPress button. This will redirect you to the login page where you can type in your newly configured WordPress username and password.

Once logged in, you will be able to see the WordPress dashboard.

From here you can configure your WordPress, set new themes, add plugins and more.

Sugested Reading

Discover our guide to learn how to install WordPress on Ubuntu using LAMP Stack.

Conclusion

Through this tutorial, we have learned how you can install WordPress using Nginx on Ubuntu. Bingo, you just configured a full setup of the most popular CMS. Go ahead and have fun exploring the power of WordPress with the additional features of a powerful virtual private server.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.