How to Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu



LAMP is the most common configuration used in web server and it stands for Linux, Apache, MySQL, and PHP. This tutorial describes how to install LAMP stack on to Ubuntu system. The Linux part of the stack is already done as the virtual private server is up and running Ubuntu. Here you will see the installation of Apache, PHP and MySQL.

I assume that you have already done the server setup with user having root privileges on your VPS. Refer to this tutorial for initial server set up.

Installing Apache


Apache is free software that is distributed by Apache Software Foundation and is supposed to be the most popular open source software that runs over 50% of web servers in the world.

Before getting to installation of any LAMP software, just make sure of these two things:

•             User should have root privileges

•             Package manager should be updated

sudo apt-get update
sudo apt-get install apache2

To verify, you can point your browser to the server IP address and check. If it displays a page which says “It works”, then it is correct.

You can get your IP address using the following command:

ifconfig eth0 | grep inet | awk '{ print $2 }'


Installing MySQL


Let’s start with MySQL installation process. MySQL database management system is used for organizing and retrieving data.

Open your terminal and enter the following command.

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

In case if the system displays any messages, press enter. You will be asked to set a root password while the program is installing. In case if you missed the chance, you can set it later from within the MySQL shell.

Once mysql is installed, you can proceed with the command to activate it:

sudo mysql_install_db

Next step is to run the secure install script to clean up.

sudo /usr/bin/mysql_secure_installation

While the secure install scrip is running, the system will ask for your MySQL root password. In that case, just hit enter as MySQL root password has not been set.

When system prompts for setting the root password, type ‘Y’ and enter your new MySQL root password. Below you can see a snapshot of the output screen, while the script is running.

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them.  This is intended only for testing, and to make the installation go a bit smoother.  You should remove them before moving into a production environment. 

Remove anonymous users? [Y/n] y                                             
... Success! 

Normally, root should only be allowed to connect from 'localhost'.  This ensures that someone cannot guess at the root password from the network. 

Disallow root login remotely? [Y/n] y
... Success! 

By default, MySQL comes with a database named 'test' that anyone can access.  This is also intended only for testing, and should be removed before moving into a production environment. 

Remove test database and access to it? [Y/n] y 
- Dropping test database... 
... Success! 

- Removing privileges on test database... 
... Success! 

Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y 
... Success! 

Cleaning up...

MySQL is installed.


Installing PHP


PHP is a programming language for building dynamic web pages.

For the installation of PHP, open the terminal and type in the following command:

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

The system will ask for your confirmation and PHP will be installed.

You can also add php to the directory index to server relevant php index files:

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

index.php should be added to the beginning of index files. The page should look like this:

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


PHP offers a variety of useful libraries and modules to be added on to your server. To get a list of available libraries, you can type in:

apt-cache search php5-

The output should look similar to this:

php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5
php5-gmp - GMP module for php5
php5-ldap - LDAP module for php5
php5-mysql - MySQL module for php5
php5-odbc - ODBC module for php5
php5-pgsql - PostgreSQL module for php5
php5-pspell - pspell module for php5
php5-recode - recode module for php5
php5-snmp - SNMP module for php5
php5-sqlite - SQLite module for php5
php5-tidy - tidy module for php5
php5-xmlrpc - XML-RPC module for php5
php5-xsl - XSL module for php5
php5-adodb - Extension optimising the ADOdb database abstraction library
php5-auth-pam - A PHP5 extension for PAM authentication

To install  a particular module, type in the command:

sudo apt-get install name_of_the_module

Now that the LAMP stack is completely installed on your system, you may have to verify it by creating a simple php info page.

For that, you need to create a new blank file and paste the following code.

sudo nano /var/www/info.php


After saving the file, go ahead and restart apache in order to bring in all your changes.

sudo service apache2 restart

Visit your php info page by going to your web address.

Now you are officially done with the installation of LAMP stack on  Ubuntu.

support2 has written 111 articles

Leave a Reply