Install WordPress with Nginx on Debian 10

Install WordPress with Nginx on Debian 10

Overview

The following post will showcase the steps how to install WordPress with Nginx on Debian 10. On a single Debian 10 server we will install and configure the LEMP stack(Nginx, PHP and MariaDB) and at the end install and run WordPress. In other words, our single Debian server will run the Nginx web server, the MariaDB server and run WordPress altogether. WordPress can also be setup and run on multiple separate servers(separate web server and separate database server). If you need setup like tha, you can check how to setup it on this post. Another method of installing and running WordPress is with Docker, which is covered on this post.

Install necessary libraries and packages

sudo apt install nginx php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl mariadb-server

Once the installation is complete, start Nginx and MariaDB if are not running and enable them to auto-start on boot.

sudo systemctl start nginx.service

sudo systemctl enable nginx.service

sudo systemctl start mariadb.service

sydo systemctl enable mariadb.service

Configure the database

Start and configure the mariadb server with a database, user and grant privileges.

Before that, first it’s recommended to run the secure installation:

sudo mysq_secure_installation

When prompted for root password change, you can skip it but that depends on you do you need to change the root password or not. With the rest of the prompted questions, you can go ahead and go with Yes option. Similar like in the picture bellow:

install wordpress with nginx debian 10

then access the database server and create the database, a user and set privileges

sudo mysql -u root -p


CREATE DATABASE sampledbwp;


GRANT ALL ON sampledbwp.* TO 'sample-admin'@'localhost' IDENTIFIED BY 'SamplePassword1';


quit

With these commands, we created the database – sampledbwp, created the user sample-admin on localhost and granted all privileges to read and write the database we just created.

install wordpress with nginx debian 10

Install and configure the WordPress

Navigate to the following directory and download and extract the WordPress installation:

cd /var/www/html/

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

sudo tar -xzfv latest.tar.gz

Switch to wordpress directory and rename the wp-config-sample.php file to wp-config.php

cd wordpress

mv wp-config-sample.php wp-config.php

Now we’re going to edit the WordPress configuration file:

sudo nano wp-config.php

In the configuration file, we need to add the database user, database name and the password we created at MariaDB server and to add salt keys from – https://api.wordpress.org/secret-key/1.1/salt/ . Refer to the sample picture bellow:

install wordpress with nginx debian 10

Save and exit.

Set the correct permissions to the WordPress installation in order to be accessible from the internet:

sudo chown -R www-data:www-data /var/www/html/wordpress

sudo chmod -R 755 /var/www/html/wordpress

Configure Nginx

Moving on to the nginx web server configuration. First step is to create the configuration file for out WordPress website. Run the following command to start the text editor:

sudo nano /etc/nginx/sites-available/wordpress.conf

Bellow you’ll find the sample configuration:

server {
        listen 80;
        listen [::]:80;
        root /var/www/html/wordpress;
        index  index.php index.html index.htm;
        server_name mysite.com www.mysite.com;

        error_log /var/log/nginx/mysite.com_error.log;
        access_log /var/log/nginx/mysite.com_access.log;
        
        client_max_body_size 100M;
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

Save and exit.

Note – Double check and confirm the root of your wordpress installation and the php-fpm version running on your server. Those must be matching in the configuration file.

In end, it should look something like this:

install wordpress with nginx

The default configuration still remains in the Nginx and is active. Until removed, your configuration will not take place, meaning your WordPress website will not be reachable, so we need to remove the default configuration:

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

sudo rm /etc/nginx/sites-enabled/default

Create the symlink for our new Nginx wordpress configuration:

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

After that, double check the configuration with:

sudo nginx -t

and restart Nginx

sudo systemctl restart nginx

Test if WordPress is up in the browser by accessing it with either the server IP address or with the URL(if you have assigned the domain name) and complete the installation:

install wordpress with nginx debian 10

And that is it. You now have a WordPress website up and running.

Few suggestions and security tips

Here I would like to point out few suggestions for safer and more secure WordPress installation and how to maintain it.

  • First suggestion is to delete – xmlrpc.php file

This file is not a crucial point of WordPress and WordPress can function without this file without any problems. Xmlrpc file was used at the early stages of WordPress as a service, where bloging clients connect to WordPress via xmlrpc to post new content(in short). This file now is a cause of many and various malicious attacks(DDoS, brute force and etc).

In this example, we can delete this file with the command:

sudo rm /var/www/html/wordpress/xmlrpc.php

One note as well – with each update/upgrade of WordPress, this file will reappear, so it’s necessary to repeat acting each the WordPress is updated.

  • Delete wp-config-sample.php

In this post we renamed this file to the wp-config.php. We did this way since we manually added the database credentials for WordPress to connect to the database server and added the salt keys as well. This file also reappears with each WordPress update/upgrade and is suggested to delete it since it’s exposed to many malicious attack which can cause your site to be taken down.

  • Hide wp-login url and add 2FA login

It’s highly suggested to change the default URL login in order to prevent brute force attacks and add two factor authentication login. You can do this easily with plugins such as – WPS Hide login and miniOrange 2-Factor. Also now WordPress has it’s 2FA login as well by default.

  • Add firewall plugin and do not use account with user name admin

One of the first plugins that it’s highly suggested to install is a firewall plugin, such is – Wordfence or Sucuri

Summary

We covered the steps how to install WordPress with Nginx on Debian 10. This scenario were WordPress is served with it’s database on a single server machine is good option if you’re also starting out now and don’t have an extra budget to spare on infrastructure or if you are a beginner who’s first time trying out WordPress on a cloud/dedicated/VM/VPS hosting or for just some testing purposes. But one downside it can cause over time is if you still have this scenario in production, it can become rather difficult to maintain the server when upgrades are needed to be applied since it can cause downtimes. That’s why it’s common practice to have at least two servers and separate the WordPress install by having on one server only web server(Nginx or Apache) and WordPress installation and the other the database server(MariaDB, MySQL). The setup process for this kind of environment, you can check out on this post.

Thank you very much for your time…

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *