How To Install LAMP Stack on Ubuntu 22.04

In this article, we want to teach you How To Install LAMP Stack on Ubuntu 22.04.

The LAMP stack is a popular open-source solution stack used primarily in web development.

LAMP consists of four components necessary to establish a fully functional web development environment. The first letters of the components’ names make up the LAMP acronym:

  • Linux is an operating system used to run the rest of the components.
  • Apache HTTP Server is a web server software used to serve static web pages.
  • MySQL is a relational database management system used for creating and managing web databases, but also for data warehousing, application logging, e-commerce, etc.
  • PHP, Perl, and Python are programming languages used to create web applications.

Each component represents an essential layer of the stack. Together, the components are used to create database-driven, dynamic websites.

How To Install LAMP Stack on Ubuntu 22.04

Before you start to install the LAMP stack, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our article the Initial Server Setup with Ubuntu 22.04.

Now follow the steps below to complete this guide.

Install Apache on Ubuntu 22.04

The first step of installing the LAMP stack is to install Apache on your server.

First, update your local package index with the following command:

sudo apt update

Then, use the following command to install Apache on Ubuntu 22.04:

sudo apt install apache2

Now you need to allow the HTTP traffic through the UFW firewall. To do this, use the command below to list all currently available UFW application profiles:

sudo ufw app list
Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

Here we just need to allow port 80. So run the following command to allow port 80:

sudo ufw allow in "Apache"

You can check your UFW firewall with the following command:

sudo ufw status
Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                                
Apache                     ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)                    
Apache (v6)                ALLOW       Anywhere (v6)   

At this point, you can see your default Ubuntu 22.04 Apache web page by typing your server’s IP address in your web browser:

http://your_server_ip

You will see the following screen:

Apache landing page on Ubuntu 22.04

Note: You can find your server’s IP address with one of the following commands:

ip addr show ens3 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
curl http://icanhazip.com

Install MariaDB on Ubuntu 20.04

At this point, you will install MariaDB or MySQL the second step of installing the LAMP on Ubuntu 22.04.

MariaDB is a relational database management system forked from MySQL. It is free and Open-source.

To install MariaDB on Ubuntu 22.04, run the following command:

sudo apt install mariadb-server

When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL.:

sudo mysql_secure_installation

You will be asked to:

  • Setting a strong root password
  • Removing anonymous users
  • Disabling remote login for the root user.
  • Removing the test database and access to it.

Answer them to continue.

When you’re finished, test whether you’re able to log in to the MariaDB console by typing:

sudo mysql -u root -p

Then, you can exit from your MariaDB shell with the command below:

MariaDB [(none)]> exit

Install PHP on Ubuntu 22.04

At this point, you can install PHP on Ubuntu 22.04, the final step of installing the LAMP stack.

In addition to the php package, you’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP files. Core PHP packages will automatically be installed as dependencies.

To install the PHP and required packages, run the following command:

sudo apt install php libapache2-mod-php php-mysql

Verify your PHP installation by checking its version:

php -v
Output
PHP 8.1.2 (cli) (built: Mar  4 2022 18:13:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

Now you can create a PHP test script to confirm that Apache is able to handle and process requests for PHP files on Ubuntu 22.04:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Then, type your server’s IP address in your web browser followed by info.php:

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

Here is an example of the default PHP web page:

PHP landing page on Ubuntu 22.04

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your Ubuntu server. Use rm to do so:

sudo rm /var/www/your-server-ip/info.php

You can always recreate this page if you need to access the information again later.

Conclusion

At this point, you learn to Install LAMP Stack on Ubuntu 22.04.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!