Full install & setup of Apache, PHP 8, MySQL and SSL on Ubuntu 20.04

Installing and setting up a LAMP stack on Ubuntu 20.04 x64. This post covers installing Apache with the brand new PHP 8, a MySQL server and finally enabling SSL with let’s encrypt for your hostname.

Apache PHP 8 MySQL SSL Ubuntu 20.04 install

The first step after SSH’ing into your server is to update the package list

sudo apt update

Once that has finished you can now progress with this guide.

Apache install

Apache is a popular, open-sourced HTTP server. Install it with:

sudo apt install apache2

Now for some firewall tinkering with UFW (Uncomplicated firewall).

When you run this command:

sudo ufw app list

If it returns ufw: command not found that means UFW is not installed.

You can install UFW with:

sudo apt install ufw

Now allow Apache traffic:

sudo ufw allow 'Apache'

Make sure you have your SSH port or SSH in general allowed:

sudo ufw allow 22

or



sudo ufw allow ssh

This is important as you don’t want to be blocked out of your server.

Turn on UFW

sudo ufw enable

Check Apaches status with:

sudo systemctl status apache2

You can also navigate to HTTP://SERVER_IP_HEREin a web browser to see if Apache is installed and able to be accessed.

You will be greeted with the default page upon successful install

Apache2 Ubuntu Default Page It works

PHP 8 install

PHP 8 is the most recent version of PHP and a main one too, it was released November 26th 2020.

Installing PHP 8 on Ubuntu 20.04 means you must first install the independent software sources:

sudo apt install software-properties-common

and then the repository for the PHP versions:

sudo add-apt-repository ppa:ondrej/php

Press enter to confirm

Now finally actually downloading and installing PHP 8 and the apache module for PHP 8:

sudo apt install php8.0 libapache2-mod-php8.0

Go ahead and restart Apache

sudo systemctl restart apache2

To check if PHP is installed and its version use:

php -v

Installing PHP extensions is done with this command:

sudo apt install php8.0-[extension_name]

A quick method to install common extensions:

sudo apt install php8.0-{bz2,curl,intl,mysql,xml}

SSL install

Time to get HTTPS or SSL for your webserver with a free let’s encrypt certificate.

sudo apt install --reinstall software-properties-common

and then add the Certbot repository:

sudo add-apt-repository ppa:certbot/certbot

Press enter to confirm when it prompts.

Install Certbot:

sudo apt install -y certbot python3-certbot-apache

Now to get an SSL certificate/s for your hostname.

sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

Replace yourdomain.com with the hostname assigned to your VPS

This will ask for your email address, terms agreement and if you want to be contacted by the Electronic Frontier Foundation about news and changes.

Choose if you want to redirect to HTTPS or not.

On success you will now need to open port 443 for the HTTPS:

sudo ufw allow 443

As the certificate expires every 90 days instead of manually renewing it Certbot can do it for you. To test that Certbots renew is functioning correctly run:

sudo certbot renew --dry-run

MySQL install

Now for the MySQL server install

sudo apt install mysql-server

Set up with more secure passwords:

sudo mysql_secure_installation

After setting and validating your password:

systemctl status mysql.service

Optional to allow remote connection to your MySQL:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find and set

bind-address = 127.0.0.1

to

bind-address = 0.0.0.0

Restart MySQL server to apply the changes

sudo systemctl restart mysql

Allow port 3306 in UFW:

sudo ufw allow 3306

and that is the MySQL server installed you will now need to configure/add users as you see fit.