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!!!
18 Comments
Vadim P. on December 20th, 2008
Apache was already automatically restarted by when you’re installing the packages. No need to manually restart.
bluehoodie on December 23rd, 2008
Thanks for the Concise how-to!
Was very helpful in getting me started without having to sift through useless info!
Saifur on December 23rd, 2008
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.
Sharon on January 21st, 2009
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.
seancomar on February 8th, 2009
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 …
Saifur on February 8th, 2009
@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.
seancomar on February 8th, 2009
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
Saifur on February 8th, 2009
@ 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.
seancomar on February 8th, 2009
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
Nasim on February 20th, 2009
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.
Saifur on February 20th, 2009
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.
John on March 3rd, 2009
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???
Saifur on March 3rd, 2009
@John, I think your PHP was not installed properly. And please also check the file permission.
Rajesh Pandian M on March 14th, 2009
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 ?
xlien on April 10th, 2009
This was fantastic! I would like to know how to get a website going? How can I test it from outside?
harry on April 11th, 2009
there is no ” extension=mysql.so ” but msql.so instead. We have to add mysql.so by hand
George Smith on June 4th, 2010
Awesome, blog post!!

I am a simple man working in IT field. I love Music, playing soccer, traveling new places. By profession I am a software engineer and enjoy my job. Currently I am doing my M Sc in Software Engineering in Blekinge Institute of Technology, Sweden. I love researching on new technologies such as programming languages, architectures, tools and techniques.
Hi! Thanks for this information. I found it ordered and precise. Bye!