WordPress/Nginx on DigitalOcean

Small Guide to install a WordPress on a Ubuntu 22.04 Droplet on DigitalOcean VPS.
Few simple steps to create a small project using the most famous CMS on the web.

Create your Droplet on DigitalOcean

Login your ssh session and install your webserver (nginx)

apt update
apt upgrade
apt install nginx
ufw allow 'Nginx HTTP'

Install MySQL

apt install mysql-server

Install php-fpm

apt install php-fpm php-mysql

Create the database

mysql
mysql> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql> CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';

Install Additional php extensions

sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip

Configure ngnix

nano /etc/nginx/sites-available/wordpress

Within the main server block, let’s add a few location blocks.

Start by creating exact-matching location blocks for requests to /favicon.ico and /robots.txt, both of which you do not want to log requests for.

Use a regular expression location to match any requests for static files. We will again turn off the logging for these requests and will mark them as highly cacheable, since these are typically expensive resources to serve. You can adjust this static files list to contain any other file extensions your site may use:

server {
    . . .

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
    . . .
}

Inside of the existing location / block, let’s adjust the try_files list. Comment out the default setting by prepending the line with a pound sign (#) and then add the highlighted line. This way, instead of returning a 404 error as the default option, control is passed to the index.php file with the request arguments.

This should look something like this:

server {
    . . .
    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    . . .
}

Now, let’s check our configuration for syntax errors by typing:

nginx -t

If no errors were reported, reload Nginx by typing:

systemctl reload ngin

Install unzip and and Download WordPress

apt install unzip
cd /var/www/
wget https://wordpress.org/latest.zip
unzip latest.zip .

point the root directory to the installation

server {
    . . .
    root /var/www/wordpress/;
    . . .
}

Setting up the WordPress Configuration File

To grab secure values from the WordPress secret key generator, type:

apt install unzip
curl -s https://api.wordpress.org/secret-key/1.1/salt/

Output will be something like:


define('AUTH_KEY',         '...');
define('SECURE_AUTH_KEY',  '...');
define('LOGGED_IN_KEY',    '...');
define('NONCE_KEY',        '...');
define('AUTH_SALT',        '...');
define('SECURE_AUTH_SALT', '...');
define('LOGGED_IN_SALT',   '...');
define('NONCE_SALT',       '...');

Copy into you wp-config.php file and relace your user and password for the database connection


. . .

define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wordpressuser' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

. . .

define( 'FS_METHOD', 'direct' );