What is a LAMP stack?

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

The name LAMP is an acronym of the following programs:

  • Linux Operating System
  • Apache HTTP Server
  • MySQL database management system
  • PHP programming language
layers that make up the lamp stack
The layering of the LAMP stack

It is critical to install each component of the stack separately and in a specific order. Here is a step-by-step tutorial on how to install the LAMP Stack on Ubuntu 18.04.

Prerequisites

  • Ubuntu 18.04 Bionic Beaver
  • User with sudo privileges
  • Access to a terminal/command line

Tools/Software

  • The apt-get tool, pre-loaded in Ubuntu and other Debian-based distros.

Install LAMP On Ubuntu 18.04

Note: This article presumes that you already have installed the Linux operating system and will not include the process for it.

Step 1: Update Package Repository Cache

Before you begin:

1. Open the terminal either by using the CTRL+ALT+T keyboard shortcut or by running a search in Ubuntu Dash, typing in the word “terminal.”

Select the Terminal Icon as seen in the image below.

Ubuntu Dash terminal in apache

2. Make sure to update the package repository cache to ensure it installs the latest versions of the software. To do so, type in the following command:

sudo apt-get update

Step 2: Install Apache

1. To install Apache, run the following command in the terminal:

sudo apt-get install apache2

Press y (yes) and hit ENTER to permit the installation.

2. Check if Apache is installed correctly, by running the Apache service status with the command:

sudo service apache2 status

If everything installed correctly, you will receive output as seen in the image below:

apache is running

3. Next, make sure that the UFW firewall has an application profile for Apache by typing in the following command:

sudo ufw app list

In the Apache Full profile, you should make sure it enables traffic to ports 80 and 443. Find this by typing the command:

sudo ufw app info “Apache Full”

You will see the output as in the following image:

verifying the UFW firewall has an application profile

4. To ensure Apache is running, enter the IP address of your server in the address bar and press ENTER.

The test Apache web server page should display as below.

Apache Ubuntu Default screenshot image
Apache2 Ubuntu Default Page

Note: To identify the server’s public IP address, run the command:

sudo apt-get install curl
curl http://icanhazip.com

Step 3: Install MySQL and Create a Database

To install MySQL, type in the following command:

sudo apt-get install mysql-server

Allow the installation by pressing y and ENTER.
During the installation, you will be prompted to set the root user password.

Note: Although it is not mandatory to set the root password, it is recommended as a precautionary safety measure.

Step 4: Install PHP

1. To install PHP, run the following command:

sudo apt-get install php libapache2-mod-php php-mysql

Press y and ENTER to allow the installation.

2. Next, you should modify the way Apache serves files when directories are requested. By default, Apache looks first for a file card named index.html. However, we want it to prefer the index.php file.

To change this, open the dir.conf file in a text editor with root privileges:

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

In the configuration file, you will see the information as in the image below:

configuration file screenshot of code

You now need to move the PHP index file to the first position:

moving the PHP index file example

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

Step 5: Restart Apache

In order for the changes to take effect, you must restart the Apache service.

Type the command:

sudo systemctl restart apache2

Optional: Install PHP Modules

You can add additional modules to improve the functionality of PHP. You can search through a variety of libraries and modules by piping (listing in chunks) the results of the apt-cache search into less, allowing to scroll through the output of other commands.

1.  type in the following command:

apt-cache search php- | less

2. Once you have opened it, scroll up and down by using the arrow keys and see all the options as well as a short description for each.

For example, to find out what the module php-cli does, type:

apt-cache show php-cli

This will display the description of the module. If you decide to install the package, after viewing its description, you can do so by using the following command:

sudo apt-get install php-cli

3. When finished, press q to quit.

Step 6: Test PHP Processing on Web Server

1. Create a basic PHP script and save it to the “web root” directory. This is necessary for Apache to find and serve the file correctly. This directory is located at /var/www/html/.

To create a file in that directory, type in the following command in the terminal:

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

That will open the bank file.

2. Inside the file, type in the valid PHP code:

<?php

phpinfo ();

?>

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

4. Open a browser and type in your IP address/info.php
The output should display the details of the LAMP stack as seen in the image below:

 

Conclusion

By following this guide, you should have successfully installed each layer of software required to build the LAMP stack on Ubuntu 18.04.

With it, you have everything necessary for web application development.