How to Install LAMP on Ubuntu 16.04 in 3 Steps

How to Install LAMP on Ubuntu 16.04 in 3 Steps

This tutorial teaches how to install LAMP on Ubuntu 16.04 VPS. LAMP is an acronym for Linux, Apache, MySQL, PHP. It is a popular stack for creating and deploying dynamic web applications.

In this stack, Linux serves as the operating system for the web application. MySQL is used as the database. Apache is used as the web server. PHP is used to process dynamic content.

In some other variants of this stack, Perl is used instead of PHP or Python. However, for this tutorial, we are going to install PHP, as it is the most popular choice for this stack.

What You’ll Need

Before you begin with Ubuntu LAMP tutorial, you will need the following:

How LAMP Works

Whenever a web page request arrives at a server it is passed on to an application called web server, in our case Apache. Apache looks for the file being requested in the URL of the request and passes this information to the PHP interpreter. It executes the logic written in that file, pulls data from the MySQL database if needed and generates a web page. Apache, our web server sends this generated web page to the client. This whole process is executed with some variations every single time when you request a web page from a LAMP server.

Step 1 – Installing Apache Web Server

Before beginning the installation, update your system and make sure you have latest packages.

sudo apt-get update
sudo apt-get upgrade

Now install Apache2 with the following command

sudo apt-get install apache2

Checking Your Installation

To check the installation, open your browser on your local machine and enter the following address in address bar.


For example, if your VPS IP address is your address should be:

You should see a page that looks like this:

Installing LAMP on Ubuntu - Apache 2 Default Page

Important! If you don’t know your VPS’s IP address, the quickest way to find it is by running following the command. This command prints the public IP address of your VPS.

 dig -4 TXT +short

Troubleshooting Your Installation

If you did not see the above image don’t worry, you might have enabled the firewall. You have to enable Apache to serve web requests on port 80 and port 443 in your firewall. Install UFW.

sudo apt-get install ufw

Then allow HTTP and HTTPS traffic through the firewall.

sudo ufw allow http
sudo ufw allow https

This command enables HTTP and HTTPS traffic through the firewall. UFW is command line application called Uncomplicated Firewall. It is used to manage and make rules for Linux firewall. Now enter your VPS Ip address in your browser to check the installation. You can check the status of Apache server with the following command.

sudo systemctl status apache2

Alternatively, you can also use the Hostinger VPS firewall. Select your VPS and navigate to the Firewall section:

The Firewall button on hPanel VPS dashboard

From there, create a new firewall configuration, give it a name, and set any rules you prefer:

The process of creating a new firewall rules on hPanel

Step 2 – Installing MySQL

MySQL is the database for your application. To install MySQL, enter the following command.

sudo apt-get install mysql-server

During installation, it will prompt you for the password of the root user. Make sure to use a strong password. Don’t leave it blank.

Installing LAMP on Ubuntu - MySQL Password

The root user is the highest privileged user of MySQL. Using root user you can create other users for the databases. It is a good practice to create a separate user/role for the database of a new web application. You can check the status of MySQL service with the following command.

sudo systemctl status mysql

Example output:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-07-11 09:39:40 EDT; 1min 39s ago
 Main PID: 9579 (mysqld)
   CGroup: /system.slice/mysql.service
           └─9579 /usr/sbin/mysqld

Jul 11 09:39:39 abandoned-plate systemd[1]: Starting MySQL Community Server...
Jul 11 09:39:40 abandoned-plate systemd[1]: Started MySQL Community Server.

Suggested Reading

Check out our tutorial to learn other methods to check and list installed packages on Ubuntu.

Step 3 – Installing PHP

PHP executes your application. Install PHP and extra modules with the following command

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-cgi php-curl php-json

This command will install the latest stable version of PHP and some extra modules that are required for web application to work.

Step 3.1 – Checking your PHP Installation

Now that you have installed PHP, we will check if it is working properly by creating a test file and opening it in the browser. First, install the nano text editor.

sudo apt-get install nano

Nano is a command line text editor and is easier to get started with for beginners. Click here to learn how to use the nano text editor. Now, enter the following command.

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

This command will open nano editor with a blank test.php file for editing. The directory /var/www/html where we are creating our test PHP file is known as the webroot. This is where Apache looks for the file requested in the website URL by default if it has not been configured to look somewhere else. Check out Apache Ubuntu documentation page for information about its configuration. Also, you need root privileges to be able to write to this directory. We have used sudo before our command. Now enter the following text in opened editor:


After entering this text press Ctrl + X (or CMD + X if you are on Mac), and then Y, and then hit ENTER. This will save the file and exit the editor. Now open following web address in your browser


You should see a page that looks like this:

LAMP on Ubuntu Installed

The phpinfo() function we called inside our test.php script displays information about the PHP installation and its configuration. Now remove this test file by entering following command:

sudo rm /var/www/html/test.php

Important! It is very important to remove this test file after checking installation because it may help an attacker gain critical information about the server configuration.


You have learned how to install LAMP on Ubuntu. After installation, you can copy your PHP files to the server and deploy your web application. You can also install phpMyAdmin to manage your databases on a web interface. Be sure to check our other VPS tutorials and if you have any problems, comments, ideas do let us know in the comments sections. Happy coding!

The author

Prithviraj S.

Prithviraj is a cybersecurity expert that loves a challenge. When he's not managing servers and fixing security flaws, he writes about it on various blogs.