How To

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

LAMP Stack is a group of open source softwares that are installed on a server to host dynamic websites and web apps.

LAMP is an acronym which represents

  • L stands for Linux Operating System.
  • A stands for Apache Web Server.
  • M stands for MySQL Database.
  • P stands for PHP programming language.

Note: For this article I am going to create a fresh DigitalOcean droplet with Ubuntu 16.04.3 LTS linux distribution image.


Before you begin installing LAMP stack on fresh server, you need to perform some basic configuration steps as part of as an early basic setup. This will increase security and usability of your server. You can learn how to do this by completing the steps from our initial server setup blog.

Step 1: Install Apache

Apache is the most widely used web server software. It is developed and maintained by Apache Software Foundation, Apache is an open source software available for free.

We can install Apache easily using Ubuntu’s package manager, apt. Ubuntu package manager allows us to install most software painlessly from a repository maintained by Ubuntu. You can learn more about apt from here.

Before installing Apache first run update and upgrade command using command line on terminal.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install apache2

Since we are using sudo command, these operations are executed with root privileges. It will ask you for you regular user’s password to verify your identity.

Once you’ve entered your password, apt will tell you which packages it plans to install and how much disk space is required. Press Y and hit Enter to continue the installation.

Set Global ServerName to Suppress Syntax Warnings

Now, we need to add a single line to /etc/apache2/apache2.conf file to suppress warning message. If you don’t set ServerName globally, you will receive the following warning message when checking you server Apache configuration for syntax errors:

$ sudo apache2ctl configtest
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

Open the main configuration file with you favourite text editor, I will use nano for this example:

$ sudo nano /etc/apache2/apache2.conf

Inside at the bottom of the file, add ServerName directive that will point to your primary domain name. If you don’t have a domain name associated with your server, you can also use your server’s public IP Address:


Save and close the file when you have added the ServerName directive.

Now check for syntax errors by typing the following command:

$ sudo apache2ctl configtest
Syntax OK

Now restart Apache to implement your changes:

sudo systemctl restart apache2

Adjusting the Firewall to Allow Web Traffic

Assuming that you have followed our Initial Server Setup blog instructions to enable UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can list application profile for Apache using the following command:

$ sudo ufw app list
Available applications:
  Apache Full
  Apache Secure

Now look at Apache Full profile, it shows that it enables the traffic to ports 80 and 443:

$ sudo ufw app info "Apache Full"
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web

Now allow incoming traffic for this profile:

$ sudo ufw allow in "Apache Full"

You can verify on the spot to check whether things go as planned by visiting your server’s public IP address in your web browser.


If you see this page, then your web server is correctly installed.

Step 2: Install MySQL

Now our web server is up and running, it’s time to install MySQL. MySQL is a an open source Relational Database Management System.

We can use apt to install mysql.

$ sudo apt-get install mysql-server

During the installation, your server will ask you to select and confirm a password for the MySQL “root” user. This is an administrative account in MySQL that has heightened privileges similar to the root account of the server itself. Make sure the password is strong and unique and don’t leave it blank.

When the installation is complete, we have to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. To run the script type in the terminal:

$ mysql_secure_installation

After the script is completed, your database setup is completed and we proceed further.

Step 3: Install PHP

PHP (recursive acronym for “PHP: Hypertext Preprocessor” is a widely used, open source general purpose scripting language. We will be using PHP to dynamically displaying the web pages by leveraging the data from MySQL database.

We again use APT to install our components. We are also going to install some helper packages as well, so that PHP code can run under Apache server and talk to MySQL database:

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

Now, we have our PHP installed on our server. We will test it in a moment.

We want to modify the way apache server files when a directory is requested. Currently, if a user requests a directory from the server, Apache first first looks for index.html. We want to tell our server to prefer PHP files, so we’ll make apache look for PHP files first.

To do this, open the dir.conf file using the command below:

$ sudo nano /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

We want to move the highlighted php index file to the first position after DirectoryIndex specification, like this:

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

Now save and close the file and restart Apache.

$ sudo systemctl restart apache2

Step 4: Testing PHP processing on your Web Server

To test our system we’ll create a very basic PHP script. We will call this test.php. In order for Apache to find and serve the file, it must be saved in a very specific directory called as “web root”.

In Ubuntu 16.04, it is located at /var/www/html/. We can create a file at this location by the following command:

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

This will open a blank file and we will put the following text in it.

echo phpinfo();

Now save and close the file when you are done.

We can now test whether our web server correctly display content generated by a PHP script. To try this out, visit this page in your web browser:


The page will be served something like this:

If you found this page, give a pat on your shoulder. You have successfully setup your first Web Server.

Thanks for reading this article. If you found this article useful be sure to clap to recommend this article.