How to Install LAMP in Ubuntu

Introduction

The LAMP stack is a set of open-source tools used for web application development. For a web application to work, it has to include a server operating system, a web server, a database, and a programming language. Each layer of software is necessary for creating a database-driven and dynamic website.

This step-by-step tutorial shows you how to install LAMP in Ubuntu.

How to install LAMP in Ubuntu.

Prerequisites

  • Ubuntu 18.04 or later
  • User with sudo privileges
  • Access to a terminal/command line

How to Install LAMP in Ubuntu

LAMP is a collection of four components that make up a fully functional web development environment. The LAMP acronym contains the initials of the components' names:

  • Linux Operating System
  • Apache HTTP Server
  • MySQL database management system
  • PHP programming language (Pearl and Python are also sometimes used in the stack)

Follow the steps below to install each tool on your system.

Step 1: Install Apache

Apache HTTP Server is the web server running on top of Linux in the LAMP stack. The web server uses HTTP to process requests and transmit information through the internet.

Follow the procedure below to install Apache.

1. Before installing the first LAMP component, ensure the package list on the system is up to date. In the terminal, type:

sudo apt update

2. To install the Apache package, run the following command:

sudo apt install apache2 -y

Note: The -y flag allows skipping the installation confirmation prompt.

Installing Apache web server in Ubuntu.

3. Check if Apache installed correctly by checking the Apache service status:

sudo service apache2 status

The service shows as running in the output:

Checking if the Apache service is running in Ubuntu.

Exit the status screen by pressing Ctrl + C on the keyboard.

4. Next, make sure that the UFW firewall contains the Apache profiles by typing in the following command:

sudo ufw app list
Checking if Apache profiles exist in ufw.

5. Ensure the Apache Full profile allows the traffic on ports 80 and 443 by running the command:

sudo ufw app info "Apache Full"

The output should look similar to the following example:

Checking the ports of the Apache Full profile in ufw.

6. To confirm that Apache is running, enter the IP address of your server in the address bar of an internet browser and press ENTER.

The test Apache web server page should display as below.

Apache2 Ubuntu Default Page.

Note: You can also access the Apache test page by typing localhost in the address bar.

Step 2: Install MySQL and Create a Database

MySQL is a relational database management system for creating and maintaining dynamic enterprise-level databases. It is compatible with all major OS platforms, which makes it a good fit for web application development.

Note: Refer to our article and find out what is a relational database.

Install MySQL by typing the following command:

sudo apt install mysql-server -y
Installing MySQL server in Ubuntu.

Step 3: Install PHP

Although other programming languages, such as Python and Pearl, also work well within LAMP, PHP is usually the final layer of the stack because it integrates well with MySQL. As a dynamically typed language, PHP embeds into HTML, improving the speed and reducing the complexity of web applications.

Install PHP by following the steps below.

1. Obtain the necessary PHP packages by typing:

sudo apt install php libapache2-mod-php php-mysql -y
Installing PHP in Ubuntu.

2. Modify the way Apache serves files by opening the dir.conf file in a text editor with root privileges:

sudo nano /etc/apache2/mods-enabled/dir.conf

The configuration file looks like in the example below:

Editing the dir.conf file in Ubuntu.

By default, Apache first looks for an index.html file card.

3. Edit the list so that the index.php file is in the first position:

The dir.conf file edited to list the PHP page first.

4. Press CTRL + X to save and close the file. Press y and ENTER to confirm.

Install PHP Modules (Optional)

If necessary, add more modules to improve the functionality of PHP. Search, view, and install various libraries and modules by following the procedure below.

1.  Get a list of available PHP modules with:

apt-cache search php- | less

The command pipes the results of the apt-cache search into less to simplify viewing the output.

The output of apt-cache in Ubuntu.

2. Scroll up and down by using the arrow keys to see all the options, including a short description for each module.

3. For example, to find out what the module php7.4-tidy does, type:

apt-cache show php7.4-tidy

The output displays the module description.

Checking package details using apt-cache.

4. To install the php7.4-tidy package after viewing its description, use the following command:

sudo apt install php7.4-tidy

5. When you finish, press q to quit.

Step 4: Restart Apache

For the changes to take effect, restart the Apache service by typing:

sudo systemctl restart apache2

If the command executes correctly, it returns no output.

Step 5: Test PHP Processing on Web Server

To test the new LAMP installation, create a basic PHP script and place it in the web root directory located at /var/www/html/, then check if the script is accessible via an internet browser. The steps below explain the procedure for performing this test.

1. Create a file in the web root directory by typing the following command:

sudo nano /var/www/html/info.php

2. Inside the file, type the PHP code:

<?php
phpinfo ();
?>
Creating an info.php file in Nano.

3. Press CTRL + X to save and close the file. Press y and ENTER to confirm.

4. Open an internet browser and type the following address:

[server-ip-address]/info.php

Alternatively, type:

localhost/info.php

The output should display the details of the LAMP stack, as seen in the image below:

Viewing the PHP version in Firefox.

Note: Hosting a web application on our Bare Metal Cloud platform allows you to eliminate the virtualization overhead and improve the overall app performance.

Conclusion

By following this guide, you successfully installed each layer of the software required to build the LAMP stack on Ubuntu. With LAMP, you have everything necessary to start web application development.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Install XAMPP on Ubuntu 18.04
June 5, 2019

The XAMPP stack is an open-source Apache distribution of a PHP development environment consisting of...
Read more
How to Install the LAMP Stack on CentOS 7
May 14, 2019

The LAMP stack is a bundle consisting of a Linux operating system, an Apache server, a MySQL database, and...
Read more
What is a LAMP Stack?
January 6, 2022

The LAMP stack is a set of open source software used for web application development. For a web application...
Read more
How to Update Linux Kernel In Ubuntu
October 22, 2018

The Linux kernel is like the central core of the operating system. It works as sort of a mediator, providing...
Read more