HomeUBUNTUInstall LAMP Stack on Ubuntu 22.04|20.04|18.04

Install LAMP Stack on Ubuntu 22.04|20.04|18.04

Hello and welcome to our tutorial on how to Install LAMP Stack on Ubuntu 22.04|20.04|18.04. The LAMP stack is the first component you could need if you plan to construct a web application. LAMP is a collection of free and open-source web development tools, including;

  • Linux: Operating system.
  • Apache: Web server.
  • MySQL/MariaDB: Database.
  • PHP: A web scripting language.

Install LAMP Stack on Ubuntu 22.04|20.04|18.04

The steps in this guide will show you how to set up the LAMP stack on Ubuntu 20.04.

Step 1~ Update Ubuntu 22.04|20.04|18.04

Before running any installation on Linux OS, it’s advisable to update and upgrade the system packages to the latest.

Run the following command to update Ubuntu 22.04|20.04|18.04 system:

sudo apt update && apt upgrade
sudo reboot

Step 2~ Install Apache Web Server on Ubuntu 22.04|20.04|18.04

Run the following command to install the Apache web server:

sudo apt install apache2 -y

Start and enable Apache:

sudo systemctl enable apache2 --now

Confirm if Apache is active and running:

$ systemctl status apache2
 apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-09-03 20:43:21 EAT; 28s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 41208 (apache2)
      Tasks: 55 (limit: 4506)
     Memory: 5.0M
     CGroup: /system.slice/apache2.service
             ├─41208 /usr/sbin/apache2 -k start
             ├─41209 /usr/sbin/apache2 -k start
             └─41210 /usr/sbin/apache2 -k start

Sep 03 20:43:21 tutornix systemd[1]: Starting The Apache HTTP Server...

Allow Apache over the firewall if UFW is active:

sudo ufw allow Apache

By going to your browser and typing http://Server IP or hostname into the address bar, you can confirm that Apache is accessible from the outside world. You should be able to arrive at the Apache HTTP server test page.

i.e http://192.168.56.140 or http://tutornix

tutornix is the server hostname.

Install LAMP Stack on Ubuntu
Install LAMP Stack on Ubuntu

Step 3~ Install MariaDB Database Server on Ubuntu 22.04|20.04|18.04

LAMP stack is being used in this demonstration together with MariaDB on Ubuntu 22.04|20.04|18.04.

Run the following command to install MariaDB:

sudo apt install mariadb-server -y

Start and enable the MariaDB service as follows after the installation is finished:

sudo systemctl enable mariadb --now

Check MariaDB status:

$ systemctl status mariadb
 mariadb.service - MariaDB 10.3.34 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-09-03 20:50:47 EAT; 39s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 43143 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 4506)
     Memory: 63.7M
     CGroup: /system.slice/mariadb.service
             └─43143 /usr/sbin/mysqld

Sep 03 20:50:46 tutornix systemd[1]: Starting MariaDB 10.3.34 database server...
Sep 03 20:50:46 tutornix mysqld[43143]: 2022-09-03 20:50:46 0 [Note] /usr/sbin/mysqld (mysqld 10.3.34-MariaDB-0ubuntu0.20.04.1) starting as process 43143 ...
Sep 03 20:50:47 tutornix systemd[1]: Started MariaDB 10.3.34 database server.
Sep 03 20:50:47 tutornix /etc/mysql/debian-start[43178]: Upgrading MySQL tables if necessary.

Create a password for the root user to secure the MariaDB instance:

$ sudo mysql_secure_installation
......
Enter current password for root (enter for none): <ENTER>
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: <NEW PASSWORD>
Re-enter new password: <RE-ENTER PASSWORD>
Password updated successfully!

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

Thanks for using MariaDB!

Now login to MariaDB:

$ sudo mysql -u root -p
Enter password: <Enter Password>
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> QUIT;
Bye

Step 4~ Install PHP on Ubuntu 22.04|20.04|18.04

Run the following command to install PHP:

sudo apt install php -y

Now install PHP required modules:

sudo apt install libapache2-mod-php php-common php-cli php-common php-json php-opcache php-readline -y

Confirm PHP installation by checking the version:

$ php -v
PHP 7.4.3 (cli) (built: Jun 13 2022 13:43:30) ( 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

Now test if PHP is working smoothly by creating a PHP file as shown below: NOTE: Switch to the root user.

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

Restart the Apache web server:

sudo systemctl restart apache2

View the file by typing the URL http://server IP/info.php into the browser:

Install LAMP Stack on Ubuntu
Install LAMP Stack on Ubuntu

Both Apache and PHP are functioning correctly with the above output.

Finale

This concludes our tutorial on setting up the LAMP stack on Ubuntu 22.04|20.04|18.04. We sincerely hope you find this resource beneficial.

Read more about LAMP.

You can also check the following Guides:

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here