Install WordPress on Ubuntu 20.04 with Nginx, MariaDB, PHP7.4 (LEMP)

Prerequisite

  • LEMP Stack (Nginx, MariaDB, PHP7.4) on Ubuntu 20.04

Step 1: Download WordPress

sudo apt update && sudo apt upgrade
wget https://wordpress.org/latest.zip

Next, extract the zip archive using the command below.

sudo apt install unzip

sudo mkdir -p /var/www/your-domain

sudo unzip latest.zip -d /var/www/your-domain

Step 2: Create a Database and User for WordPress Site

Log into MariaDB shell as root with the following command.

sudo mariadb -u root

or

sudo mysql -u root

Once you are logged in, create a database for WordPress using the following command. Create database I named it wordpress you can choose whatever name you like. Replace wordpress from the command with your choice.

create database wordpress;

Then enter the command below to create a database user for WordPress. This command also grants all privileges of WordPress database to the user. Replace wpuser and your-password with your preferred username and password.

grant all privileges on wordpress.* to wpuser@localhost identified by 'your-password';

Flush the privileges table for the changes to take effect and then get out of MariaDB shell.

flush privileges;

exit;

Step 3: Configure WordPress

Go to your WordPress directory.

cd /var/www/your-domain/

Copy the sample configuration file and rename it to wp-config.php.

sudo cp wp-config-sample.php wp-config.php

Now edit the new config file with a command-line text editor like Nano.

sudo nano wp-config.php

Find the following lines and replace the red texts with the database name, username and password you created in the previous step.

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

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

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

Then scroll down to find the following line.

$table_prefix = 'wp_';

By default, every WordPress database table name begins with wp_ as the prefix. It’s highly recommended to change it to something else to improve security. Use random characters like below.

$table_prefix = '9OzB3g_';

Save and close the file. To save the file in Nano text editor, press Ctrl+O, then press Enter to confirm. Next, press Ctrl+X to exit.

We also need to set the Nginx user (www-data) as the owner of the WordPress site directory by using the following command.

sudo chown www-data:www-data /var/www/your-domain/ -R

Step 4: Create an Nginx Server Block for WordPress

Create the server block file in /etc/nginx/conf.d/ directory. The file name must end with .conf.

sudo nano /etc/nginx/conf.d/example.com.conf

Paste the following texts into the file. Replace the red texts with your own domain name. Don’t forget to create A records for your domain name in your DNS manager.

server {
  listen 80;
  listen [::]:80;
  server_name www.your-domain.com your-domain.com;
  root /var/www/your-domain.com/;
  index index.php index.html index.htm index.nginx-debian.html;

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

   location ~ ^/wp-json/ {
     rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
   }

  location ~* /wp-sitemap.*\.xml {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  client_max_body_size 20M;

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;

    # Add headers to serve security related headers
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Permitted-Cross-Domain-Policies none;
    add_header X-Frame-Options "SAMEORIGIN";
  }

  #enable gzip compression
  gzip on;
  gzip_vary on;
  gzip_min_length 1000;
  gzip_comp_level 5;
  gzip_types application/json text/css application/x-javascript application/javascript image/svg+xml;
  gzip_proxied any;

  # A long browser cache lifetime can speed up repeat visits to your page
  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;
  }

  # disable access to hidden files
  location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;
  }

}

Note: Replace 7.4 with version of PHP you have installed.

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

Enter your domain name in the browser address bar.

your-domain.com

or

your-domain.com/wp-admin/install.php

You shall see the WordPress installation wizard. Select a language.

If the installation wizard isn’t displayed, you probably need to install some PHP7 extensions. Note: Replace 7.4 with the version you have

sudo apt install php-imagick php7.4-fpm php7.4-mbstring php7.4-bcmath php7.4-xml php7.4-mysql php7.4-common php7.4-gd php7.4-json php7.4-cli php7.4-curl php7.4-zip

Then reload PHP-FPM and Nginx. The wizard should now be displayed.

sudo systemctl reload php7.4-fpm nginx

Before entering your sensitive information in the setup wizard, it’s recommended to enable HTTPS to prevent traffic hijacking.

Step 5: Enabling HTTPS

To encrypt the HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 20.04 server.

sudo apt install certbot python3-certbot-nginx

And run this command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email you@your-domain.com -d yourdomain.com,www.yourdomain.com

A valid email is recommended you@your-domain.com or any email to which you have access. email: Email used for registration and recovery contact

The certificate should now be obtained and automatically installed.

Reload the WordPress setup wizard, you can see that HTTP is automatically redirected to HTTPS connection.

Step 6: Finish the Installation with the Setup Wizard

Create an admin account and click the Install WordPress button.

And now your new WordPress site is installed. Your page could be different as per the version of WordPress you installed.

How to Redirect www to non-www (Or Vice Versa)

We have already enabled redirecting HTTP to HTTPS, what’s left to do is redirect www to non-www, or vice versa. It’s very easy.

Simply go to WordPress Dashboard > Settings > General and set your preferred version (www or non-www) in WordPress Address and Site Address. Be sure to include the https:// prefix.

 

Increase Upload File Size Limit

Please check out my post here

 

 

 

 

 

 

 

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Liked this post? Share with others!

Do you want to boost your business today?

This is your chance to invite visitors to contact you. Tell them you’ll be happy to answer all their questions as soon as possible.