Installing a LAMP Server on Ubuntu

April 26, 2012 by
Filed under: HowTo, linux, Ubuntu, web development 

Why Install LAMP on Ubuntu?

Why should you install LAMP on Ubuntu? When working on web development, I prefer to work within the privacy of my own development environment located on my computer. I'd rather make the majority of my mistakes where no one else can see them rather than out in the wild of the Internet. In order to have that private development environment, I install a LAMP configuration. If you're not familiar with LAMP, it stands for Linux, Apache, MySQL, php (and/or Perl). LAMP is one of the most common web hosting platforms on the Internet, so it's a great environment to build and test your website.

Follow these step by step instructions to install and configure LAMP  on Ubuntu 12.04-Precise Pangolin. This process has also been tested and works on Linux Mint 13,  14, and 15, Ubuntu 12.10-Quantal Quetzal and Ubuntu 13.04 Raring Ringtail. If you have a reasonably fast broadband connection, you should be done in less than half an hour.

Install LAMP on Ubuntu

The developers at Ubuntu have made it really easy to install and configure the packages for LAMP with a single terminal command. So open up a terminal window and let's get started.

sudo apt-get install lamp-server^

Yes, you read that right. Don't leave out the caret (^) at the end. The command won't work without it. It's magical!

Affiliate Link
Install LAMP on Ubuntu

Install LAMP on Ubuntu

The apt package manager will now show the packages to be installed and ask for confirmation. Hit <Enter> to confirm and continue with the install.

Install LAMP packages

Install LAMP packages

After some time downloading packages, you will be prompted to set a password for the root user for MySQL.

Set MySQL root password

Set MySQL root password

Enter the password you want to use for MySQL. Do not leave it blank. You'll be prompted a second time to confirm your password.

Confirm MySQL root password

Confirm MySQL root password

After confirming your password, apt will continue installing the remaining packages.

Remaining LAMP package installation

Remaining LAMP package installation

Your LAMP installation is now complete. Wow, that was easy! Now there's just a few more steps to get things configured to make it easy to work with your system.

Test Apache

Open a web browser window and enter the address http://localhost/. You should see a web page that says "It Works!"

Apache works

Apache works

Test php

Now that you've confirmed that the Apache web server works, you want to make sure that your php installation is working. To do that you need to create a file in /var/www called testing.php. You can use your favorite text editor as root, or you can use the following terminal command:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/testing.php

After that you need to restart the Apache web server.

sudo service apache2 restart

Go back to your web browser and enter the address http://localhost/testing.php/ and you should see a page displaying information about your php installation.

php information

php information

Fix Apache fully qualified domain name

You may have noticed an error message from Apache about the server's fully qualified domain name.

apache2: Could not reliably determine the server's fully qualified domain name, using for ServerName

This is not a big deal, but if it bothers you, you can fix it with the following commands.

echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

Now reload the Apache web server.

sudo service apache2 reload

You should no longer see the error message.

Configure MySQL

Since this tutorial is for setting up a local web development environment, the MySQL needs to be bound to the localhost IP address. By default this should be on your system. Just in case, you can verify it with these commands.

cat /etc/hosts | grep localhost

You should see something like localhost
::1     ip6-localhost ip6-loopback

You now  want to verify that you have the correct bind address in MySQL's my.cnf file.

cat /etc/mysql/my.cnf | grep bind-address

You should see:
bind-address =

If the bind address doesn't match the one set for localhost on your system, you'll need to edit /etc/mysql/my.cnf as root to correct it.

Install phpMyAdmin

You don't need to install phpMyAdmin, but unless you're a wizard with SQL, you'll want it to do administrative tasks on your MySQL databases.

You can install phpMyAdmin with this terminal command:

sudo apt-get install libapache2-mod-auth-mysql phpmyadmin

You'll be prompted to confirm that you want to install the packages. Hit <Enter> to continue.

Install phpMyAdmin packages

Install phpMyAdmin packages

You'll next be prompted to select a web server to configure with phpMyAdmin. This is important! Use the arrow keys on your keyboard to highlight apache2 and then used the space bar to select it. Then hit <Enter> to continue. Make sure to click the picture below to see it in full size to see what I'm describing.

Configure phpMyAdmin for Apache

Configure phpMyAdmin for Apache

The next screen will ask if you want to configure phpMyAdmin with a database called dbconfig-common. Select "Yes" and hit <Enter>.

phpMyAdmin configure dbconfig-common

phpMyAdmin configure dbconfig-common

You'll next be prompted to enter the MySQL root password you created earlier so that the new database can be created. So enter your MySQL root password and hit <Enter>.

Enter MySQL root password

Enter MySQL root password

The next prompt is to create a MySQL application password for phpMyAdmin. You can hit <Enter> if you want a random password to be created. I usually use the same password that I used for the MySQL root password. It's probably not the greatest idea in terms of security, but since this is for a closed development environment, it's probably low risk.

phpMyAdmin MySQL application password

phpMyAdmin MySQL application password

Finally, you'll be prompted to confirm your MySQL application password. Type the same password as the previous step and hit <Enter>.

Confirm MySQL application password

Confirm MySQL application password

Installing and configuring phpMyAdmin is now complete.

Testing phpMyAdmin

The last step is to make sure that phpMyAdmin is working. Open a web browser and enter the address http://localhost/phpmyadmin/. You should see a page that looks like this.

phpMyAdmin login screen

phpMyAdmin login screen

You should now be able to log in with the username root and the root password that you created earlier.

phpMyAdmin logged in

phpMyAdmin logged in

Congratulations, you are now done installing and configuring LAMP and phpMyAdmin on Ubuntu 12.04. You can now start building the local version of your website. If you are only working on one site, you can place your files in /var/www. Note that /var/www is owned by the user and group root, so you will either need to copy files over as root, or change the ownership and/or permissions of that directory so you can write your files there. As an alternative, you can do some additional Apache configuration so that you can keep your files somewhere under your home directory. You can even set up multiple sites that way.

If you've run into some problems, check out Fixing Common Problems with LAMP on Ubuntu. If things seem horribly broken and you want to start fresh, try Uninstalling LAMP from Ubuntu.


126 Responses to “Installing a LAMP Server on Ubuntu”

  1. Thanks for this post, Can anyone tell me how can i make permission for var/www folder, so that i can easily create and edit any file of www folder?

    • Linerd says:

      The folder is owned by a user/group called www-data. Adding your user ID to that group should give you permission to create and edit files there.

  2. Dinum says:


  3. Ezra says:

    The next screen will ask if you want to configure phpMyAdmin with a database called dbconfig-common. Select "Yes" and hit .

    Following instructions on another website, I selected "No". Thereafter, I discovered the phpmyadmin not available. Please what can I do?

  4. Mks says:

    I tried the above method, but when I test my "localhost/testing.php" the shown window does not appear, instead the "testing.php" file gets saved in my Downloads folder.

    Any ideas how can I fix this?

  5. Steve says:

    Great! Thanks so much. Took me all of ten minutes to set up my local sites thanks to your fine directions,,,

  6. Pavan says:

    Hi every one,
    I'm using ubuntu 12.04, and i'.m trying to save .php files on the root folder i.e., file:///var/www/
    But, when i'm going to save my file it's shows the following errors., i.e., it doesn't saves my file.
    What can I do?

    The errors are.,,

    Could not save file hi.php
    Error opening file '/var/www/hi.php': Permission denied

    File disappeared from disk
    File name: file:///var/www/hi.php

  7. Emanuele says:

    Thanks,it worked very well!

  8. Alejandro Lanza says:

    Awesomeness, thx so much!

  9. ramin says:

    thank you very much. it was a very well written and smooth tutorial. thank you again.

  10. Jo says:

    Getting this message:

    p@laptop:~$ sudo apt-get install tasksel
    E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
    E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

    How can I install tasksel?

  11. pavan says:

    Thanks for clear description,
    I completely followed the above instructions, but i can't open phpmyadmin. When i'm trying to open, it says 404 error. How could i get phpmyadmin

  12. Bhavyesh Gadhethariya says:

    This is exactly what i was searching. Really a precise method to configure LAMP (Linux, Apache, Mysql, Php) on ubuntu 12.04

  13. Kallist0 says:


    How do change the location of my Sites folder, I keep getting permission errors; I was following a tutorial on another site. I must be misunderstanding something; when Change the DocumentRoot and Directory to point to the new location I get permission errors in my web browser.

    Instead of the default site location /var/www/ I want my Sites folder to be /home/accountname/Sites/

  14. tag says:

    Thanks for such clear directions

  15. Alex says:

    This is exactly what I was looking for. I just hope it works. Thank you for covering that this is used for a local environment!

  16. elremx says:

    everything works fine until i try to test phpadmin. it says not found on this server

    any ideas?

  17. Al says:

    The best article ever, thank you so much!

  18. Andy says:

    Thanks for the very clear directions. So, if I were setting up a Linux/MySQL/PHP server for a public site, how would these directions differ? It seems like the applications and installs would be the same, but perhaps some steps or best practices would be different?

    Forgive the dumb question. I'm relatively new to Ubuntu and I'd love to hear any suggestions for setting up and tuning a production box.

  19. perlman says:

    In command line there is such error (...-desktop:~$ sudo apt-get install lamp-server^):
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    E: Unable to locate package lamp-server^
    E: Couldn't find task 'lamp-server'
    E: Couldn't find any package by regex 'lamp-server^'

    Please, help me to start installing

    • Linerd says:

      Is tasksel installed on your system? Try

      sudo tasksel install lamp-server

      You don't need the "^" in this version of the command. If tasksel is not installed, install it with

      sudo apt-get install tasksel

      then try again.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>