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

A “LAMP” is a group of open source software that is installed together to enable a web server to host dynamic websites and web applications. This term is actually an acronym which represents Linux, Apache web server, MySQL data source, and PHP.

Step One — Install Apache

We can install Apache easily using Ubuntu’s program manager, apt. This is package manager allows us to install most application pain-free from a repository managed by Ubuntu.

Install Apache

Afterward, your web server(Apache) is installed. We can verify that by visiting your server’s public IP address in your web browser http://your_server_IP_address.

If you have installed Apache web server on your local machine then that can be verified by visiting or http://localhost.

After that, you will see the default Ubuntu Apache web page. which is there for information and testing reasons. If you see this page, then your web server is now correctly installed.

How To Find your Server’s Public IP Address
If you do not know what your server’s public IP address is, there are various methods to discover it. Usually, this is the address, you use to connect to your server through SSH.
you can use the iproute2 tools to get your address by typing this:

Step Two — Install MySQL

Now we have up and running Apache web server, So it is time to install MySQL. MySQL is a database management system. Run following command on terminal to install MySql:

During installation, your server will ask you to select and make sure passwords for the MySQL “root” user. This is an administrative account in MySQL. Confirm the password and continue the installation.

When the installation is finished, we need to run some extra commands to get our MySQL environment set up safely.

First, we need to tell MySQL to make its database directory structure where it will store its details. You can do this by typing on terminal:

Afterward, we want to run a simple security script that will remove some risky defaults and lock down access to our database system a little bit. Start the interactive script by running on terminal:

You will get asked to enter the security password you set for the MySQL root account. Next, it will ask you if you want to modify that security password. If you are satisfied with your present password, type “n” for “no” at the prompt.

For the rest of the questions, you should simply hit the “ENTER” key through each prompt to accept to the default values. This will remove some example users and databases, disable remote root logins, and load these new rules so that MySQL immediately aspects the changes we have created.

At this point database (MySql) installation finished.

Step Three — Install PHP

In below command we’re going to install php and also include some helper packages as well:

This should install PHP without any problems.

Optional Step:

Currently, if a user requests a directory from the server, Apache will first look for file called index.html in that directory. If we want to tell our web server to prefer PHP data files, so we’ll make Apache look for a index.php file first.

To do this, type below command on terminal to open the dir.conf file with root privileges:

It will look like this:

We need to shift the PHP index.php file above to the first place after the DirectoryIndex specification, like this:

When you are finished, save and close the file by pressing “CTRL-X”. and then you have to confirm the save by typing “Y”. After that hit “ENTER” to confirm the file save location.

After this, we need to reboot the Apache web server to be able for our changes to be identified. Restart Apache using the following command:

Install PHP Modules

To improve the performance of PHP, we can optionally install some additional modules.

To see the available choices for PHP modules & libraries, type following command on terminal:

This will show result of optional components that you can install.

If we decided that php5-cli is something that we need, we could type:

If we need to install more than one module then we can do that by listing each one, separated by a space, like this:

Now LAMP stack is installed and configured.

Step Four — Test PHP on Web Server

In order to test that We will call this script info.php. In order for Apache to find the file and serve it properly, it must be saved to a very particular directory, which is known as the “web root”.

In Ubuntu 14.04, this directory is situated at /var/www/html/. We can make the file at that place by typing:

This will open an empty file. We want to put the following text:

When you are finished, save and close the file. Now try this out, for that we have to visit this page in our web browser.

The address you want to visit will be:

On local machine visit following address: or http://localhost/info.php

The page should look something like this:

Leave a Reply

Your email address will not be published. Required fields are marked *