Installing Wordpress on Nginx (Ubuntu tutorial)

Last update: 04-21-2024

Why Nginx for Apache?

Apache is often the go-to choice for deploying WordPress due to its widespread use, ease of configuration, and strong compatibility with WordPress's requirements.

However, Nginx is a viable alternative and offers its own set of advantages. Nginx is known for its high performance, particularly in handling a large number of concurrent connections, which can lead to faster web page loading times. Additionally, Nginx has a reputation for efficient resource usage, which can be beneficial for servers with limited resources.

Its ability to handle static content efficiently and its reverse proxy features also make Nginx a strong contender for WordPress hosting, especially in high-traffic scenarios or for dynamic websites with complex caching requirements.

Updating the package list

We start by updating the package list:

sudo apt update

PHP, MySQL and Nginx installation

Notice that when deploying on Ubuntu using Nginx, `php-fpm` (PHP FastCGI Process Manager) is used instead of the standard PHP module because Nginx does not embed PHP processing within itself.

Unlike Apache, which can process PHP internally using a module like `mod_php`, Nginx serves as a reverse proxy and requires an external processor to handle PHP files. `php-fpm` provides this functionality by acting as a FastCGI server for PHP scripts, allowing Nginx to communicate with it for processing PHP code.

This setup enhances performance and scalability, as `php-fpm` can handle multiple PHP processes concurrently, making it more suitable for high-traffic websites.

sudo apt install php-fpm php-mysql mysql-server nginx unzip

Configuring Nginx to work with PHP

Let's now replace Nginx's 'default' configuration

cd /etc/nginx/sites-available
sudo rm default
sudo nano default

We can then paste in the following configuration to the newly created empty file:

server {
    listen 80;
    server_name your_server_domain_or_IP;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

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

Restarting Nginx

We need to restart Nginx for the new configuration to take effect

sudo systemctl restart nginx.service

Removing the default index.html page

cd /var/www/html
sudo rm index.nginx-debian.html

Downloading Wordpress to the Ubuntu server

While still in the /var/www/html folder, let's download WordPress from wordpress.org:

sudo wget https://wordpress.org/latest.zip

Extracting the contents of the Wordpress archive

sudo unzip latest.zip

Deleting the WordPress zip archive

sudo rm latest.zip

Moving WordPress to the server's root folder

sudo mv wordpress/* .

Changing the owner of the Wordpress files

sudo chown -R www-data:www-data *

Securing MySQL installation

To improve the security of MySQL, it's recommended to run the mysql_secure_installation script:
sudo mysql_secure_installation
Answer 'yes' to all of the questions, and choose '2' as the required password strength.

Creating the database and a database user

We first need to start MySQL:
sudo mysql

Then, we can create the database for WordPress:

CREATE DATABASE wordpress_db;

And a database user:

CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'P@55word';

Make sure that you replace the password with something a bit more secure!

We then need to grant privileges to the user on the databse:

GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';

We can then quite from MySQL

quit

Installing WordPress

We can now visit the server's IP address and use WordPress's built in installer.

We will need to provide the database and username we just created.

Creating wp-config.php

If you get the message "Unable to write to wp-config.php file":

create the file in the /var/www/html folder:

sudo nano wp-config.php

Then copy the content of the file from the WordPress installer and paste it into your file.

Once you do that, click on "Run the installation", and WordPress will be installed

Happy WordPressing!

1 Comments

Add a new comment: