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 127.0.1.1 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 127.0.0.1 on your system. Just in case, you can verify it with these commands.

cat /etc/hosts | grep localhost

You should see something like
127.0.0.1 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 = 127.0.0.1

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.

Comments

125 Responses to “Installing a LAMP Server on Ubuntu”

  1. LucidLynx says:

    Nope... Couldn't find package lamp-server. you fail.
    ~Lucid Lynx

    • Linerd says:

      Read the tutorial again. The command is NOT

      sudo apt-get install lamp-server

      The correct command is

      sudo apt-get install lamp-server^

      Sorry, you fail...

  2. Greate article. thank you so much. before i installed XAMPP but i just remove it and go step by step on this post. thank man again

    • For me it worked up to this command:
      $ sudo apt-get install libapache2-mod-auth-mysql phpmyadmin

      [outuput supressed]

      Package libapache2-mod-auth-mysql is not available, but is referred to by other package
      This might mean that package is missing, is outdated or is available only from another source

      E: Package 'libapache2-mod-auth-mysql' does not have a candidate for installation

      (Since my installation is in Portuguese, I had to translate the above text back to English. This means
      the wording might not be the same one you would see under an original English installation. Nevertheless,
      the meaning is similar enough).

      In a nutshell, I cannot finish the configuration because I can't find the fingered package.

      Any hints, please?

      - fernando

      • Linerd says:

        Did you try running

        sudo apt-get update

        Then try running the command to install phpmyadmin.

        Are you using Ubuntu 12.04 or another version?

  3. abas says:

    Hi

    followed your easy setup but a few questions.

    1.
    Is there an upgrade path? Currently php version running is 5.3.x?

    2.
    Is there a way to remove/delete this task, lamp-server?

    Thanks.

    • Linerd says:

      The php version installed should be the latest one that's available in the repositories. As that gets updated, you should get the updates with your regular system updates.

      I have written a tutorial to remove LAMP. Make sure to do the dry run first to make sure that no packages you need will be removed. I've had some comments that certain developer packages get removed. Also, if you're running Kubuntu you'll have to reinstall the kubuntu-desktop package.

      http://tuxtweaks.com/2010/10/remove-lamp-in-ubuntu/

  4. really delighted! says:

    Fantastic! Awesome! over the past decade ... off and on I tinker with various distros.. lots of them.. for me it has been really big headache installing LAMP on linux -- following these steps is like surfing the waves under sunny summer evening.. already finished 6 installations with happy smiles...

  5. keya says:

    Phew! Thanks! I kept getting stuck at phpmyadmin because right at that point we were getting a flash flood warning, so I wasn't really concentrating and kept getting the black and white screen without login info.

    I had to reset the mysql password,

    https://help.ubuntu.com/community/MysqlPasswordReset

    uninstalled phpmyadmin, then reinstalled it, and REALLY READ the instructions on selecting the Apache2 server, and now everything seems to be working. Thank you for your fabulous instructions. I've spent a week with command line stuff, and it's really quite fun.

  6. Dipak Chakraborty says:

    Most useful article. Thank You very much.

  7. Bill says:

    Nice tutorial, I've done this the hard way many times before. I have a couple of suggestions:

    1.) Don't use your root password for phpmyadmin for security reasons, have it generate the random one. You don't need to know it, it's used by the program.

    2.) After installing phpmyadmin, I have a habit of editing the configuration file in /etc/phpmyadmin/apache.conf and changing the /phpmyadmin alias to something less common like /webdata6, that helps to keep bad guys from discovering that phpmyadmin is in use just in case an exploit becomes available.

    3.) In general, restarting apache shouldn't be necessary when creating and testing php. If it doesn't work, then restart.

    Great tutorial, thanks.

    • Linerd says:

      Bill, thanks for your tips. I've never been too concerned about security since I've only used this LAMP setup for development on my local machine, but I also know that some people have used this for their VPS setup, so these are definitely some good security tips. As for the Apache restart, it seems like every other version needs it so I keep it in the tutorial to keep it foolproof.

  8. Paul Aphivantrakul says:

    This walkthrough is awesome. Thank you so much.

  9. snoozer says:

    I echo comments by Rich..... OUTSTANDING LAMPP GUIDE. Follow carefully and click on the images provided for clarification.

    The only thing I would like to see in this tutorial is the command to change ownership of the web root folder.

    I followed this tutorial and it worked a treat in Linux Mint 15

  10. Marsup says:

    I placed the following file (test.php) in /var/www after installing LAMP through the procedures given above.
    the http://localhost is working but why the http://localhost/test.php is not showing any output. It is just blank, no error. Am I missing something here ?

    PHP Testing

    <?php
    echo “If this works, we really did it!”;
    ?>

    • Linerd says:

      Did you restart Apache?

      sudo service apache2 restart
      • Marsup says:

        Yes I did. The installation is ok and the samples given in the installation procedure is working. But why a simple
        different example is not working? When one line php program like this is working, why
        not the one with echo? I am really puzzled, if someone can help me?.. I use Linux Mint 14. Thanks.

        Marsup

        • Linerd says:

          I'm far from being a php expert. Did you try your program without the italics tags? If the tests in the tutorial worked, then maybe there is a syntax error in your program. Like I said, I'm no php expert. It's just a shot in the dark.

          I understand that there are some significant differences between php4 and php5. Maybe the version installed doesn't support the formatting tags?

      • Marsup says:

        When I restart Apache with the sudo service apache2 restart command no error and no display
        to my above simple .php file. Now, when I stopped sudo service apache2 stop, it definitely giving error like

        Unable to connect Firefox can't establish a connection to the server at site1.
        (Same behavious with google chrome: could not connect to ..)

        What is missing in the code above?

        -Masup

        • Linerd says:

          Apache2 is your web server, so when you stop it, of course it's no longer serving web pages. Just start it up again with

          sudo service apache2 start
      • Marsup says:

        Sorry, I have intervene again for the third time today, as I was deliberate to get it. First thanks to Linerd for
        giving the suggestion to restart apache2. But other thing is that i copied and pasted the " character, from a
        .pdf text book. That was the culprit, and when I did on my key board, all is fine.

        Thanks again. I come back again as I move forward. very kind of you all.

        -Marsup

  11. Moi says:

    Flawless...Thanks a lot

  12. jl says:

    I have a problem

  13. brian says:

    Perfect tutorial. You are best.

  14. Issa says:

    Thank's a lot!!!
    great job........
    It's very helpfull.

  15. Den says:

    Many thanks! You are the best!

  16. Anne says:

    This worked perfectly for me :) Thanks so much.

  17. Thomas says:

    Not sure if my case was an isolated case, but I needed to manually add the line "Include /etc/phpmyadmin/apache.conf" in the /etc/apache2/apache2.conf (as per https://help.ubuntu.com/community/ApacheMySQLPHP) in order to get the myPhpAdmin running

  18. Ed says:

    Very nice! Thank you. Works great!

  19. Rich says:

    Outstanding, simple, easy to follow instructions that just works. Thanks! It looks like a number of people have had the same problem I did with the phpMyAdmin installation. I thought apache 2 was selected because it had a red mark, but this just meant the cursor was positioned there to allow it to be selected. I think this could be avoided with a small addition to the instructions. Just say that if an asterisk ("*") does not appear within the apache2 selection, it has not been properly selected.

    • kc says:

      This happened to me - to fix this I had to purge the install:
      sudo apt-get purge libapache2-mod-auth-mysql phpmyadmin

  20. Amit ghosh says:

    i have install lamp but how can i use php code.i insert php code in html but its not showing,what can i do,please tell me

    • Brandon says:

      Create a PHP file instead of HTML. You can put HTML in it and the PHP extension instructs the server to process the PHP code in your file. Hope that helps!

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>