Install LAMP Stack on Ubuntu 20.04

LAMP is a group of open-source software comprising Linux, Apache, MySQL or MariaDB, and PHP. Just like the LEMP stack which contains NGINX instead of Apache, LAMP is also used for developing and deploying web solutions; including dynamic websites and web apps.

In this guide, we are going to install the LAMP stack on Ubuntu 20.04.

Prerequisites

  • A Linux server running Ubuntu 20.04. This takes care of the “L” in LAMP
  • A user with sudo privileges

Install Apache on Ubuntu

The Apache HTTP server is used for serving resources to clients over the web. Before anything else, update your Ubuntu packages with the command below if you have not done so recently.

$ sudo apt update

Next, run the following command to install Apache version 2 on Ubuntu 20.04.

$ sudo apt install apache2

If you are prompted, enter y to continue with the installation.

Once installed, you can check the version of the Apache2 HTTP server with:

$ sudo apache2 -v

Also, confirm that the Apache2 server is active (running) with the next command.

$ sudo systemctl apache2
Check Apache2 Service Status
Check Apache2 Service Status

Now, open a web browser and enter your server’s IP address. Or enter localhost if you are locally connected to the server.

You should see the Apache2 Ubuntu Default Page as shown in the image below.

Apache2 Ubuntu Default Page
Apache2 Ubuntu Default Page

You may host multiple sites on a single Apache2 web server and connect your registered domains accordingly.

This takes care of the “A” in LAMP.

Install MariaDB on Ubuntu

For the “M” part of the LAMP acronym, we need to install a database management system to store and manage site/application data. Let us opt for MariaDB. MariaDB is a derivative of MySQL, however, it offers relatively faster performance.

Run the command below to install MariaDB on Ubuntu 20.04.

$ sudo apt install mariadb-server

Enter y if prompted to continue with the installation.

After MariaDB is successfully installed, check the service status with the next command.

$ sudo systemctl status mariadb
Check MariaDB Service Status
Check MariaDB Service Status

You should see that the MariaDB service is active (running) already. You may press q to return to the shell prompt. Otherwise, start the service with the command below and then check the status again.

$ sudo systemctl start mariadb

Next, run the command below to run the mysql_secure_installation script and then follow the instructions to secure MariaDB.

$ mysql_secure_installation

The following is a sample of what is expected after you run the script.

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current password for the root user.  If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

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!


By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Now, run the next command to login to mysql with your chosen password.

$ sudo mysql -u root -p

If you see the MariaDB prompt, you are on course.

Install PHP on Ubuntu

PHP (Hypertext Preprocessor) is a server side scripting language which enables the dynamic processing of web contents while interacting with databases. Run the command below to install PHP and associated components.

$ sudo apt install php php-mysql

Check the installed version of PHP with:

$ php -v
Check PHP Version
Check PHP Version

To confirm that PHP is working correctly, let’s perform a simple test as follows.

Create an index.php file in the default website root directory with the command below.

$ sudo nano /var/www/html/index.php

Paste the sample PHP code below in the text editor.

<?php
phpinfo();
?>

Save changes and close the file.

Restart Apache2 with:

$ sudo systemctl restart apache2

Now, open a web browser and visit ServerIP/index.php. For example, 192.11.9.2/index.php.

If everything is in order, you should see a page displaying information about your PHP installation similar to what is shown in the image below.

Test Apache2 PHP Processing
Test Apache2 PHP Processing

Conclusion

In this guide, we successfully set up the LAMP stack comprising Ubuntu 20.04 Linux server, Apache2, MariaDB and PHP. You are all set to start developing, testing and deploying dynamic websites and web apps.

1 thought on “Install LAMP Stack on Ubuntu 20.04”... add one

Leave a Comment