How to Install LAMP (Linux, Apache, MySQL & PHP) Stack on Ubuntu 20 LTS Server

LAMP is a popular open-source applications stack to host dynamic website comprises with Linux, Apache, MySQL/MariaDB and PHP/Perl/Python. This guide will help you to install Apache Web Server, MySQL and PHP on Ubuntu 20.04 LTS which are part of LAMP Stack. We can install LAMP by using the APT command or by using the tasksel command. Many of the popular CMS (Content Management System) like WordPress, Drupal, Joomla etc. uses the LAMP stack to their applications.

Follow the below steps to Install LAMP Server

. Ubuntu 20.04 LTS Running Instance
. root or sudo privileges
. Internet access to download packages
Ubuntu 20.04 LTS Installation Guide with Screenshots

Step 1: Update your system

Post Ubuntu 20.04 LTS installation, it’s always good idea to update repository and software packages. Following command to update the repository (Click on picture to enlarge it).
$ sudo apt update && sudo apt upgrade
Note down the Ubuntu server details and IP Address.
$ sudo cat /etc/os-release
$ sudo ip addr show  | grep -i inet

Step 2: Install Apache Web Server

Apache Web Server packages are available on Ubuntu official repository. Enter the following command to install Apache Web Server (Click on picture to enlarge it)
$ sudo apt install -y apache2 apache2-utils
$ sudo systemctl status apache2.service
$ sudo systemctl enable apache2.service
$ sudo apache2 -v

Start apache2.service if it is not started and enable to start automatically at system boot up also check the apache2 version. Allow HTTP access if firewall (UFW or FirewallD) is exist on your environment. 

Allow HTTP Access on UFW or FirewallD

Add following rule in iptables/UFW if exist on your environment or system (Click on picture to enlarge it).
For iptables
$ sudo iptables -I INPUT -p tcp –dport 80 -j ACCEPT
To list active rules in a table
$ sudo iptables -L


$ sudo ufw allow http

To list available application profiles available on UFW
$ sudo ufw app list

Once the installation is done, Now open browser and type IP Address or Host Name/DNS of the server in the address bar, you see the default Ubuntu page “It works!”, which indicates that our Apache Web Server is running state, in your case IP Address and HostName would be different (Click on picture to enlarge it).
[http://IP Address/HostName]

Note: Set to “www-data” (apache user) as the owner of default document root (/var/www/html) with “-R” option resursively. By default it’s owned by root user.
$ sudo chown -R www-data:www-data /var/www/html/

Step 3: Install MySQL Database

Now, Apache Web Server is up and running, it’s time to install MySQL Database Server to store and manage data. Enter following command to install MySQL database from Ubuntu command prompt (Click on picture to enlarge it).
$ sudo apt install mysql-server

MySQL Server should be automatically started. Use following command to check status.
$ sudo systemctl status mysql
Use the following command to start MySQL Server automatically at system boot up also check MySQL Server version (Click on picture to enlarge it).
$ sudo systemctl enable mysql
$ sudo mysqladmin -p -u root version

Post installation script for Securing MySQL Server and setting root password
$ sudo mysql_secure_installation
You will be asked to “VALIDATE PASSWORD COMPONENT”, if you don’t want to enable this component press any key to skip and follow rest of the steps. Here, we have selected “Y” showing three levels of password validation policy below.
0 = LOW
Installer will prompt to set MySQL root user password (Click on picture to enlarge it)

Press “Y” to continue with provided password. For rest of the questions press “Y” at each prompt to remove anonymous users, Disallow root login remotely, remove test database (Click on picture to enlarge it).

Reload privilege tables with “Y” and you are done! (Click on picture to enlarge it)

By default, MySQL uses unix_socket to authenticate user login, means you can use username and password of OS to login into MySQL prompt. Login with following command without root password (Click on picture to enlarge it).
$ sudo mysql
SQL> select Host, User, authentication_string, plugin from mysql.user;

if you wish to switch to mysql_native_password as a default authentication plugin, can do using ALTER USER statement as shown below (Click on picture to enlarge it).
SQL> ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

Login with password set as “password
$ sudo mysql -u root -p

Exit from MySQL prompt with command “exit“and this is the end of MySQL Installation.

Step 4: PHP Installation

In the steps above, we have installed Apache Web Server and MySQL. Now, we will be installing php along with php-mysql, libapache2-mod-php to display and process dynamic content to the user (Click on picture to enlarge it)
Run following command to install the packages
$ sudo apt install php libapache2-mod-php php-mysql

Post PHP installation, create a “info.php” file in the document root directory adding below lines to test the working of PHP (Click on picture to enlarge it).
$ sudo echo “<?php phpinfo(); ?>” | sudo tee /var/www/html/info.php

Check PHP installed version
$ sudo php –version

Testing PHP
Open Web browser and navigate address bar and type “”. You should see PHP information as shown below. 

Congratulation! this is end of LAMP Stack installation using APT, now let’s see the steps to install LAMP Stack using tasksel command.

Install tasksel to install LAMP Server

tasksel is easy-to-use tool that will install a group of software packages. Following command to install tasksel on Ubuntu 20.04, if you don’t have package.
Update your system
$ sudo apt update && sudo apt upgrade
Next, install tasksel package with following command which will installed dependencies packages also.
$ sudo apt install taskel


Install LAMP Server using tasksel on Ubuntu 20.04 LTS


Following single command to install LAMP Server, this will install all the required packages for LAMP Server


$ sudo tasksel install lamp-server
Check the server status with the following command. Follow the steps 2, 3 and 4 above (for Apache, MySQL and PHP) to check the working of it.
$ sudo systemctl status apache2
$ sudo systemctl status mysql
$ sudo php –version
You may also like to read:
That’s it! In this article, we have seen LAMP Installation on Ubuntu 20.04 LTS. Please share it with others and use comment box below to raise a query if any. Also follow us on tecluesdotcom Facebook Official Page.


Leave a Comment