How to Install LAMP (Linux, Apache, MySQL, PHP) on Ubuntu (8.04 / 8.10)

This is for the newbies in the world of Ubuntu. Use the following steps to install LAMP (Linux, Apache, MySQL and PHP) in Ubuntu.

  • Open up the Terminal (Applications > Accessories > Terminal).
  • Run the following command to install Apache
    $sudo apt-get install apache2
  • To test Apache open up any web browser and then enter the following into the web address:
    http://localhost/
  • Now run the following command in the terminal to install PHP
    $sudo apt-get install php5 libapache2-mod-php5
  • In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:
    $sudo /etc/init.d/apache2 restart
  • In order to test PHP, in the terminal copy/paste the following line:
    $sudo gedit /var/www/testphp.php
  • Copy/Paste this line into the phptest file: <?php phpinfo(); ?>
  • Save and close the file.
  • http://localhost/testphp.php
  • Once again open up the amazing Terminal and then copy/paste this line:
    $sudo apt-get install mysql-server
  • This is where things may start to get tricky. Begin by typing the following into Terminal:
    $mysql -u root -p
  • Following that copy/paste this line:
    $mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');
  • We are now going to install a program called phpMyAdmin which is an easy tool to edit your databases. Copy/paste the following line into Terminal:
    $sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
  • After that is installed our next task is to get PHP to work with MySQL. To do this we will need to open a file entitled php.ini. To open it type the following:
    $sudo gedit /etc/php5/apache2/php.ini
  • Now we are going to have to uncomment the following line by taking out the semicolon (;). ;extension=mysql.so To look like this: extension=mysql.so

Now just restart Apache and you are all set!

$sudo /etc/init.d/apache2 restart

We are ready to start code in PHP using MySQL for database and Apache as HTTP server in Ubuntu!!!

About Saifur

A passionate software engineer, having several years of experiences in different area of software industry in Europe and Asia loves music, playing soccer, traveling new places, researching on new technologies such as programming languages, frameworks, software architectures and different project management practices.

, , , , , , ,

19 Responses to How to Install LAMP (Linux, Apache, MySQL, PHP) on Ubuntu (8.04 / 8.10)

  1. Jhon F. Ortiz O. December 5, 2008 at 8:55 pm #

    Hi! Thanks for this information. I found it ordered and precise. Bye!

  2. Vadim P. December 20, 2008 at 5:07 am #

    Apache was already automatically restarted by when you’re installing the packages. No need to manually restart.

  3. bluehoodie December 23, 2008 at 11:30 am #

    Thanks for the Concise how-to!
    Was very helpful in getting me started without having to sift through useless info!

  4. Saifur December 23, 2008 at 6:51 pm #

    Thanks Vadim P. I have mentioned that if it doesn’t restarted automatically in some cases. And as I wanted to make the post precise I didn’t write anything extra so that new users can easily get it.

  5. Sharon January 21, 2009 at 6:22 pm #

    Thankyou for your concise easy to follow instructions. I am a newbie and have been trying for ages to set this up and now I have … at last. Thankyou.

  6. seancomar February 8, 2009 at 8:22 pm #

    Saifur,

    Clear and straightforward instructions but I am having issues with PHP install. It is Ubuntu 8.10 on my Dell D620. Apache2 works fine and http://localhost displays index.html file. But after so many tries (and debug attemptss) the testphp.php file just refuses to work. The firefox browser displays a pop-up window asking ‘You have chosen to open phptest.php which is a PHP file from http://localhost. What should firefox do with this file? I have restarted apache2 and checked error log files in /var/log/apache2 which show the access made but no error. Thanks in advance for pointing me in right direction …

  7. Saifur February 8, 2009 at 8:27 pm #

    @seancomar, please check your file permission. I think the file that you want to see isn’t owned by the apache user. Hope that will work.

  8. seancomar February 8, 2009 at 9:07 pm #

    Saifur,

    It is owned by apache suer and I did use these commands:
    $ cd /var/
    $ sudo chown -R user www

    ‘user’ is the apache user

    Is there any change required in httpd.conf file? In windows install, I recall seeing this line:

    AddType Application/X-httpd-php .php

    Thank a bunch
    Sean

  9. Saifur February 8, 2009 at 9:15 pm #

    @ seancomar, it should work if it is owned by apache. Please make sure the file is owned by apache user and readable by your web server (apache). And please let me know exactly what error it shows.

  10. seancomar February 8, 2009 at 10:11 pm #

    Saifur,

    The only difference is I had installed apache2 using synaptec package manager in ubuntu.
    So, /var/www directory is owned by ‘user’
    The index.html and phptest.php are also owned by ‘user’.
    The error message is what I stated in my first message. The browser is not recognizing what to do with the file with .php extension. It is as if apache2 server does not knnow that PHP is available. The error is the same which you will get if you rename your index.html file to be index.xyz and then attempt to display it in the browser.

    Thanks much.
    Sean

  11. Nasim February 20, 2009 at 6:00 pm #

    It’s way easy to install LAMP on your Ubuntu by just clicking on Synaptic.

    Open Synaptic, on the menubar select Edit > Mark Packages by Task. Now on the new dialog select LAMP Server and press OK. Now click Apply. All your apache, php and mysql will be installed properly.

    Note that, phpmyadmin need to be installed separately.

  12. Saifur February 20, 2009 at 8:15 pm #

    I know it’s much easier to install using synaptic but I always prefer to configure everything by own. And of course configuring everything by own is not for the beginner but if anyone do this s/he can easily identify any type of server related issue s/he may face at the time of implementation to the real server. Because in the server you may not get the GUI access. You may have to access the server using SSH terminal.

    Thanks.

  13. John March 3, 2009 at 5:27 am #

    I have installed all according to ur instruction. But when I want to brows http://localhost/testphp.php it offer me to download. what should i do???

  14. Saifur March 3, 2009 at 5:58 am #

    @John, I think your PHP was not installed properly. And please also check the file permission.

  15. Rajesh Pandian M March 14, 2009 at 7:54 am #

    thank you for LAMP installation !!
    can u tell how to do like http://mywebsite.com
    instead of typing http://localhost/testphp.php.
    can suggest any links ?

  16. xlien April 10, 2009 at 11:04 am #

    This was fantastic! I would like to know how to get a website going? How can I test it from outside?

  17. harry April 11, 2009 at 6:31 pm #

    there is no ” extension=mysql.so ” but msql.so instead. We have to add mysql.so by hand

  18. George Smith June 4, 2010 at 6:26 am #

    Awesome, blog post!!

  19. bhupen November 19, 2010 at 6:55 pm #

    when im trying to execute these steps it showing this kind of error. how can i resolve it please suggest me.
    when installing apache
    E: Couldn’t find package apache2
    whene installing php
    E: Couldn’t find package php5
    and when m restarting apache
    $sudo /etc/init.d/apache2 restart

Leave a Reply


*