How to Install LAMP Stack with MariaDB on Ubuntu 20.04

First, check for any pending system upgrade.

apt update
apt upgrade

Install Apache

A command to install apache2 and with its utilities.

apt install -y apache2 apache2-utils

Next, check the Status of Apache.

systemctl status apache2

Output:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Tue 2020-04-28 17:05:36 UTC; 1min 35s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 13286 (apache2)
      Tasks: 55 (limit: 1075)
     Memory: 5.1M
     CGroup: /system.slice/apache2.service
             ├─13286 /usr/sbin/apache2 -k start
             ├─13288 /usr/sbin/apache2 -k start
             └─13289 /usr/sbin/apache2 -k start

If Apache is not active can start using the following command.

systemctl start apache2

Use the following command to auto starts apache at boot time.

systemctl enable apache2

To check Apache Version.

apache2 -v

Output:

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

To Enable Firewall.

To enable TCP port.

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

To allow the Http

ufw allow http

Output:

Rules updated
Rules updated (v6)

Now you can verify whether the apache installed correctly or not by opening it in a web browser.

NOTE: Replace your Server IP-address below.

http://ip-address

LAMP

Install MariaDB Database Server

To install MariaDB.

apt install mariadb-server mariadb-client

To check the status of MariaDB.

systemctl status mariadb

Output:

● mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor prese>
     Active: active (running) since Tue 2020-04-28 17:26:53 UTC; 1min 18s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 14342 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 1075)
     Memory: 68.6M
     CGroup: /system.slice/mariadb.service
             └─14342 /usr/sbin/mysqld

To start MariaDB if it is not active.

systemctl start mariadb

Use the following command to auto starts MariaDB at boot time.

systemctl enable mariadb

Next, MariaDB database security.

NOTE: In this step you will prompt several questions.

mysql_secure_installation

Output:

Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

To login MariaDB.

 mariadb -u root

To exit from MariaDB.

exit;

To check MariaDB Version.

mariadb --version

Output:

mariadb  Ver 15.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Install PHP7.4

To install PHP.

apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline

To Enable the Apache php7.4 module and restart the Apache Web server.

a2enmod php7.4

systemctl restart apache2

To check PHP Version.

php --version

Output:

PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( 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

To test PHP scripts we need to add info.php file in the document.

nano /var/www/html/info.php

Add the following in the file.

<?php phpinfo(); ?>

To verify enter the following link in a web browser.

NOTE:Replace your server IP-address below.

http://server-ip-address/info.php

LAMP

To Run PHP-FPM with Apache.[Optional]

NOTE: This is optional step PHP 7.4 is used to run the PHP code but if you want to run PHP code with PHP-FPM follow this step.

First, Disable PHP 7.4.

a2dismod php7.4

Next. Install PHP 7.4 FPM.

apt install php7.4-fpm

Enable proxy_fcgi and setenvif module.

a2enmod proxy_fcgi setenvif

Output:

Considering dependency proxy for proxy_fcgi:
Enabling module proxy.
Enabling module proxy_fcgi.
Module setenvif already enabled
To activate the new configuration, you need to run:
  systemctl restart apache2

To enable php7.4-fpm file.

a2enconf php7.4-fpm

Restart the Apache.

systemctl restart apache2

To verify enter the following link in a web browser.

NOTE:Replace your server IP-address below.

http://server-ip-address/info.php

LAMP

Now you have successfully installed LAMP stack (Apache, MariaDB, and PHP7.4) on Ubuntu 20.04.