Install WordPress UBUNTU

How to Install WordPress with Nginx, MariaDB, PHP 7.4, and a free SSL on Ubuntu 20.04

The instructions below will work on other versions of Ubuntu, including 18.04 and 16.04, but these are specifically written for 20.04.

1. Update the server

Before doing anything, you should update your server and its packages with the following commands:

apt-get update && apt-get upgrade -y

2. Install Nginx

To install Nginx on Ubuntu 20.04, run the following command:

apt-get install nginx -y

Check if Nginx is installed with:

nginx -v

Which should give you an output similar to this:

nginx version: nginx/1.18.0 (Ubuntu)

If you use a firewall you’ll need to add a rule to allow Nginx.

3. Install MariaDB

MariaDB is pretty much the same with MySQL, so don’t get confused by the name. To install MariaDB, run the following command:

apt-get install mariadb-server -y

To check if it’s installed, log into your MariaDB server by running:

mysql

If you can log in it’s installed. You should get the specific MariaDB version you’re running in the welcome message:

Server version: 10.3.25-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Exit out of the database server by running:

exit;

You should run the following script to secure and configure your database server:

mysql_secure_installation

And follow the prompts. Use a strong password. You can enter Y (the default) for all prompts.

4. Install PHP

Ubuntu 20.04 uses PHP 7.4, so make sure the plugins you’re planning on using work well with PHP 7.4.

To install PHP with all its dependencies and needed modules, run the following commands:

apt-get install php-fpm php-curl php-mysql php-gd php-mbstring php-xml php-imagick php-zip php-xmlrpc -y

Check if PHP is installed with:

php -v

Which should give you an output similar to this:

PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )

That’s it. The LEMP stack is installed. Let’s move on.

5. Configure PHP

First, start by editing the php.ini file.

We’ll use the Nano text editor (which is the easiest for beginners). After you edit something with Nano, hit “CTRL + X”, then Y, and Press enter to save the changes. You can search the file with “CTRL + W”.

So open the file:

nano /etc/php/7.4/fpm/php.ini

And search for this line:

;cgi.fix_pathinfo=1

Uncomment the line by removing the ; and update it to 0:

cgi.fix_pathinfo=0

Find all the following lines in the php.ini file and update them accordingly:

upload_max_filesize = 500M
post_max_size = 2000M
memory_limit = 2000M
max_execution_time = 120

You can use different values depending on your server.

6. Create a database

First, log in to your MariaDB server with the password you set earlier:

mysql -u root -p

And create a database for your WordPress by running these commands:

CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'EnterStrongPassword';
GRANT ALL ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
exit;

With these commands, you’ll create a database and a user and grant permissions to the user. You can use your own names instead of these. Remember to use a strong password.

7. Download WordPress

Start by navigating to the directory where you want to download WordPress. We’ll use Nginx’s default directory:

cd /var/www/html

And download the latest version:

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

Extract the archive in the directory you’re currently in:

tar -zxvf latest.tar.gz --strip-components=1

Remove the archive:

rm -f latest.tar.gz

And update permissions:

chown -R www-data:www-data /var/www/html/
chmod -R 755 /var/www/html/

8. Configure Nginx

First, create an Nginx configuration file:

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

And paste the following (after updating example.com to your domain)

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;
    client_max_body_size 500M;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
	
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }	
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }	
    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;
    }
}

And enable the configuration file:

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

Disable the default configuration file if you’re not using it:

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

Run the following command to test and see if everything’s ok with Nginx:

nginx -t

Now restart Nginx and PHP-FPM for the changes to take effect

systemctl restart nginx.service
systemctl restart php7.4-fpm.service

9. Configure WordPress

There’s a default wp-config file that we need to edit. So first rename the file:

mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php

Open it:

nano /var/www/html/wp-config.php

And update the following lines with your database information:

define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'EnterYourDatabasePassword');

Save and close the file.

For security reasons, you should update the security keys in your wp-config file.

First, go here to generate them.

Open the wp-config.php file again:

nano /var/www/html/wp-config.php

And update the following lines with the ones you generated:

define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');

Save and close the file.

You can be done here if you don’t plan on using HTTPS, but we’ll show you how to set up a free SSL certificate from Let’s Encrypt.

10. Install and configure a free SSL certificate (HTTPS)

We’ll use Let’s Encrypt and the certbot for automatic installation and configuration.

First, install the certbot:

apt-get install python3-certbot-nginx -y

Then install a certificate for your domain:

certbot --nginx -m [email protected] -d example.com -d www.example.com

At the prompts, agree to the Terms and Conditions by entering “a” and then optionally subscribe to ETF with “y”.

IMPORTANT: the certbot will ask you whether to redirect all traffic to HTTPS (option 2) or not (option 1), you need to choose option 2. So enter 2.

The certbot will automatically update your Nginx configuration file. A Let’s Encrypt certificate lasts 90 days by default. You’ll be notified before it expires, so you can renew it.

To renew your certificate manually, run:

certbot renew

To automatically update your certificate, set up a cron job by running:

crontab -e

And adding the following line:

0 1 * * * /usr/bin/certbot renew & > /dev/null

Which will automatically renew your certificate every 30 days.

11. Finish installing WordPress

Navigate to https://example.com and follow the steps to finish installing WordPress.

First, you’ll need to choose a language.

Then, you’ll need to enter site information such as title, username, password etc.

final installation steps

Click on ‘Install WordPress’ and that’s it! You’re done. You’ve successfully installed WordPress on Ubuntu 20.04 with Nginx, PHP 7.4, MariaDB, and Let’s Encrypt SSL (HTTPS).