LAMP Ubuntu 16.04 for Beginners [Step by Step] Install

Once you have bought and done with the server setup, then the next task is to install the necessary applications on the Ubuntu server to make it perfect for running a website. (If you know basics, just scroll down to see install lamp ubuntu instructions).

To run a dynamic website, you need 4 things. They are Server, Web Server applications, Database Management System and web request processor.

This setup is required to run a basic website, WordPress and site entirely developed on PHP and HTML.

If you have site developed with any other web language, then you have to install them on server. Let us dive into the process of install lamp ubuntu.

Today, in this article, let us see How to Install LAMP on Ubuntu.

What is LAMP?

LAMP stands for Linux, Apache, MySQL and PHP. Here in Linux, we can refer to Ubuntu Operating System as it is one of the Debian version of Linux distribution.

Let us see the rest of the applications.

What is Apache?

Apache is the web application software which runs on an operating system and coordinates between the MySQL, PHP and any other web application programming languages installed on the server.

Apache is the first web server which became very famous among developers due to its support and ability to server the request without consuming much server capacity.

However, many other applications are being on use today.


Apache is the best one till the date and it is free.

What is MySQL?

MySQL is the Database management software which stores, retrieves and manipulates the data based on given instruction.

MySQL is used by PHP in WordPress and it is very essential to run a dynamic website.

What is PHP?

PHP is the web programming language. Unlike ASP.NET, PHP is open source and free. We can say more than 50% website on the internet are powered by PHP.

PHP is also the very first dynamic web programming language being learnt by all programmers. Those who studied other scripting languages must have crossed through the PHP learning stage.


Let us see How to install LAMP stack on Ubuntu and configure them to host and run a website on the server.

Install LAMP Ubuntu


Install Apache on Ubuntu 16.04

You can use install lamp stack ubuntu commands to proceed with installation. 

The first application that we are going to install is Apache.

Here, we are going to install Apache version 2. There are many upgraded version available.


I am choosing Apache 2 as it is stable for long time.

Apache is the default option for many programmers. If you are the fan of Nginx, then follow my other tutorial here.

We can easily install apache from Ubuntu package manager .apt. Ubuntu made it easy by maintaining repository for many software.

First Update the package index by executing the below command.

$ sudo apt-get update

Then, install apache using the below command.

$ sudo apt-get install apache2


You have to use sudo infront of all these commands. Since you are working from normal user you need to add sudo in administrator privilege requiring commands.

The prompt will ask you to enter the password to verify whether you are authorized. Enter the password.

Now, the details of packages and it’s disk space requirement will be displayed. You have to enter Y to agree and proceed with that.

Then Press enter to continue the install lamp ubuntu process.

Resolving Global name Server Error

This warning will occur when you check for the apache configuration during the syntax error checking.

if you want to see the error, check the apache configuration with the following command.

The warning comes since you did not set the name server globally. To avoid this we will add a line to the apache configuration file.

$ sudo apache2ctl configtest

you will get the following ouput.

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using Set the 'ServerName' directive globally to suppress this message
Syntax OK

Now, let us dive into resolving the issue.

Open the apache configuration file using the below command.

$ sudo nano /etc/apache2/apache2.conf

Go to the bottom of the file and add the Servername directive as mentioned below.

ServerName server_domain_or_IP

Once you are done with the above step, save and close the file, then check for the syntax error.

$ sudo apache2ctl configtest

You will get the below output.

Syntax OK

Once you are done with that process, now, it’s time to restart the apache web server.

Use the below command to restart the apache web server.

$ sudo systemctl restart apache2

Once you are done with the process, make few changes in the firewall to support the apache work.

Firewall Configuration for Apache

The UFW will allow the http and https traffic. Since, you have installed the Apache web server, it will be listed in UFW application list.

To see the ufw application list, use the below command.

$ sudo ufw app list

You will get the list of application registered with ufw.

Available applications:
  Apache Full
  Apache Secure

To look at the apache profile, use the below command.

$ sudo ufw app info "Apache Full"

You will get the below output.

Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web


You can see the traffic is allowed in the port 80 and 443.

To allow the incoming traffic, use the below command.

$ sudo ufw allow in "Apache Full"

