The Complete Guide To Install LAMP Server(Ubuntu, Apache, MySQL, PHP) Using Ubuntu 20.04 LTS
The Complete Guide To Install LAMP Server(Ubuntu, Apache, MySQL, PHP) Using Ubuntu 20.04 LTS
June 17, 2020

This tutorial provides the steps required to install LAMP Stack using Ubuntu 20.04 LTS server as listed below. The steps should be the same or similar on other versions of Ubuntu and Linux systems.

Linux - Ubuntu 20.04

Apache - Apache 2.4

MySQL - MySQL 8

PHP - PHP 7.4

phpMyAdmin

Prerequisites

This tutorial assumes that you have already completed the installation of the Ubuntu 20.04 LTS server on your preferred service provider. It also assumes that the standard web server ports 80 and 443 are publicly accessible. The popular service providers include AWS, Linode, Digital Ocean, UpCloud, Vultr, etc. You can also follow the below-listed tutorials to spin up Ubuntu 20.04 LTS.

Install Apache 2.4 On Ubuntu 20.04 LTS

This section provides the steps to install Apache 2.4 on Ubuntu 20.04 LTS. The commands required to install Apache 2.4 are shown below.

# Refresh the indexes
sudo apt update

# Install Apache
sudo apt install apache2

# Verify Apache
apache2 -version

# Output
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2020-04-13T17:19:17

The above-mentioned commands installed Apache 2.4.41 while writing this tutorial. Also, execute the below-mentioned commands to install additional modules.

# Install security
sudo apt-get install libapache2-mod-security2

# Enable additional modules
sudo a2enmod rewrite ssl security2 deflate expires headers

# Restart Apache 2
sudo systemctl restart apache2

The default www directory used by the Apache Web Server is /var/www. Now open the Apache's Home Page in your favorite browser by using the Server's public IP address or the DNS name assigned by the service provider. It should show the Home Page similar to Fig 1. It can also be accessed using http://localhost in case it's installed on a local desktop or server system.

LAMP Server - Ubuntu 20.04 LTS - Apache Home Page

Fig 1

You can follow How To Install Apache 2 On Ubuntu 20.04 LTS for more details and Configure Virtual Host On Apache to configure the websites for production usage. You can also follow How To Install Let's Encrypt For Apache On Ubuntu and Redirect HTTP to HTTPS on Apache to secure your websites using Let's Encrypt.

Install MySQL 8 On Ubuntu 20.04 LTS

This section provides the steps required to install MySQL 8 on Ubuntu 20.04 LTS. Now execute the below-mentioned command to install MySQL 8.

# Install MySQL Server 8
sudo apt install mysql-server

# MySQL Status
sudo systemctl status mysql

# Output
mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-06-18 05:10:55 UTC; 18min ago Main PID: 14768 (mysqld) Status: "Server is operational" Tasks: 40 (limit: 1119) Memory: 317.7M CGroup: /system.slice/mysql.service └─14768 /usr/sbin/mysqld
-----

It installs MySQL core, server, and client packages. Also, the MySQL server version 8.0.20 was installed while writing this tutorial. Configure and secure the installation using the command mysql_secure_installation as shown below. It will ask to set the root password and a few security questions.

Step 1 - Secure Installation - Execute the command to start the MySQL configuration.

# Secure MySQL
sudo mysql_secure_installation

Step 2 - Password Validator - It asks to confirm the usage of the password validator to validate the password. It will also show the password strength while providing the password.

# Password Validator Component
Press y|Y for Yes, any other key for No: y

If we select No, it won't check the password strength of the MySQL root and other users while adding them. We should use a strong password for the MySQL users, hence it's recommended to use the Password Validator Component.

Step 3 - Password Validation Level - The secure installation command asks the password validation level and provides options to choose among Low(0), Medium(1), and Strong(2). It's recommended to use at least Medium Level to have a strong password of all the MySQL users. The validation rules of all the levels are as listed below.

Low - It expects a password having at least 8 characters without any restriction on the characters.

Medium - The Medium Level expects a password that has at least 8 characters and allows numeric, uppercase, lowercase, and special characters.

Strong - The Strong Level expects a password that has at least 8 characters and allows numeric, uppercase, lowercase, and special characters. It also allows the dictionary file.

# Password Validation Level
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Step 4 - Root Password - The default authentication plugin used by MySQL for the root user is auth_socket.

# Password Prompt
New password:<password>
Re-enter new password:<repeat password>

In case you have selected to use Password Validator, it will also show the password strength of the root password and confirm to use the given password as shown below.

# Confirm Password
Estimated strength of the password: 80
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

If we opt for No, it will again prompt for the password.

