How to install WordPress on Ubuntu 18.04 running Nginx on a Virtual Host Server with LetsEncrypt SSL.

WordPress runs much better on Nginx than Apache. This guide will show you how to easily install multiple WordPress instances on Ubuntu 18.04 running Nginx with a free LetsEncrypt SSL certificate. Presumable you have a LEMP installation already running.

Most of my websites use WordPress and I build a lot of them to either sell on Flippa or keep with the hope that one day they earn revenue. This page you are reading now is hosted on a virtual hosting server running Nginx on Ubuntu 18.04 along with around 20 other websites. I use DigitalOcean to host all of my websites and the guide below is exactly how I manage to do so.

The process is really simple. We create a virtual host instance, install LetsEncrypt, create a domain instance, point the domain to the server, add the SSL certificate, create a MySql database and install WordPress. Follow the step by step guide below and repeat for each instance of WordPress you wish to install per domain. Don’t forget to change “example.com” to your own domain name!

Note: You must already be running a LEMP installation. LEMP already has PHP, MySql and Nginx installed as default.

DigitalOcean no longer provides a LEMP instance on Ubuntu 18.04 and as this guide is written for Ubuntu 18.04, run the following commands to install LEMP.

  1. sudo apt update
  2. sudo apt install nginx
  3. sudo ufw allow 'Nginx Full'
  4. sudo apt install mariadb-server
  5. sudo mysql_secure_installation (enter current root password when requested and answer yes to everything else)
  6. sudo apt install software-properties-common
  7. sudo add-apt-repository ppa:ondrej/php
  8. sudo apt update
  9. sudo apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip unzip -y

The above will create a LEMP instance on Ubuntu 18.04. Now you can continue the guide below to install wordpress.

1, Create a Virtual Host Instance

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R www-data: /var/www/example.com

Now we create a server block. Simply run the command below and copy the code into the blank file replacing example.com with your own domain name.

Command:

sudo nano /etc/nginx/sites-available/example.com

Code:

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com/public_html;
    index  index.php index.html index.htm;
    server_name example.com www.example.com;
     client_max_body_size 100M;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;     } }

Now run the following commands:

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

2, Setup your Domain DNS

You need to setup an A and CName record. In this guide we will be using non www.

A Record = Hostname: example.com > Value: Server IP > TTL: 3600

CName = Hostname: www.example.com > Value: example.com > TTL: 43200

Example below:

3, Install LetsEncrypt SSL for the Domain

sudo add-apt-repository ppa:certbot/certbot
sudo apt install python-certbot-nginx
sudo ufw allow 'Nginx Full'
sudo systemctl reload nginx
sudo certbot --nginx -d YOURDOMAIN.COM

Select option 2:

2: Redirect - Make all requests redirect to secure HTTPS access.

4, Install ZIP UNZIP

sudo apt install zip

5, Download WordPress Files to your Server

Navigate to the public_html folder of your domain name:

cd /var/www/example.com/public_html

Now grab the WordPress zip file using WGET:

sudo wget https://simonward.net/wp.zip

Unzip the WordPress archive

sudo unzip wp.zip

6, Create a MySQL Database

You will need your default database password for this step. If you are using DigitalOcean, run the following command to get the default MySQL password:

cat /root/.digitalocean_password

Now we create a database name, database user and database password using the commands below:

mysql -p
create database databasename;
grant all privileges on databasename.* to 'databaseusername'@'localhost' identified by "databasepassword";
flush privileges;
exit


7, Install Additional PHP Extensions

sudo apt update
sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip
sudo systemctl restart php7.2-fpm

8, Redo File Permissions

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

9, Install WordPress

Now the hard part is done, simply navigate to your domain name and follow the WordPress installation instructions. You will need your MySQL info from step 6.

That’s all there is to it! You should now have a fully working WordPress installation on your Ubuntu 18.04 virtual hosting server running on Nginx.

To add more WordPress websites, simply follow the guide and miss out the LetsEncrypt and PHP initial installations or just skip steps 3 and 7 using only the

sudo certbot --nginx -d YOURDOMAIN.COM

to install SSL onto your domain name.

You might want to also setup Postfix Email Forwarding using this simple guide here.

I use DigitalOcean for all my website hosting. You can get $100 free DigitalOcean credit by using my referral link here.