How to install LAMP on Ubuntu 21.04 / 20.04

What is LAMP?

The LAMP stack stands for Linux, Apache, MySQL and PHP. LAMP is an open-source software stack. It is used to provide high-performance web applications development. This tutorial will explain how you'll install LAMP on Ubuntu 21.04 and 20.04.

Updatig Package Repository

Update the available packages by using the following command.

sudo apt update -y

Installing Apache

Install Apache by using the following command.

sudo apt install apache2 -y

Verify if Apache is running by using the following command.

sudo systemctl status apache2

Output should look something like this.

apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-04-11 14:36:01 CEST; 20h ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 12175 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 12179 (apache2)
      Tasks: 8 (limit: 2229)
     Memory: 11.7M
     CGroup: /system.slice/apache2.service
             ├─12179 /usr/sbin/apache2 -k start
             ├─12181 /usr/sbin/apache2 -k start
             ├─12182 /usr/sbin/apache2 -k start
             ├─12183 /usr/sbin/apache2 -k start
             ├─12184 /usr/sbin/apache2 -k start
             ├─12185 /usr/sbin/apache2 -k start
             ├─12552 /usr/sbin/apache2 -k start
             └─13118 /usr/sbin/apache2 -k start

Apr 11 14:36:01 testing systemd[1]: Starting The Apache HTTP Server...
Apr 11 14:36:01 testing systemd[1]: Started The Apache HTTP Server.

If Apache is running you should be able to reach the server from within your browser. Go to http://YOUR-IP and you should be prompted with a default Apache page.

Apache Webpage Example

Installing MariaDB

You can install MariaDB by using this command.

sudo apt install mariadb-server mariadb-client -y

Verify if MariaDB is running by using the following command.

sudo systemctl status maridb.service

The output should look something like this.

mariadb.service - MariaDB 10.5.13 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-13 10:21:42 CEST; 42s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 28206 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 28207 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 28209 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemc>
    Process: 28271 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 28273 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 28256 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 15 (limit: 2229)
     Memory: 58.6M
     CGroup: /system.slice/mariadb.service
             └─28256 /usr/sbin/mariadbd

Apr 13 10:21:41 testing mariadbd[28256]: 2022-04-13 10:21:41 0 [Note] InnoDB: Buffer pool(s) load completed at 220413 10:21:41
Apr 13 10:21:42 testing mariadbd[28256]: 2022-04-13 10:21:41 0 [Note] Reading of all Master_info entries succeeded
Apr 13 10:21:42 testing mariadbd[28256]: 2022-04-13 10:21:41 0 [Note] Added new Master_info '' to hash table
Apr 13 10:21:42 testing mariadbd[28256]: 2022-04-13 10:21:41 0 [Note] /usr/sbin/mariadbd: ready for connections.
Apr 13 10:21:42 testing mariadbd[28256]: Version: '10.5.13-MariaDB-0ubuntu0.21.04.1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubuntu 21.04
Apr 13 10:21:42 testing systemd[1]: Started MariaDB 10.5.13 database server.

Configuring MySQL

For a fresh installation of MySQL you probably want to run the security script that comes with it. This script will ask you a couple of questions regarding security. Awnser them to your likings. To run the script you'll need to run the following command:

mysql_secure_installation

Installing PHP

Install PHP by using the following command.

sudo apt install php -y

Installing PHP Modules

You can check which modules are available by using the following command.

apt-cache search php

Once you'll know which modules you want to install. You can install the module by using the following command.

sudo apt install php-<modulename> -y

For example:

sudo apt install php-cli -y

Summary

This tutorial was simple and staightforward. In this tutorial we have seen how you install Apache, MySQL and PHP on a Ubuntu 21.04 or 20.04 server. You have now installed LAMP on your Ubuntu 21.04 or 20.04 server.

In case you have any questions make sure to let us know via our community.