How to Install LAMP Stack with MariaDB on Ubuntu 22.04

A LAMP stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MySQL or MariaDB database, and dynamic content is processed by PHP.

First, check for any pending system upgrades.

apt update
apt upgrade

Install Apache

Command to install Apache along with its utilities.

apt install -y apache2 apache2-utils

Next, check the Status of Apache.

systemctl status apache2

Output:

root@server:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-03-14 21:07:34 UTC; 45min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 22154 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 22158 (apache2)
      Tasks: 7 (limit: 1074)
     Memory: 13.7M
        CPU: 294ms
     CGroup: /system.slice/apache2.service
             ├─22158 /usr/sbin/apache2 -k start
             ├─22159 /usr/sbin/apache2 -k start
             ├─22160 /usr/sbin/apache2 -k start
             ├─22161 /usr/sbin/apache2 -k start
             ├─22162 /usr/sbin/apache2 -k start
             ├─22163 /usr/sbin/apache2 -k start
             └─22195 /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 start Apache at boot time.

systemctl enable apache2

You can confirm the Apache2 version with the below command,

apache2 -v

Output:

Server version: Apache/2.4.52 (Ubuntu)
Server built:   2022-02-03T18:25:47

Enable and Configure Firewall

To enable HTTP TCP port,

ufw allow http

Output:

Rules updated
Rules updated (v6)
Now you can verify whether the apache is installed correctly or not by opening it in a web browser.

http://ip-address

NOTE: Replace with your Server IP address

images

Install MariaDB Server

To install MariaDB server, run the below command:

apt install mariadb-server mariadb-client

To check the status of MariaDB.

systemctl status mariadb

Output:

root@server:~# systemctl status mariadb

● mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-03-14 20:55:55 UTC; 57min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 14311 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 7 (limit: 1074)
     Memory: 57.2M
        CPU: 1.266s
     CGroup: /system.slice/mariadb.service
             └─14311 /usr/sbin/mariadbd

To start MariaDB if it is not active.

systemctl start mariadb

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

systemctl enable mariadb

Next, MariaDB database security.

NOTE: In this step, you will be prompted with 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 to MariaDB.

 mariadb -u root -p

To exit from MariaDB.

exit

To check MariaDB Version.

mariadb --version

Output:

root@server:~# mariadb --version
mariadb  Ver 15.1 Distrib 10.3.25-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Install PHP

PHP 8.1 is the default version of PHP that would be installed on Ubuntu 22.04.

To install PHP.

apt install php libapache2-mod-php php-mysql php-common php-cli php-common php-json php-opcache php-readline php-mbstring php-gd php-dom php-zip php-curl

To Enable the Apache PHP module and restart the Apache Web server.

systemctl restart apache2

To check PHP Version.

php --version

Output:

root@crown:~# php --version
PHP 8.1.0 (cli) (built: Nov 25 2021 19:57:29) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies

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

nano /var/www/html/info.php

Add the following to the file.

<?php phpinfo(); ?>

To verify enter the following link in a web browser.

NOTE: Replace with your server IP address below.

http://ip-address/info.php

images

Run PHP-FPM with Apache

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

NOTE: This is an optional step, PHP 8.1 is used to run the PHP code but if you want to run PHP code with PHP-FPM follow the below steps.

First, Let us disable PHP,

a2dismod php

Next, Install PHP-FPM.

apt install php8.1-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 php8.1-fpm file.

a2enconf php8.1-fpm

Restart the Apache.

systemctl restart apache2

To enable php-fpm.

systemctl enable php8.1-fpm

To start php-fpm.

systemctl start php8.1-fpm

To check the status of php-fpm.

systemctl status php8.1-fpm

Output:

root@crown:~# systemctl status php8.1-fpm
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-03-14 22:16:17 UTC; 29s ago
       Docs: man:php-fpm8.1(8)
   Main PID: 24942 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1074)
     Memory: 7.3M
        CPU: 48ms
     CGroup: /system.slice/php8.1-fpm.service

Now you have successfully installed the LAMP stack (Apache, MariaDB, and PHP8.1) on Ubuntu 22.04.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS