How to Install LAMP Stack with MariaDB on Ubuntu 21.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:

[email protected]:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Thu 2021-04-29 16:37:46 UTC; 16s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 31366 (apache2)
      Tasks: 55 (limit: 2280)
     Memory: 5.5M
     CGroup: /system.slice/apache2.service
             ├─31366 /usr/sbin/apache2 -k start
             ├─31367 /usr/sbin/apache2 -k start
             └─31368 /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-08-12T19:46: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:

[email protected]:~# systemctl status mariadb
● mariadb.service - MariaDB 10.3.25 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor prese>
     Active: active (running) since Thu 2021-04-29 16:41:06 UTC; 15s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 33004 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2280)
     Memory: 64.7M
     CGroup: /system.slice/mariadb.service
             └─33004 /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:

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!

To login MariaDB.

 mariadb -u root

To exit from MariaDB.

exit;

To check MariaDB Version.

mariadb --version

Output:

[email protected]:~# mariadb --version
mariadb  Ver 15.1 Distrib 10.3.25-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:

[email protected]:~# php --version
PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( 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]

FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for heavy-loaded sites.

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 enable php7.4-fpm.

systemctl enable php7.4-fpm

To start php7.4-fpm.

systemctl start php7.4-fpm

To check the status of php7.4-fpm.

systemctl status php7.4-fpm

Output:

[email protected]:~# systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: >
     Active: active (running) since Thu 2021-04-29 17:13:08 UTC; 15min ago
       Docs: man:php-fpm7.4(8)
    Process: 42271 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/>
   Main PID: 42263 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 1, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2280)
     Memory: 8.1M
     CGroup: /system.slice/php7.4-fpm.service

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 21.04.