Step 5 - Remove Anonymous Users - After providing the password, the secure installation process asks to remove the anonymous users. MySQL adds an anonymous user while installing it for testing purposes and allows anyone to log in without a password. It's recommended to remove the anonymous user.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Step 6 - Disallow Remote Login - The secure installation process also asks to confirm whether remote login is allowed for the root user. We should choose option y to restrict the root user to the localhost. We can always add additional users to allow remote login when required.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Step 7 - Remove Test Database - MySQL creates the test database while installing it. You may keep the test database for analysis purposes and later drop it.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

Step 8 - Reload Privilege Tables - At last, the secure installation asks to reload the privileges tables to immediately apply the changes.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

Now we will change the password plugin of the root user to caching_sha2_password to allow the other applications including phpMyAdmin to login to MySQL server using the root user. You may skip this in case the root user is not required by other applications including phpMyAdmin.

# Login to MySQL
sudo mysql

# Check password scheme of root user
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

# Note the password plugin of root user
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| debian-sys-maint | $A$005$[DA
NP9|K1zAmHe`LVwrhII7zBo5b5xUoPnvOLuCa9CSJVqCn7W1rzOCCyZD | caching_sha2_password | localhost |
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| root | | auth_socket | localhost |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+

# Change the password plugin - caching_sha2_password
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<pw>';

# Apply changes
mysql> flush privileges;

# Check password scheme of root user
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

# Note the password plugin of root user
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | $A$005$ZtYD-ppbn>iO�"MHhl/0TXh9Qo3xYdWK3ThKPmDB6r.QhVlZY1dcT1LWH0A | caching_sha2_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

# Quit Database
mysql> exit

Apart from changing the root user password plugin, we can also add additional users and databases as shown below.

# Login to MySQL
sudo mysql
# OR
mysql -u root -p

# Create Users
mysql> CREATE USER 'projecta'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';
mysql> CREATE USER 'projectb'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

# Create Database - with default character set
mysql> CREATE DATABASE projecta;

# Create Database - with utf8mb4 character set - preferred
mysql> CREATE DATABASE projectb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

# Grant privileges to the users
mysql> GRANT ALL PRIVILEGES ON projecta.* TO 'projecta'@'localhost';
mysql> GRANT ALL PRIVILEGES ON projectb.* TO 'projectb'@'localhost';

# Apply Changes
mysql> FLUSH PRIVILEGES;

# Exit MySQL
mysql> exit;

You can also follow How To Install MySQL 8 on Ubuntu 20.04 LTS to learn more about installing MySQL 8.

Install PHP 7.4 On Ubuntu 20.04 LTS

This section provides the steps to install PHP 7.4 on Ubuntu 20.04 LTS. We can install PHP 7 on Ubuntu 20.04 LTS as shown below.

# Install PHP 7.4 on Ubuntu 20.04 LTS
sudo apt-get install php7.4

# Verify PHP
php --version

# Output
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

# Install Apache2 Module
sudo apt-get install libapache2-mod-php7.4

# Install MySQL Extension
sudo apt-get install php7.4-mysql

# Install CGI and CLI if not installed by default
sudo apt-get install php7.4-cgi php7.4-cli

# Install CURL and JSON extensions
sudo apt-get install php7.4-curl php7.4-json

# Install PHP GD and Imagick
sudo apt-get install php7.4-gd php-imagick

# Multibyte String, Internationalization and Spell Check
sudo apt-get install php7.4-mbstring php7.4-intl php7.4-pspell

# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation
sudo apt-get install php-mbstring php-intl php-pspell

# Emails
sudo apt install php7.4-imap

# Tidy and XML RPC
sudo apt-get install php7.4-tidy php7.4-xmlrpc

# Excel
sudo apt-get install php7.4-xsl

# Install OPcache extension
sudo apt-get install php7.4-opcache

# Install Zip
sudo apt-get install php7.4-zip

The above-mentioned commands install PHP 7.4 with the generic extensions required by most of the applications. You can also follow How To Install PHP 7 On Ubuntu 20.04 LTS to learn more about installing PHP 7.

Install phpMyAdmin On Ubuntu 20.04 LTS (Optional)

This section provides the steps required to install phpMyAdmin on Ubuntu 20.04 LTS. It's optional to install phpMyAdmin as part of the LAMP stack set up. The MySQL server can be accessed directly using the SSH. We can also access MySQL using PHPStorm and SQLYog using SSH and HTTP Tunneling.

Before we start installing phpMyAdmin, we must disable the Validate Password Plugin of MySQL as shown below.

# Login to MySQL

sudo mysql
# OR
mysql -u root -p

# Disabled Validate Password Plugin
mysql> UNINSTALL COMPONENT "file://component_validate_password";
mysql> exit;

Now start installing phpMyAdmin on Ubuntu by executing the commands as shown below.

# Install phpMyAdmin
sudo apt install phpmyadmin php-mbstring php-gd php-zip php-json

The installation process asks to choose the web server as shown in Fig 2.

LAMP Server - Ubuntu 20.04 LTS - phpMyAdmin - Web Server

Fig 2

The install process asks to create the storage database of phpMyAdmin. You may either skip it or install it. In case you have opted to install the database by choosing Yes as shown in Fig 3, it will ask for the password and confirm password as shown in Fig 4 and Fig 5.

LAMP Server - Ubuntu 20.04 LTS - phpMyAdmin - Database

Fig 3

LAMP Server - Ubuntu 20.04 LTS - phpMyAdmin - Password

Fig 4

LAMP Server - Ubuntu 20.04 LTS - phpMyAdmin - Confirm Password

Fig 5

This creates the database of phpMyAdmin and also completes the installation. We can use phpMyAdmin with and without its database. Now again enable the Validate Password Plugin as shown below.

# Login to MySQL

sudo mysql
# OR
mysql -u root -p

# Enable Validate Password Plugin
mysql> INSTALL COMPONENT "file://component_validate_password";
mysql> exit;

The installation process installs phpMyAdmin at /usr/share/phpmyadmin and generates the configuration files at /etc/phpmyadmin. The Apache Web Server configuration file can be accessed at /etc/phpmyadmin/apache.conf. Also, the installation script enables phpMyAdmin by generating the symlink at /etc/apache2/conf-enabled/phpmyadmin.conf.

Now change the alias and add the line AllowOverride All to the apache configuration as shown below. It's required later for the Basic Authentication.

# Update Apache Configuration
sudo nano /etc/phpmyadmin/apache.conf

# Update
Alias /mydb /usr/share/phpmyadmin
----
----
<Directory /usr/share/phpmyadmin>
Options SymLinksIfOwnerMatch
DirectoryIndex index.php
AllowOverride All
----
----

# Save and exit the editor

Now, enable the PHP module mbstring, check phpMyAdmin configuration and enable it for the Apache Web Server if the installation didn't enable it, and restart the Apache Web Server as shown below.

# Enable PHP module mbstring
sudo phpenmod mbstring

# Test phpMyAdmin configuration file
sudo ls -l /etc/apache2/conf-enabled/phpmyadmin.conf

# Output - File not exist yet
ls: cannot access '/etc/apache2/conf-enabled/phpmyadmin.conf': No such file or directory

# If above error -> Enable phpMyAdmin configuration for Apache
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-enabled/phpmyadmin.conf

# Again test phpMyAdmin configuration file
sudo ls -l /etc/apache2/conf-enabled/phpmyadmin.conf

# Output - File exists - phpMyAdmin configuration enabled for Apache
lrwxrwxrwx 1 root root 27 Jun 12 13:29 /etc/apache2/conf-enabled/phpmyadmin.conf -> /etc/phpmyadmin/apache.conf

# Restart Apache
sudo systemctl restart apache2

The above-mentioned commands enable phpMyAdmin as a sub-domain to the primary domain using the alias phpmyadmin. We can access the same on the browser using the URLs as shown below.

Localhost - http://localhost/phpmyadmin OR http://127.0.0.1/phpmyadmin

Remote/Production Server - http://www.myserverdomain.com/phpmyadmin OR http://xx.xx.xxx.xxx/phpmyadmin - Replace www.myserverdomain.com with your server's domain address or xx.xx.xxx.xxx with your server's IP address.

Now secure the phpMyAdmin installation using the in-built security feature of Apache Web Server i.e. Basic Authentication. After enabling the Basic Authentication, the browser will prompt for username and password to access the phpMyAdmin web interface. We can enable the Basic Authentication as shown below.

# Configure htaccess of phpMyAdmin
sudo nano /usr/share/phpmyadmin/.htaccess

# Content
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user

# Save and exit the editor

The .htaccess file makes sure that the web interface of phpMyAdmin is accessible only if the valid username and password are specified in the AuthUserFile i.e. .htpasswd file. We can add username and password to the .htpasswd as shown below.

# Make directory
sudo mkdir -p /etc/phpmyadmin

# Generate Username Password pair
sudo htpasswd -c /etc/phpmyadmin/.htpasswd username

# Output
New password:
Re-type new password:
Adding password for user username

We can add more users to the htpasswd file without using the -c argument. Now if we try to access phpMyAdmin, it will show a prompt asking for username and password. You can also follow The Complete Guide To Install And Secure phpMyAdmin On Ubuntu 20.04 LTS to learn more about installing phpMyAdmin.

Summary

This tutorial provided the steps required to install and set up the LAMP stack using Ubuntu 20.04 LTS, Apache 2.4, MySQL 8, and PHP 7.4.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS