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

The acronym LAMP refers to first letters of the four components of a solution stack, composed entirely of free and open-source software, suitable for building high-availability heavy-duty dynamic web sites, and capable of serving tens of thousands of requests simultaneously. The meaning of LAMP acronym depends on the software components included in the stack, but most often it is Linux, Apache, MySQL and PHP.

We are assuming that we already have the Linux operating system installed, which in this case is our Ubuntu server. The first thing to start with is Apache. The Apache HTTP Server, commonly referred to as Apache is a web server application notable for playing a key role in the initial growth of the World Wide Web. It is one of world’s most popular and widely used web servers nowadays. First off, you can start by installing Apache:

# sudo apt-get update
# sudo apt-get install apache2

That is all. That will install apache with web root folder under /var/www and start it. We can verify if it works by entering our server’s IP address or domain name into our browser. If it works, we should get a page that says “It Works!”.

The next step is to install MySQL. MySQL is very popular, widely used open source relation database management system. To install MySQL, we execute the following command:

It will install the MySQL server and the required libraries in order to establish communication with PHP. During the installation, we will get prompt to enter new MySQL root password. We need to make sure that we use secure password. Once the installation is finished, we need to install MySQL system tables:

# sudo mysql_install_db

The next step is to make sure MySQL installation is secure. This can be done manually by editing the default configuration file. If you feel that you have enough knowledge you can skip the next step and set the configuration manually, otherwise execute the following command:

# sudo /usr/bin/mysql_secure_installation

We will be prompt for the MySQL root password. During the process, we will be asked a lot of questions. Basically, every “yes” answer, means increased security, so if we want to make our installation more secure, we should answer “yes” to all questions. After that, the new configuration will be applied, MySQL server process will be restarted and we it will be ready to use and listening for incoming connection on port 3306.

The last step is to install PHP. PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. To install it, execute:

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

That is all, answer confirmatively to the prompts and PHP installation will be executed automatically. After we have PHP installed, it is good practice to add index.php to the directory index file directive:

It is very likely that we will need to use some of the available php modules and libraries. We do not have to install all of them, but we can check which are available by executing:

# apt-cache search php5-

This will list all possible modules and libraries. For example, in order to install the APC module we need to run:

# sudo apt-get install php5-apc

This concludes the LAMP installation. Now we can create test file and put it in our web root directory. We should create file with the following content:

<?php
phpinfo();
?>

If we point our web browser to our web server and that file, we should get basic report of our PHP installation and about modules and libraries included. Now we have fully functional LAMP server where we can host a lot of websites and databases. We have become one of the millions members of the LAMP family.