How to install LAMP on Ubuntu (16.04+)

What is LAMP ?

Lamp stands for linux, apache, MySql and PHP. When we talk about installing LAMP, we're referring to install this group of software over a linux environment, which is typically used to host websites or applications that use PHP such as Wordpress, Drupal, or the Laravel framework. In this guide we'll be installing and configuring each of these components one by one to achieve what is called a LAMP stack. In the other hand there are prepared packages that include these three software components (apache, mysql, php) already configured  like XAMP, more typically used in local development environments, if you're more interested in installing XAMP follow this link.

Installing Apache

First things first, we need to update the available packages lists from the ubuntu software repositories and then install apache. Open the terminal with CTRL+ALT+T keys combination and type the following:

sudo apt-get update
sudo apt-get install apache2

Bear in mind that using sudo will run these commands under root / admin privileges. it will ask your password to do so.

After the install completes type the following to restart apache:

sudo /etc/init.d/apache2 restart

Checking apache is working

Type in the web browser http://localhost or http://yourIpAddress it should load the apache2 default page:

apache 2 default page


That makes it for apache, now let's continue with MySql.

Installing MySql

Back to the terminal type the following command:

sudo apt-get install mysql-server

During the installation process, it will ask you to set the password for the "root" user, which is the super user of your database. You can leave it blank if you want, which I don't recommend if you're setting up a production server.

MySql root password


Checking MySql is working

For this we'll need to install the mysql client, then use the client to access our database server we just installed. Type the following in the terminal:

sudo apt-get install mysql-client

After it's done type the following command in the terminal, we'll be logging in to the database using the root user .

mysql -uroot -p

It will ask for your password, type it and hit enter (it won't show characters in the console when you type it). If everything went fine you should see the a welcome message from mysql and the mysql prompt.



Installing PHP

Since we want our PHP installation to work with apache server, and be able to connect to mysql we're going to install php, plus some extra extensions that will allow that functionality. 

Type in the terminal:

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

The above will install php itself, apache module for php, an encription library and the php mysql module, those are the minimum extensions to have installed in order for the LAMP stack to work.

Checking PHP is working

To test php is working we are going to serve a small php script on the server. By default all the scripts are run in the web root, in ubuntu the web root is located in "/var/www/html" system path, so we are going to create a php file in that location and run it through the web browser.

Type in the terminal:

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

This will open an edit window inside the terminal (which is actually a text editor called nano), in there type the following:


Save the file by hitting CTRL+X, then Y and hit enter.

Re start apache by typing the following in the terminal:

sudo /etc/init.d/apache2 restart

Now in the web browser visit http://localhost/info.php or http://yourIpAddress it should load the php information website. It should look something like this:

php info website


Installing PHP extra extensions (optional)

There are some extensions that are very popular among several php applications, such as GD extension for image processing, or XML PHP to parse xml files. The following command will install a list of the most used and popular php extensions, so you won't run into any issues for missing one extension when trying to run one application: 

In the terminal run:

sudo apt-get install php-curl php-gd php-mbstring php-common php-xml

Then restart apache server

sudo /etc/init.d/apache2 restart

The PHP ini File

The php.ini file is where all the initial configuration for php is set, at some point you'll need to modify this file to modify some options for php, some common cases are to increase the memory a php script can take from your server, to show or not to show certain types of php errors or the maximum file size that some one can upload to your server using a php script. 

In Ubuntu, assuming we have php version 7, the file would be located at:


You'll need root access to modify the file, so you could run this line in the terminal to edit it using "gedit" a more user friendly editor:

sudo gedit /etc/php/7.0/apache2/php.ini

You should see something like this:

php ini


Also after you finish editing the file, you'll have to restart the apache server for the changes to take effect.

All set!

And that's it, you're ready to host php applications or run php scripts using an apache server hosted in an linux ubuntu server.

Alex Perea

Web and software developer, passionate by technology and design. Autodidact and pleased to share any piece of knowledge.