In this guide, I will show you how to install WordPress and configure it on a Ubuntu 20.4 machine,

This tutorial will teach you any technical terms you are not familiar with and make sure you become a WordPress techie in no time.

If you find this tutorial challenging, it means that you are learning a new skill. However, if you dont have the time or technical skills to manually configure WordPress, I’d recommend reading: How to install WordPress with a few clicks. or hiring an expert like me on Fiverr.com/wordpress_experts

Prerequisite

  • You will need have an Ubuntu 20.04 server with at least 1GB RAM.
  • You will need to have LAMP stack installed. LAMP standing for Apache, MariaDB and PHP7.4.
  • You will also need a domain name so that visitors can type the name in and access your website.

I registered my domain name at NameCheap because the price is low and they give whois privacy protection. This is essential so you’re name and details are not searchable by hackers.

Heres a quick tutorial, if you dont already have a domain name. Remember that the mywebsite.xyz is a lot cheaper compared to a .com domain if you want to practice.

This tutorial assumes that you have already set up a LAMP stack on Ubuntu 20.04. If not, please check out the following tutorial.

Step 1 : Download and Install WordPress on Ubuntu

From your laptop SSH into your Ubuntu machine

ssh <Ubuntu_Username>@<Your_Ubuntu_IP_Address>

Run

sudo apt update && sudo apt upgrade

“sudo apt-get update” fetches you a list of packages for all of your repositories and PPA’s and make sure it is up to date. While “sudo apt-get upgrade” does an actual software upgrade. 

This is important when first configuring your machine to ensure it is not running any outdated packages that may leave you vulnerable to hackers.

Next go to WordPress.org’s download page and copy the download link by right clicking the download button and select copy link

Type in wget and then paste in the direct download link to install the latest version of WordPress.

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

Next, extract the archive to the /var/www/ directory with unzip.

sudo apt install unzip

sudo mkdir -p /var/www/

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

The -d option specifies the target directory (where you will store it). WordPress web files will be extracted to /var/www/wordpress automatically. We can then rename the directory “wordpress” differently, so it’s easy for you to remember where your wordpress files are. I’ve replaced it with adamkoyuncu.uk and you can use your real domain name.

sudo mv /var/www/wordpress /var/www/adamkoyuncu.uk

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

It will prompt you for a password that you set whilst installing the LAMP stack.

Set something that is at least 12 characters long with special symbols and photograph it. The reason is that hackers will use software to try and hack your password and gain access to your database.
Read about it here and How to Protect your WordPress Website

Once you are logged in, password set, create a database for WordPress typing the below. I named it wordpress, but you can use whatever name you like such as your site name. (Don’t leave out the semicolon as it will assume that you will want to continue the command.)

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';

You’ll then need to flush the privileges table for the changes to take effect so that only you have access to everything in the database.

Then exit out of MariaDB.

exit;

Step 3: Configure And Install WordPress On Ubuntu

Go to your WordPress directory.

cd /var/www/adamkoyuncu.uk/

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

Replace the lined out words with the database name, username and password you created in the previous steps.

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

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

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

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 Apache user (www-data) as the owner of the WordPress site directory using the following command.

sudo chown www-data:www-data /var/www/adamkoyuncu.uk -R

The chown command allows you to change the user and/or group ownership of a given file, directory, or symbolic link.

Step 4: Create an Apache Virtual Host file for WordPress

Run the following command to create a virtual host file for your WordPress site in the /etc/apache2/sites-available/ directory.

sudo nano /etc/apache2/sites-available/adamkoyuncu.uk.conf

Put 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.

If you are not sure how to do that click here.

<VirtualHost *:80>       
        ServerName www.adamkoyuncu.uk
        ServerAlias adamkoyuncu.uk

        DocumentRoot /var/www/adamkoyuncu.uk

        #This enables .htaccess file, which is needed for WordPress Permalink to work. 
        <Directory "/var/www/adamkoyuncu.uk">
             AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/adamkoyuncu.uk.error.log
        CustomLog ${APACHE_LOG_DIR}/adamkoyuncu.uk.access.log combined
</VirtualHost>

Save and close the file. Then test configurations.

sudo apache2ctl configtest

If you see “Syntax OK”, then enable this virtual host.

sudo a2ensite adamkoyuncu.uk.conf

And reload Apache for the changes to take effect.

sudo systemctl reload apache2

Set a correct A record for your domain name, then enter your domain name in browser address bar.

adamkoyuncu.uk

or

adamkoyuncu.uk/wp-admin/install.php

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

