How to install WordPress using Nginx on Ubuntu 18.04

Nginx is an open source web server, first released in 2004, written entirely in the . This server has many benefits that you can discover in this tutorial, in which we will show you how to install WordPress with Nginx.

Why use Nginx with WordPress?

has recently gained immense popularity and is commonly used as an alternative to . Nginx supports reverse proxying, caching, media streaming, load balancing, and much more. All of this makes it ideal for running a WordPress website powered by a .

We mention some of the outstanding features of Nginx:

  • It is a server designed to work with little memory usage.
  • It can support extremely high attendance.
  • It has IPv6 enabled.
  • Support reverse proxy with efficient caching.
  • Provides a built-in load balancer.
  • Supports WebSockets.
  • It optimally handles index files and static files and provides automatic indexing.
  • It is accompanied with FastCGI for efficient caching.

Nginx is much more than a conventional web server, which is one of the reasons why it has become so popular. Nginx outshines many legacy web servers and consistently provides benchmarks that outperform them.

This server solves many scalability issues and is taken as a solution to the C10K problem commonly related to a large number of visitors.

And as you already know, Nginx and WordPress work great together!

In this post, we will walk you through installing WordPress using Nginx on the Linux platform.

Similar to , the use of Nginx is known as LEMP, which stands for Linux, Nginx, MySQL/MariaDB, and PHP.

Prerequisites for installing WordPress using Nginx

  1. Be logged in with sudo access
  2. Have Nginx pre-installed
  3. Have one installed for your domain
  4. Have one that points to the public IP of your server. In this example we will use example.com

How to install WordPress with Nginx

Let’s see the whole 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 following command to install them:

See also  How to create a free web page? - Explanatory video

sudo apt install nginx

Installation will take a while. Once the installation is complete, the Nginx service will start automatically. To find out the status of the service, use the following command:

sudo systemctl status nginx

3. (Optional) Configure UFW

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

sudo ufw allow ‘Nginx Full’

To check the status, you can use:

sudo ufw status

4. Install and configure the MySQL database

To store data we will use . In case you don’t have MySQL installed, you can get it using:

sudo apt install mysql-server

Once this is complete, the MySQL database will start automatically. You can use the following command to check the status:

sudo systemctl status mysql

You will then be able to login to the MySQL shell using:

mysql -u root -p

This will switch you to the MySQL console. Here you can create a database and a database user with the names WordPress and WordPressUser respectively.

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

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

5. Install PHP

You can install all the required PHP extensions directly, with a single command, as these are the only ones WordPress will use. You can do this 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

Upon completion of this installation, PHP-FPM will start automatically. This is a Fast CGI process manager that allows 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/example.com

From the official WordPress website, you can download the latest WordPress installations. Download it to the /tmp directory. You can access said directory using CD and download the file using wget:

See also  Top 15 Alternatives to WordPress in 2022

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

You can then extract this file to the directory created earlier. This can be done using:

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

The web server will require full access to these files. Change the permission using:

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

You should keep in mind that Nginx and PHP run as the user and group www-datathis is why it is used in the above command.

7. Configure Nginx for WordPress

To configure Nginx for WordPress, you have to create a new server block for your WordPress installation. Navigate to /etc/nginx/sites-available. There, create a file with your domain name, in our example it would be example.com.

Add this code to the newly created file:

# Redirect HTTP -> HTTPS server { listen 80; server_name www.example.com example.com; include snippets/letsencrypt.conf; return 301 https://example.com$request_uri; } # Redirect WWW -> NON-WWW server { listen 443 ssl http2; server_name www.example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; include snippets/ssl.conf; return 301 https://example.com$request_uri; } server { listen 443 ssl http2; server_name example.com; root /var/www/html/example.com; index index.php; # SSL parameters ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; include snippets/ssl.conf; include snippets/letsencrypt.conf; # log files access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.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; } }

Remember, everything that says ‘example.com’ should be replaced with your domain name.

For easier administration, create a symbolic link to the directory sites-enabled.

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

You can validate the Nginx configuration using:

sudo nginx -t

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

You can then restart Nginx using:

sudo systemctl restart nginx

Having reached this point you will have PHP, MySQL and Nginx configured and started. Next, configure and verify your WordPress installation.

See also  is recommended as the best choice for web beginners by experts at HostingAdvice

8. Configure WordPress for Nginx

You’re almost done! The last step is to set up your own WordPress!

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

You will see a screen showing the language selection. Choose your preferred option.

You will see a page of instructions that you can read and skip to the next page. On the next screen, you can configure the details of your Database.

Provide your database name along with username and password. For the example, we use WordPress for the database name and WordPressUser for the username.

You can start the installation by clicking the button.

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

For security, you should change the username from admin to something else. Click 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 you are 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.

To end

Through this tutorial, you have learned how you can install WordPress using Nginx on Ubuntu 18.04. Bingo, you just got a complete setup of one of the most popular CMS. Go ahead and have fun exploring the power of WordPress with the added features of a powerful virtual private server!

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...