How to install LAMP Stack on Ubuntu 20.04

Introduction

LAMP (Linux, Apache, MySQL, PHP/Perl/Python) is a very common example of a web service stack, named as an acronym of the names of its original four open-source components: the Linux operating system, the Apache HTTP Server, the MySQL relational database management system (RDBMS), and the PHP programming language. The LAMP components are largely interchangeable and not limited to the original selection. As a solution stack, LAMP is suitable for building dynamic web sites and web applications.

Install Apache

Install and start Apache.

sudo apt install apache2 -y
sudo systemctl start apache2.service

Enable Apache to start on boot.

sudo systemctl enable apache2

Install MariaDB

Install MariaDB, a drop-in replacement for MySQL.

sudo apt install mariadb-server mariadb-client -y

Enable MariaDB to start on boot.

sudo systemctl enable mariadb

Secure the database. Answer all the security questions as shown.

sudo mysql_secure_installation

Initially, there is no password for root. Press ENTER.

Enter current password for root (enter for none):
Press ENTER to enter a new password.

Set a root password? [Y/n]
Enter and confirm a root password for MariaDB.

New password:
Re-enter new password:
Password updated successfully!
Press ENTER to remove the anonymous user.

Remove anonymous users? [Y/n]
Press ENTER to disallow remote root logins.

Disallow root login remotely? [Y/n]
Press ENTER to remove the test database.

Remove test database and access to it? [Y/n]
Press ENTER to reload the privilege tables.

Reload privilege tables now? [Y/n]

This completes the basic security configuration.

Install PHP

Install the base PHP package and common extensions.

sudo apt install php php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y

Restart Apache to load PHP.

sudo systemctl restart apache2

Test

Test Apache

To verify Apache is running, navigate to your server’s IP address with your web browser. You will see the default Apache2 page. For example:

http://ipaddress/

Test PHP

As root, execute the following to create the PHP info page.

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

To verify PHP is working, navigate to your info page URL.

http://ipaddress/info.php

Test Database

Connect to the database as root.

mysql -u root -p -h localhost

Create a test user.

CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'test_pass';

Create a test database.

CREATE DATABASE test_database;

Grant the test user privileges on the test database.

GRANT ALL PRIVILEGES ON test_database.* TO 'test_user'@'localhost';

Exit the database client.

quit

As root, create a file to test the PHP database connection.

nano /var/www/html/db_test.php

Add the following to the file:

<?php
$conn = new mysqli('localhost', 'test_user', 'test_pass', 'test_database');

if ($conn->connect_error) {
    die("Failed: " . $conn->connect_error);
}
echo "Success";
?>

To verify the database is reachable by PHP, navigate to your page URL. 

http://ipaddress/db_test.php

You should see “Success”. This verifies that the complete LAMP stack is functioning properly.

Tips and tricks

In this tutorial, you have successfully learned how to install LAMP Stack on Ubuntu 20.04. If you want to learn more about LAMP checkout these documentations:
https://en.wikipedia.org/wiki/LAMP_(software_bundle)
https://ubuntu.com/server/docs/lamp-applications

If you want a server with Ubuntu 20.04 operating system or with LAMP preinstalled, get started now with VPSie and get one month for free.

This tutorial is to help you based on this original tutorial https://www.vultr.com/docs/install-a-lamp-stack-on-ubuntu-20-04-lts please use our tutorial for notes and tips.

Related Articles