How to install LAMP Stack on Ubuntu 20.04 (Focal Fossa)

This guide will discuss how users as users install and set up Apache, MariaDB and PHP (LAMP stack) on Ubuntu 20.04 (Focal Fossa) Linux systems. LAMP is an acronym for – Linux, Apache, MySQL / MariaDB with PHP. The LAMP Stack is not a single software package, but a set of open source tools used to drive web applications and websites. Each component can be used independently for service applications.

Install LAMP Stack on Ubuntu 20.04 (Focal Fossa)

The LAMP Stack contains the following open source software applications.

  • Linux – This is the operating system that hosts the application.
  • Apache – Apache HTTP is a free and open source cross-platform web server.
  • MySQL / MariaDB – Open source relational database management system.
  • PHP -A programming / scripting language for developing web applications.

You can install and configure the LAMP Stack on Ubuntu 20.04 (Focal Fossa) operating system using a virtual machine on a promise, a virtual machine in the cloud, or a dedicated server. The usage account used in this setting requires sudo permissions to install software, edit configuration files, and manage services.

Step 1: Update Ubuntu 20.04 (Focal Fossa)

Before starting to install the LAMP Stack package on Ubuntu 20.04, it is recommended to keep the repositories and packages up to date.

sudo apt update && sudo apt -y upgrade

Step 2: Install MariaDB database server

MariaDB is a relational database management system derived from MySQL. It is free and open source. Use the following guidelines for installation.

How to install MariaDB Server on Ubuntu 20.04 (Focal Fossa)

After installation, make sure you secure the database server. This includes:

  • Set strong root password
  • Delete anonymous user
  • Disable remote login for the root user.
  • Delete the test database and access it

Run the following command to protect the database server.

$ sudo mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user.  If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none):  OK, successfully used password, moving on… Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password:  Re-enter new password:  Password updated successfully! Reloading privilege tables..  … Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them.  This is intended only for testing, and to make the installation go a bit smoother.  You should remove them before moving into a production environment. Remove anonymous users? [Y/n] Y  … Success! Normally, root should only be allowed to connect from 'localhost'.  This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y  … Success! By default, MariaDB comes with a database named 'test' that anyone can access.  This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y Dropping test database… … Success! Removing privileges on test database… … Success!  Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y  … Success! Cleaning up… All done!  If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!

Test MariaDB database installation.

$ mysql -u root -p
 Enter password: 
 Welcome to the MariaDB monitor.  Commands end with ; or g.
 Your MariaDB connection id is 67
Server version: 10.3.19-MariaDB-1 Ubuntu 20.04
 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 MariaDB [(none)]> SELECT VERSION();
 | VERSION()         |
 | 10.3.19-MariaDB-1 |
 1 row in set (0.001 sec)
 MariaDB [(none)]> 

Step 3: Install Apache Web Server

The Apache Web server package is available on the official Ubuntu 20.04 repository. All you need to do is execute the install command using sudo.

sudo apt install -y apache2 apache2-utils

Confirm the Apache version and version.

$ sudo apache2 -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2019-08-14T14:36:32

After installation, the service will start automatically.

$ systemctl status apache2
 ● apache2.service - The Apache HTTP Server
      Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
      Active: active (running) since Tue 2019-12-24 11:17:28 UTC; 20s ago
    Main PID: 721 (apache2)
       Tasks: 55 (limit: 614)
      Memory: 4.9M
      CGroup: /system.slice/apache2.service
              ├─721 /usr/sbin/apache2 -k start
              ├─723 /usr/sbin/apache2 -k start
              └─724 /usr/sbin/apache2 -k start
 Dec 24 11:17:28 ubuntu20 systemd[1]: Starting The Apache HTTP Server…
 Dec 24 11:17:28 ubuntu20 apachectl[720]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using Se>
 Dec 24 11:17:28 ubuntu20 systemd[1]: Started The Apache HTTP Server.

After making changes using the systemctl command, you can restart the service or reload.

sudo systemctl reload apache2sudo systemctl enable apache2

To make the service start at startup, use

sudo systemctl is-enabled apache2

To see the full status of the Apache server, use apache2ctl command.

$ sudo apache2ctl fullstatus 

Your output should look similar to the following.

Open the server IP address in a browser to see the default Apache page.

How to install LAMP Stack on Ubuntu 20.04 (Focal Fossa)

Step 4: Install PHP on Ubuntu 20.04

Now that we have Apache and MariaDB installed, the missing part is PHP. We will install common PHP and standard extensions. The PHP version installed on Ubuntu 20.04 is PHP 7.3.

sudo apt install php libapache2-mod-php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath

If it is not enabled, enable the Apache module and restart the web server.

sudo a2enmod php7.3 

Confirm your PHP version.

$ php -v
PHP 7.3.11-0ubuntu1 (cli) (built: Nov 20 2019 14:21:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
     with Zend OPcache v7.3.11-0ubuntu1, Copyright (c) 1999-2018, by Zend Technologies

Create a php script to test your LAMP stack installation.

echo "" | sudo tee /var/www/html/phpinfo.php

Open your server IP and URL: http://[ServerIP/hostname]/phpinfo.php

How to install LAMP Stack on Ubuntu 20.04 (Focal Fossa)

This gives detailed information about PHP and Apache web servers. This marks the end of our guide on how to install LAMP Stack on Ubuntu 20.04 Linux systems.

Other interesting guides:

How to install phpMyAdmin with Apache on Debian 10 (Buster)

How to monitor Apache Web Server with Prometheus and Grafana in 5 minutes

Monitor MySQL / MariaDB with Prometheus in five minutes