Step 5: Enabling HTTPS

To encrypt our HTTP traffic, we can enable HTTPS by installing Let’s Encrypt which will give us a free TLS certificate.

Type in the following command to install Let’s Encrypt client (certbot) on our Ubuntu 20.04 server.

sudo apt install certbot python3-certbot-apache

And run this command to obtain and install TLS certificate.

sudo certbot --apache --agree-tos --redirect --hsts --uir --staple-ocsp --email email@example.com -d adamkoyuncu.uk,adamkoyuncu.uk

Where

  • --apache: Use the Apache plugin.
  • --agree-tos: Agree to terms of service.
  • --redirect: Force HTTPS by 301 redirect.
  • --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
  • --uir: Add the “Content-Security-Policy: upgrade-insecure-requests” header to every HTTP response.
  • --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.
  • --email: Email used for registration and recovery contact.
  • -d flag is followed by a list of domain names, separated by comma. You can add up to 100 domain names.

The certificate should now be obtained and automatically installed.

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

Step 6: Finish the Install WordPress on Ubuntu with the Setup Wizard

Create an admin account and click the Install WordPress button.


Please ensure you read: How Protect Your WordPress From Hackers

And now your new WordPress site is installed.

Redirecting WWW to Non-WWW (Or Vice-Versa)

We have now enabled redirecting from HTTP to HTTPS, what’s left to do is redirect www to non-www, or vice versa.

Its an easy change. 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.

Fixing the Double 301 Redirect

There is a double 301 redirect problem. This is mainly because we have set the preferred domain version in the WordPress Address and Site Address fields. Our Apache server redirects HTTP to HTTPS, then WordPress redirects to www or non-www domain.

You could say that you can lose SEO link juice when doing double 301 redirect. If you are worried about that, then you can use the method below to make all domain versions to go directly to the final destination, so there will be a single 301 redirect.

Edit your virtual host file. (Not the SSL virtual host)

sudo nano /etc/apache2/sites-enabled/adamkoyuncu.uk.conf

CertBot client added the following lines to the file to redirect HTTP to HTTPS.

RewriteEngine on
RewriteCond %{SERVER_NAME} =adamkoyuncu.uk [OR]
RewriteCond %{SERVER_NAME} =www.adamkoyuncu.uk
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

To redirect to www or non-www domain, you need to change the last line. Replace %{SERVER_NAME} with your preferred domain version like below. (www domain)

RewriteRule ^ https://www.adamkoyuncu.com%{REQUEST_URI} [END,NE,R=permanent]

If you prefer non-www domain, change it to the following.

RewriteRule ^ https://adamkoyuncu.uk%{REQUEST_URI} [END,NE,R=permanent]

Then save and close the file.

We will also need to edit the SSL virtual host.

sudo nano /etc/apache2/sites-enabled/adamkoyuncu.uk-le-ssl.conf

Add the following lines above the closing </VirtualHost> tag to redirect non-www to www domain if you prefer www domain.

RewriteEngine on
RewriteCond %{SERVER_NAME} =adamkoyuncu.uk
RewriteRule ^ https://www.adamkoyuncu.uk%{REQUEST_URI} [END,NE,R=permanent]

If you want to redirect www to non-www domain, add the following lines instead.

RewriteEngine on
RewriteCond %{SERVER_NAME} =adamkoyuncu.uk
RewriteRule ^ https://adamkoyuncu.uk%{REQUEST_URI} [END,NE,R=permanent]

Save and close the file. Reload Apache service for the changes to take effect.

sudo systemctl reload apache2

To be more clear, below is a screenshot of my Apache virtual host file and SSL virtual host file for redirecting non-www to www domain.

***IMPORTANT
Make sure you set your preferred domain version in WordPress Address and Site Address before editing Apache virtual host files. If WordPress settings contradict with Apache configuration, your site will end up in a redirect loop.

TLS Certificate Auto-Renewal

To automatically renew Let’s Encrypt certificate, simply edit root user’s crontab file.

sudo crontab -e

Then add the following line at the bottom.

@daily certbot renew --quiet && systemctl reload apache2

Now go and reload Apache which is needed for it to present the new certificate to clients.

Now You Know How To install WordPress on Ubuntu 20+ machine like A-Techie.

If you’d like to just install it with a few clicks next time, I’d recommend reading Easy Install WordPress With A Few Clicks – Ultimate Guide.

Or if you have installed this on AWS and want to automate its back up, check out Free WordPress Backup in AWS With Automation – Full Guide