How to install LAMP Stack on Ubuntu

Stack is a collection of different software program that stack together to create a platform necessary to support application software. The various independent application software’s/ services work together to build a fully functional environment needed for an specific type of application to work. For web development one of the most popular stack is known as LAMP stack. LAMP stack is a set of open source software and services that are used for web application development. LAMP is an acronym for the following program:

  • Linux OS
  • Apache server
  • MYSQL database
  • PHP

Linux is the Operating system and the backbone for the stack. All the other software and services run on top of Linux. Linux OS is best suited for hosting websites due to its flexibility and customization feature. For this example, we will be using Ubuntu distro of Linux.

Apache HTTP server is a web server software which runs on top of Linux. It is used to process request and transmit information through the internet using HTTP protocol.

MySQL is a relational database system that helps us to create and manage the database in our system. It is necessary when we need to change the content of our website, when we need to make interactive webpage and need to get feedback or input from our users, and basically store and retrieve web contents easily.

PHP is a programming language that helps us to combine every element of the LAMP stack and allows the web application or the web page to run efficiently. It interacts exceptionally well with MySQL, and it is also dynamically typed language making it faster and easier to work with.

Prerequisites for LAMP Stack:

Some basic things that we need in order to install our LAMP Stack is:

  • Ubuntu OS (others can be used too)
  • A non-root sudo enabled user account

Step1- Updating the Repository Information:

A Linux repository is a storage location from which our system retrieves and install necessary OS updates and applications. Each repository is a collection of software hosted on a remote server and intended to be used for installing and updating software packages. As we will be needing to install various packages we will need to update the local repository information on our Ubuntu machine. For this run:

					sudo apt-get update

Step 2- Installing Apache2 server and configuring firewall

A Linux repository is a storage location from which our system retrieves and install necessary OS updates and applications. Each repository is a collection of software hosted on a remote server and intended to be used for installing and updating software packages. As we will be needing to install various packages we will need to update the local repository information on our Ubuntu machine.

For this run:

					sudo apt install apache2

Now, we will need to see the application profiles in the firewall and allow “Apache” inside the firewall. Furthermore, we must also enable the firewall and check the status of the firewall.

For all these process, run:

					sudo ufw app list
sudo ufw allow in "Apache"
sudo ufw enable
sudo ufw status

To check whether the apache2 server is correctly setup and running, type “localhost” on your browser and an Apache2 default Ubuntu webpage should appear as shown in the picture.

Step 3- Install MySQL server:

MySQL database system is required to be able to store and manage data for our domain/website. So, we need to install MySQL server, for this run:

					sudo apt install mysql-server

After completion of the MySQL server, it is always advised to run a secure installation which is already present in MySQL. For this run:

					sudo mysql_secure_installation

By running the secure installation, we will make sure that we remove any common default settings and manage several settings like lock-down access to database system, remove anonymous user, disallow remote login, remove test database present.

After entering the above code it prompts us to configure VALIDATE_PASSWORD_PLUGIN. What this does it, if enabled, MYSQL has specific criteria selected for the type of password that are accepted and whenever we keep the password it should match with the given criteria. We will type “no “ for now, but we must always be sure to have a unique and a strong password for our user.

To check if MYSQL is installed, run:

					mysql --version

Step 4 – Install PHP and the required dependencies

In this process we are going install PHP and its required dependencies. The use of the dependencies that we are going to install is provided below:

  • Libapache2-mod-php : to enable Apache to handle/access PHP files.
  • Php-mysql: This is a PHP module that allows PHP to communicate with the MYSQL based database.

Now, to install PHP and the dependencies, run:

					 sudo apt install php libapache2-mod-php php-mysql
 php -v #To check the version of the PHP we installed

Finally, all the packages are installed.

Now, we can create a virtual host for out website. In this example, I will set up the website domain name as astroprolific (you can setup your own domain name).

By default on Ubuntu Apache has one server block enabled and is configured to server webpages from the /var/www/html directory. This is very convenient for hosting a single site but if we need to host multiple sites, this can be somewhat cumbersome. So, we can create our own directory inside /var/www. For now we will keep this the same as the domain name and create the directory by running the following command:

					sudo mkdir /var/www/astroprolific #astroprolific is my folder and domain name.

After the creation of the directory. we need to assign the directory with the correct ownership. So, we assign the directory with the $USER environment variable(which references the current system user). To do this run:

					sudo chown -R $USER:$USER /var/www/astroprolific

After this, we need to configure details about our domain in the Apcahe2 server. So, we need to create a new config file inside sites-available directory in Apache2. We can simple create new configuration file by running the following command:

					sudo nano /etc/apache2/sites-available/astroprolific.conf #use_your_own_domain_name

After running the above command an empty nano editor opens, type the following in the nano window:

(Replace your_domain with your domain name you have setup in the above steps.)

					<VirtualHost *:80>
    ServerName your_domain
    ServerAlias www.your_domain
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Now, we need to enable the new virtual host for our domain, for this run:

					sudo a2ensite astroprolific#replace with your domain name


When we first check if the Apache was setup, we entered “localhost” in the browser and it navigated us to the default apache2 Ubuntu webpage. We need to disable the default webpage that appears and show the webpage that are present in our domain .So, to disable the default website that comes along with the Apache installation run:

					sudo a2dissite 000-default

We need to check that the configuration file that we have created does not have any syntax error. In order to do that, run:

					sudo apache2ctl configtest


To make sure that everything we did for the web server is taken in to effect, we need to reload the apache2 server. For this run:

					sudo systemctl reload apache2

Now, our domain should be ready. We can now start to make webpages and run them. But, in the next step we will make sure that we can host .html from our domain and also test PHP processing on our domain.

Step 5- Creating a simple webpage and checking PHP processing on our server.

First, we will make a simple html webpage and check whether our virtual host works as we require. For this,we will create a index.html file inside our domain directory and enter few lines of html codes. For this run:

					nano /var/www/astroprolific/index.html #enter your domain name inplace of astroprolific

Type the below codes into the index.html file we just created.

	<title>	Checking our webserver </title>
<h1> This is our domain's webpage. </h1>
<p> This content should be displayed when we type in our domain name or ip in the browser.</p>
<h2>This is my first time setting up a LAMP stack or is it?</h2>

After typing the given codes, save the file and go to your browser and type your_server_domain or your_ip.


After typing in the above line, the content of the index.html file must appear as shown in the picture below.

Now, we need to check if the PHP processing is working. So, we will execute a simple PHP test script to check whether our Apache2 server can handle our PHP files. For this, we will simply create a details.php and write a simple PHP script. For this run:

					nano /var/www/astroprolific/details.php #type your domain instead of astroprolific

Now, type the following in the details.php file.


Again, go to your browser and type in your domain_name or your_ip and the name of the PHP file as shown below:


You should get the following webpage in your browser.

If you see the above picture, PHP installation is working fine and you can see the details about your server from PHP. This is useful while checking for your settings and/or troubleshooting but keeping this file as it is, is not a good idea as it contains details about your PHP environment and your Ubuntu server. So, we will remove this file by simply running the following command:

					sudo rm /var/www/astroprolific/details.php

Finally, you have successfully setup your LAMP stack on Ubuntu.

Written By:

Vedant Agrawal

Founder,LD CLoud

Leave a Reply