Share this page:

Set up a LAMP server

A LAMP server consists of Linux, Apache, MySQL database, and PHP (or Perl or Python).  It's a very common combination of tools which can be used with many different web applictions, and is supported by most web hosting companies.  

The Apache web server receives requests and decides how to handle them.  Some requests may be handled by sending an HTML file back to the browser, others will be handled by running a PHP script.  

The HTML output from scripts is sent back to the browser.  Data used by a LAMP stack is stored in a database which can be accessed by PHP scripts.

Install the software

First, make sure your server is up to date:

sudo apt-get update
sudo apt-get upgrade

Now you need to install several packages:

sudo apt-get install apache2 php5 php5-mysql mysql-server mysql-client php-apc

This will install Apache, MySQL server, PHP and a PHP/MySQL interface library. I've included the MySQL client, but this is optional. When the MySQL server is installed, you will be prompted for a root password. For the purposes of this tutorial, I used 'mypassword' without the quotes.

Alternative PHP Cache (APC) is a module that caches PHP byte code when it executes. When the same code is executed again, it doesn't need to be parsed again which reduces disk access and CPU overheads.  It can lead to a significant improvement in the performance of PHP applications.  

Check that Apache is working by opening a browser on a PC or laptop, visit the IP address of your server.  In my case this is  You should see something like this:

Apache sample page

Next, test that PHP is installed correctly.  Open a file in nano in /var/www:

sudo nano /var/www/test.php

Note that in Apache 2.4, the web root is /var/www/html.  Enter this text, and save the file:

<?php phpinfo(); ?>

Type control-o and press return to save the file, and type control-x to exit nano. Now visit the new page in your web browser by visiting http://server IP address/test.php.  In my case this is

Secure the MySQL server

There are some MySQL server settings that can be changed to make it more secure.  MySQL is supplied with a utility that guides users through the process of changing these settings. Enter the following command in a terminal:

$ sudo mysql_secure_installation


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

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...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - 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...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

If you entered a root password when you installed MySQL server, then you don't need to change it and you can answer 'n' for the first question.  Enter 'y' for all the others.  

You now have a fully functioning LAMP server.  You can use it to develop PHP applications, or install an application like Wordpress.


Install phpMyAdmin

phpMyAdmin is a tool for managing MySQL servers using a web UI in your browser.  It makes it possible to view and edit the contents of a MySQL database.  phpMyAdmin is written in PHP, and it needs a web server and a MySQL database to be installed on a server in order to work.  If these things haven't been installed on your server already, follow this tutorial on setting up a LAMP server

Type this command to install phpMyAdmin:

$ sudo apt-get install phpmyadmin

The installer will prompt to see which web server needs to be configured.  In this case, choose Apache. 

phpMyAdmin installation, select a web server

Next, you will be prompted to see if you want the database to be automatically configured to work with phpMyAdmin.  Choose 'yes'.

phpMyAdmin installation, configure database
Now you will see a prompt for the database server's admin password.  In the article about setting up a LAMP server, I used 'mypassword':

Enter the database server's root password

Choose a password that phpMyAdmin can use to connect to the database server. I used 'appassword':

Enter a password for phpMyAdmin to use to access the database

Enter it again to confirm your password:

Confirm the phpMyAdmin password

Apache's main configuration file needs to be edited in order to include settings from phpMyAdmin's configuration file.  Open it in nano with this command:

$ nano /etc/apache2/apache2.conf

Scroll down to the end of the file and add the following line:

Include /etc/phpmyadmin/apache.conf

Restart Apache with this command:

$ sudo service apache2 restart

Now visit the phpMyAdmin home page in a browser by visiting http://server's IP address/phpmyadmin/, in my case this is  Log in with the root password for the database server, mypassword.  Now you can begin exploring phpMyAdmin: 

phpMyAdmin home page

Click on the phpmyadmin database on the left hand side of the page and look at the tables used by phpMyAdmin:

phpMyAdmin editing tables