In this tutorial we will look at how to install Apache, MySQL, and PHP on an Ubuntu 12.04 system using the command line. This set of packages together is called a LAMP server or a LAMP stack. The LAMP stack is the most common form of software stack used on web servers today.
Apache is used to serve web pages, PHP works with Apache to enable you to create dynamic web pages, and MySQL works with PHP to enable it to communicate with a database backend. Note that any of these packages can be installed separately but for running a dynamic website, they are usually installed together.
You will need to have superuser privileges to run most if not all of the following commands, so run the following in a terminal (and then enter your root password) to become root:
You should now be logged in as root. If for some reason you cannot run the above command, you will need to prepend all of the following commands with the word "sudo" to ensure that they are run as the superuser.
At this point it is a good idea to apply any available updates to packages. You can skip this step if you want:
apt-get update apt-get upgrade
Once the packages are installed, we are ready to install Apache.
To install the Apache web server, we need to execute the following command in the terminal:
apt-get install apache2
Once Apache has finished installing, you can navigate to http://localhost/ in a browser on the same machine to see if it is working:
If you get that screen, then you have successfully installed Apache2 on Ubuntu 12. The next step is to install the MySQL server.
After installing Apache, we are ready to install the MySQL server. Execute the following command in a terminal (as root of course):
apt-get install mysql-server
You will be prompted to enter the MySQL root password during the install. Be sure to choose a secure password and don't forget it. After entering the password, the install will continue.
After MySQL has been successfully installed, it is a good idea to secure the MySQL server installation. There is a handy script which comes with MySQL which helps us to do this. Run the following command to secure the MySQL installation:
Enter your root password when prompted. You will be asked if you want to change the root password, just say no (unless you want to). Say yes to all of the other options. When finished, your MySQL installation will be a bit more secure.
The next step will be to install PHP and get it to work with MySQL.
Now we'll install PHP on our system and integrate it with MySQL. Run the following commands:
apt-get install php5 apt-get install php5-mysql
We'll also install the Suhosin package for PHP to make it a bit more secure. This step is optional but recommended. Execute the following command to install the PHP Suhosin package:
apt-get install php5-suhosin
Restart Apache for good measure to ensure that everything is loaded correctly:
That is it for installing software. Now we need to check if everything is working correctly.
To check that Apache, MySQL, and PHP have been installed correctly and are working together, execute the following from a terminal (as root):
echo "<?php phpinfo(); ?>" > /var/www/info.php
The above command creates a PHP script in the web server document root which will give us information about our LAMP installation when we execute it.
To fetch this information, open a browser and navigate to http://localhost/info.php and you should get a web page with a lot of information on it. If you scroll down and look carefully, you should find an entry for MySQL which looks like the following:
If you see the entries for MySQL then that's it you're done. You have successfully install the Linux, Apache, MySQL, PHP (LAMP) stack using Ubuntu 12.04 as your Linux platform.
You can now upload your PHP scripts to the /var/www/ directory.
We hope you found this tutorial on installing a LAMP stack on Ubuntu 12.04 useful. Happy web serving!