How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu

Harshal Shukla
5 min readJul 17, 2023

--

In this blog, I will walk you through the steps to install LAMP Stack on Ubuntu 20.04 server.

Introduction:

A LAMP stack is a bundle of four different software technologies that developers use to build websites and web applications. LAMP is an acronym for the operating system, Linux; the web server, Apache; the database server, MySQL; and the programming language, PHP. All four of these technologies are open source, which means they are community maintained and freely available for anyone to use. Developers use LAMP stacks to create, host, and maintain web content. It is a popular solution that powers many of the websites you commonly use today.

The illustration below shows how the layers stack together:

Prerequisites:

In order to complete this tutorial, you will need to have an Ubuntu 20.04 server with a non-root sudo-enabled user account and a basic firewall.

Step 1-Install Apache and Update the Firewall

Apache is a free and open-source software that allows users to deploy their websites on the internet. It is one of the oldest and most reliable web server software maintained by the Apache Software Foundation, with the first version released in 1995.

First we need to install apache on our Virtual machine but before that lets update the necessary softwares by following command.

$ sudo apt update -y

Then, install Apache with:

$ sudo apt install apache2 -y

Once the installation is finished, you’ll need to adjust your firewall settings to allow HTTP traffic. UFW has different application profiles that you can leverage for accomplishing that. To list all currently available UFW application profiles, you can run:

$ sudo ufw app list

You’ll see output like this:

Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

To only allow traffic on port 80, use the Apache profile:

$ sudo ufw allow in “Apache”
OUTPUT

Rules updated
Rules updated (v6)

You can do a check right away to verify it by visiting your server’s public IP address in your web browser. Here you can get your public IP:

$ ip address

Do visit your server’s public IP address in your web browser:

http://your_server_ip

You’ll see the default Ubuntu 20.04 Apache web page:

Step 2-Installing MYSQL

Now, you need to install the database system to be able to store and manage data for your site. MySQL is a popular database management system used within PHP environments.

$ sudo apt install mysql-server -y

When installation is done, run the following script. This script will remove some insecure default settings and lock down access to your database system.

$ sudo mysql_secure_installation

Answer Y for yes, or anything else to continue without enabling.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:

If you answer “yes”, you’ll be asked to select a level of password validation. Keep in mind that if you enter 2 for the strongest level, you will receive errors when attempting to set any password which does not contain numbers, upper and lowercase letters, and special characters, or which is based on common dictionary words.

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

If you enabled password validation, you’ll be shown the password strength for the root password you just entered and your server will ask if you want to continue with that password. If you are happy with your current password, enter Y for “yes” at the prompt:

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

When you’re finished, type the following command to enter into mysql console:

$ sudo mysql
Output
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.34-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

To exit the MySQL console, type:

mysql> exit

Step 3-Installing PHP

PHP is the component of our setup that will process code to display dynamic content to the final user. 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.

Run the following command to install the packages:

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

Step 4-Test the PHP Processing on your Web Server

Create the directory for your_domain as follows:

$ sudo mkdir /var/www/your_domain

In order for Apache to find this file and serve it correctly, it must be saved to your web root directory.

$ sudo nano /var/www/your_domain/info.php

Insert the following command to show the php information:

<?php
phpinfo();
?>

Save & Close it by ctrl + c, then y and then Enter. To test this script, go to your web browser and access your server’s domain name or IP address, followed by the script name, which in this case is info.php:

http://your_domain_or_IP/info.php

You’ll see a page similar to this:

Now that you have a LAMP stack installed, you have many choices for what to do next. You’ve installed a platform that will allow you to install most kinds of websites and web software on your server.

Conclusion:

In this guide, we’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as web server and MySQL as database system.

In case of any questions regarding this article, please feel free to comment in the comment section or contact me via LinkedIn.

I want to thank my team Guysinthecloud for all of their Support & Guidance.

Thank You

--

--

Harshal Shukla

Lead @Code Vipassana || Facilitator & Innovator @Google Cloud || Google Cloud, Microsoft Azure, Oracle Cloud Certified || Cloud and DevOps Enthusiast