Now, all setup for the apache web server is completed. you can check it by entering your server ip address on the server.


Now, you should see the apache web server page displayed on the browser.

it means, your apache server installation is perfect and working properly.

Install MySQL on Ubuntu 16.04

After the installation of Apache, now it’s time to install MySQL.

Already, we have given a small explanation about MySQL above. Even though, i am repeating it here. MySQL is the database management system which helps in storing and retreiving data form the database for Other applications on the server.

All of our website data will be stored on MySQL application. To install the MySQL, we will use the same procedure like apache installation.

We will use .apt package manager to install MySQL.

Use the below command and don’t update the package index as mentioned above.

$ sudo apt-get install mysql-server


You will be shown list of packages and amount of disk space required to install the software. Now, Press Y and hit enter.

Similar to Server password setup, here also, you will be asked to enter a root password for MySQL.

The root in MySQL has higher privileges. Make sure you select a more secure password.

Once, you are done with the MySQL installation, now, it’s time to secure the MySQL by executing another command.

$ mysql_secure_installation

The default MySQL will have a lot of holes such default database and accounts.

Which can be used by the attacker to gain access to your MySQL database.

When you run the command, you will asked to choose yes or no for serious of security questions.

The first one will be about ” configuring validate password plugin”.

My recommendation is not to enable this plugin.


It will only allow the validated password by measuring it’s strength. The PHP will create user and password, then store it for it’s purpose. If the application did not accept the password, then it will create errors in automation in creating user.

So, just leave it blank, for other questions, just enter Y and hit enter.

It will remove test database and disable remote root login.

Remote root login is only required only when you develop big website which requires the mysql to be installed separately to handle the large data.

So, as of now, your mysql database just need to run on your server where the site is hosted.

Install PHP Ubuntu

Here comes the another tasks.

Once you have configured your web server and mysql, now it’s time to install PHP on the server.

PHP plays major role in web server configuration. PHP executes the codes and scripts to display web content.

It connects MySQL and web server. You have install some additional packages along with the PHP.

So that PHP can connect with both Apache and MySQL to work with them to process the web content.

You can use apt-get install php command like below. 

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

If you are having plain static html site, then you can leave the apache to work on default.

You have to install php on ubuntu as mentioned above with all other commands.

If you are getting package ‘php5’ has no installation candidate, then follow the below command style based on the version you use. 

sudo apt-get install php5.6 php5.6-mcrypt

When you make a request on apache web server, it will first look for a file called index.html.

It is ok, if you have static website. But, if you have wordpress or any dynamic site on the web, then apache has to look at the PHP files index.php.

In order to do this, we have to make changes in the apache directory configuration file dir.conf.

Open the file with nano editor.

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

Inside the file, you can see following lines.

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

Here, just move the index.php near to DirectoryIndex.

Then Press Ctrl+X and Y then press enter.

Once you are done with this, restart the apache to make the changes to work.

$ sudo systemctl restart apache2

To see the apache status, use the below command.

$ sudo systemctl status apache2

You can see the output contains below lines.

apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
   Active: active (running) since Wed 2016-04-13 14:28:43 EDT; 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 13581 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)

If you want to see whether PHP installation works or not, just follow the below method to test it.

Now, let us create the script info.php and store on the web root.

here the web root is /var/www/html/. This is where you will load your website content for the web server to display.

To create the info.php file, use the nano editor.

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

A file will open. Just enter the below in the file and save.


Actually, this script tells apache web server to display the information about the php configuration on the server.

Save and close the file, then go to your web browser and enter your server IP address as mentioned below.


If you see a page displaying PHP information, then it means, you have done php installation perfectly.

After that remove that file using below command.

$ sudo rm /var/www/html/info.php

You have remove the page as it gives information to any one same as it gives you.

This will help the hacker to find the information and attack the server.


You have installed LAMP stack on your server and now, it is ready to serve any web content. If you have any difficulties in following install lamp ubuntu article, please let us know the command.

You can follow the same procedure for install php7 ubuntu and install php7.1 ubuntu.

After that, you can follow this install wordpress ubuntu article.

Subscribe to the news letter to receive notification for future tutorials.




Share on facebook
Share on twitter
Share on linkedin

Leave a Comment