The LAMP (Linux, Apache, MySQL and PHP) stack is one of the approaches to building dynamic websites. A LAMP stack uses mature and reliable opensource software as building blocks and has been the cornerstone for a variety of projects including WordPress, Facebook, and Wikipedia.

Installing and configuring LAMP stack is an important skill for Linux administrators and web developers skill. So, Let’s begin with step-by-step tutorial on how to install and configure a basic LAMP stack on Ubuntu 20.04.

As a bonus, we’ll review how Tasksel can help you speed up the process.

What Is A LAMP Stack?

A LAMP stack is a set of four popular opensource components for web development.
Each of the letters in the LAMP acronym stands for one of the components:

  • Linux is the operating system in the stack.
  • Apache is the web server in the stack.
  • MySQL is the database in the stack.
  • PHP is the programming language in the stack.

With these components, web developers have all the components they need to build web applications that can serve dynamic content. Linux acts as the base layer where the other components are installed, Apache serves HTTP(S) pages, MySQL allows data persistence, and PHP allows developers to tie the web server and database layers together and create dynamic sites. For example, PHP can take data from a webform and perform CRUD (create, read, update, delete) operations on a MySQL database.

LAMP is prescriptive in which web server, database, and programming languages a developer should use, but there is flexibility in the Linux distribution. Ubuntu, Debian, RHEL, and CentOS/Rocky Linux are some of the most common distros used in LAMP stacks, but it’s mostly a matter of preference.

Additionally, there are plenty of alternatives to the traditional LAMP stack. For example, some developers use Python or Perl instead of PHP in their stacks. Similarly, some use nginx (pronounced “engine x”) instead of Apache to make a LEMP stack or replace MySQL with a different database or data persistence layer like PostgreSQL. There are also more fundamentally different LAMP alternatives like MEAN (MongoDB, Express.js, Angular.js, Node.js).

How To Install A LAMP Stack Quickly With Tasksel

We’ll walk through the process of installing each individual LAMP stack component below, and we recommend following that process if it’s your first time configuring a LAMP stack.

But, if you’re looking for a quick way to hit the ground running on Ubuntu/Debian, consider using Tasksel.

Tasksel is a tool that helps Linux administrators install multiple related packages at the same time, and it installs the Apache, MySQL, and PHP components you need for a LAMP stack with just one command. If you are intended to use Tasksel than, lets install it first.

  1. Install Tasksel:  sudo apt install tasksel -y
  2. Now let’s use Tasksel to install lamp-server:   sudo tasksel install lamp-server

After the lamp-server installed by Tasksel, you should see that Apache2, MySQL, and PHP are all installed on your server.

Step by Step LAMP Stack) On Ubuntu 20.04

  • First, let’s update our apt package lists and repositories with this command:   sudo apt update
  • Next, use apt to install the Apache webserver with this command:  sudo apt install apache2 -y
  • Next install MySQL/MariaDB Server:   sudo apt install mysql-server -y

To check the Version of installed MySQL Server run mysql -V command in terminal. For deep dive on the MySQL installation and configuration process — including information on hardening your installation — check out How to Install and Configure MySQL on Ubuntu 20.04.

  • Now install PHP the scripting language of our LAMP stack with  install 3 essential packages:

php is the default package for the PHP scripting language
libapache2-mod-php is the PHP module for Apache
php-mysql is the PHP module for MySQL

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

Now lets verify our php installation.php -v

Please check with Firewall as well. |optional|

By default, Ubuntu’s uncomplicated firewall a.k.a. ufw is disabled. You can enable ufw to implement firewall policies and secure your server. If you choose to leave ufw disabled, you can skip this step.

If you’re not sure if ufw is enabled on your system, you can check its status with this command:  sudo ufw status

If ufw is enabled, there are three available applications (listed in the output of sudo ufw app list) related to Apache you can allow through the firewall:

  • Apache will allow traffic on TCP port 80 (default HTTP) only
  • Apache Secure will allow traffic on TCP port 443 (default HTTPS) only
  • Apache Full will allow traffic on both TCP ports 80 and 443

For our example, will allow inbound traffic to Apache on TCP ports 80 and 443 with this command:  sudo ufw allow in “Apache Full”

You will see output similar to:

Rule added
Rule added (v6)

Now let’s test your Apache setup.

Use web browser to access http://<your server's IP> (where <your server’s IP> is the IP address of your Ubuntu machine).

You should see the default Apache2 Ubuntu Default Page.

Now its time to Configure And Enable Virtual Host

With all the LAMP stack components installed, you could now move on to testing PHP processing and MySQL connectivity in the subsequent steps. By default, Apache will serve web pages in the folder /var/www/html

Apache Virtual Hosts let you create multiple sites on a single server, let’s create a Virtual Host. Lets Name our site testwebsite.local. Please replace testwebsite.local with any of your desired Virtual Host Name i.e. your domain name.

 

First of all lets disable the default virtual host:

sudo a2dissite 000-default.conf

Duplicate the 000-default.conf Apache configuration file to a new /etc/apache2/sites-available/testwebsite.local.conf file:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testwebsite.local.conf

Use a text editor like vim to edit the /etc/apache2/sites-available/testwebsite.local.conf to contain configuration settings:

<directory var=”” www=”” html=”” cherryservers=”” local=”” public=””>

Require all granted

</directory>

<virtualhost :80=””>

ServerName cherryservers.local
ServerAlias www.testwebsite.local
ServerAdmin otsglobal@localhost
DocumentRoot /var/www/html/testwebsite.local/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</virtualhost>

 

Create the DocumentRoot directory from the cherryservers.local.conf file:

sudo mkdir -p /var/www/html/testsebsite.local/public

Make the www-data user and group owner of this folder and its contents:

sudo chown -R www-data:www-data /var/www/html/testwensite.local/public

Grant www-data user read, write, and execute permissions on this folder and its contents, and give everyone else read, execute permissions:

sudo chmod -R 755 /var/www/html/testwebsite.local/public

Lets link our site to Apache sites-enabled and reload Apache configurations:

sudo a2ensite testwebsite.local

sudo systemctl reload apache2

…….

Lets create our first MySQL Database

sudo mysql

CREATE DATABASE testDB;

Next create a MySQL user named user1 with a password  (replace password with your desired one):

CREATE USER ‘phpUser’@’%’ IDENTIFIED WITH mysql_native_password BY ‘password’;

Grant the user1 full privileges on the testDB database:

GRANT ALL ON testDB.* TO ‘phpUser’@’%’;

Create a customers table in the testDB

CREATE TABLE testDB.customers(name VARCHAR(50), description VARCHAR(255));

Add some test data to the customers table:

INSERT INTO testDB.customers(name, description) VALUES (‘Ahmad Ali’, ‘Muhammad Fiaz’);

Exit MySQL:  exit

OK! Lets test PHP And MySQL

Now we are able to create a PHP script to display data from our MySQL database.

/var/www/html/testwebsite.local/public/index.php with this content (be sure to change your “password” to match the MySQL credentials):

<?php 
$DBconnect=mysqli_connect("localhost","user1","password","testDB");
$result = mysqli_query($DBconnect,"SELECT * FROM customers");
while($row = mysqli_fetch_array($result))
print_r($row['name']);
mysqli_close($DBconnect);
?>

Use a web browser to access https://otsglobal.org/index.php. If everything gone correctly you will see a page that displays the name value incustomers database.