Installing Apache2 With PHP5 And MySQL Support On Ubuntu 12.04 LTS (LAMP)

Version 1.0
Author: Falko Timme
Follow me on Twitter
Last edited 05/02/2012

LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install an Apache2 webserver on an Ubuntu 12.04 LTS server with PHP5 support (mod_php) and MySQL support.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

In this tutorial I use the hostname server1.example.com with the IP address 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.

I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

sudo su

 

2 Installing MySQL 5

First we install MySQL 5 like this:

apt-get install mysql-server mysql-client

You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as [email protected], so we don't have to specify a MySQL root password manually later on:

New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword

 

3 Installing Apache2

Apache2 is available as an Ubuntu package, therefore we can install it like this:

apt-get install apache2

Now direct your browser to http://192.168.0.100, and you should see the Apache2 placeholder page (It works!):

Apache's default document root is /var/www on Ubuntu, and the configuration file is /etc/apache2/apache2.conf. Additional configurations are stored in subdirectories of the /etc/apache2 directory such as /etc/apache2/mods-enabled (for Apache modules), /etc/apache2/sites-enabled (for virtual hosts), and /etc/apache2/conf.d.

 

4 Installing PHP5

We can install PHP5 and the Apache PHP5 module as follows:

apt-get install php5 libapache2-mod-php5

We must restart Apache afterwards:

/etc/init.d/apache2 restart

 

5 Testing PHP5 / Getting Details About Your PHP5 Installation

The document root of the default web site is /var/www. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

vi /var/www/info.php

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.0.100/info.php):

As you see, PHP5 is working, and it's working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.

 

6 Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Now restart Apache2:

/etc/init.d/apache2 restart

Now reload http://192.168.0.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

 

7 phpMyAdmin

phpMyAdmin is a web interface through which you can manage your MySQL databases. It's a good idea to install it:

apt-get install phpmyadmin

You will see the following questions:

Web server to reconfigure automatically: <-- apache2
Configure database for phpmyadmin with dbconfig-common? <-- No

Afterwards, you can access phpMyAdmin under http://192.168.0.100/phpmyadmin/:

 

Share this page:

52 Comment(s)

Add comment

Comments

From: Anonymous at: 2012-05-31 04:22:32

Spent a day trying to get tomcat to do some simple stuff. Man was I happy to land on this website! I stopped the tomcat service, installed Apache2 (already had the L, M & P) and BAM! default web page, NO PROB!!!  Took me all of five minutes! Now, I'm in tall cotton; even the food tastes better :-))  MANY THANKS!!!

From: sigma at: 2013-02-16 22:15:18

huge! Works the same on ubuntu12.10. Thanks for this great tutorial

From: Thushara at: 2013-12-26 09:23:41

Awesome tutorial. Saved my day. :)

From: Anonymous at: 2012-06-20 23:36:06

thanks my friend  cool, please add symbol link phpmyadmin direct acces

 


From: Luiz at: 2012-06-21 23:33:11

 Your tutorial saved me days of work. 

Thanks a lot.

Luiz

From: Anonymous at: 2012-06-28 00:03:15

Great tutorial. I did this really fast. I have to add that I needed to add the line:
 
 Include /etc/phphmyadmin/apache2.conf
 
to the file /etc/apache2/apache2.conf for getting the phphmyadmin page to work.
 
 

 

 

From: at: 2012-07-04 15:37:52

Thanks for your post. I got a LAMP server in 5 minutes!!! Work like a charm!

You could also insert some performance tips after the basic install.

 

From: Anonymous at: 2012-07-05 07:50:52

Thank you for the superb guide.

Although regarding phpmyadmin, have to say that I need to do the:\

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
sudo /etc/init.d/apache2 reload

 as suggested in https://help.ubuntu.com/community/phpMyAdmin.

But I have to comment out the 

Include /etc/phpmyadmin/apache.conf

 to ensure it works (since I followed the help step-by-step).

Hope this will help others having the same problem.

 

From: at: 2012-08-19 12:27:57

Thank you for your details. I did this as follows: sudo dpkg-reconfigure -plow phpmyadmin sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf sudo /etc/init.d/apache2 reload as suggested in https://help.ubuntu.com/community/phpMyAdmin.(My Ubuntu version is 11.04)

From: Wim Anckaert at: 2012-09-13 07:40:47

tx, this worked also on ubuntu 12.04

From: Anonymous at: 2013-04-15 11:52:43

This works great for me as well!

Thank you very much

From: gaodayue at: 2012-09-08 13:25:16

thanks for your remind and the helpful link

From: jda at: 2012-11-07 22:57:16

Quote 'Although regarding phpmyadmin, have to say that I need to do etc '

Me too...

From: Anonymous at: 2014-05-31 19:04:59

Thanks alot for your help ! 

 I had similar problem and it does work after I read your comment :) ,,, Also , using nano instead of vi in step 5 is much easier ! if someone does not know how to deal with "vi" as well ;) ... Thank you all ! 

 

 

From: Allan at: 2012-07-07 20:19:40

Perfect, worked first try.

From: Hakob at: 2012-07-18 18:22:43

Thanks for this tutorial. It is really best tutorial for this kind of installations...

From: Gg at: 2012-08-01 07:55:02

Thanks for the nice and pretty tutorial!

I just wanted to mention that it might be worth considering the tasksel utility too. A little less control on things, but overall a quite immediate approach.

You just need to install the tasksel package from repos:

sudo apt-get update 

sudo atp-get install tasksel

Then just run tasksel as superuser:

sudo tasksel

What you get on screen is a whole bunch of configurations you can change your system into. Just scroll to "LAMP server", select it with space bar and press enter. All packages to be installed are automatically included for you. Along the installation you'll be asked to provide pretty much the same info such as MySQL password etc. Once done, you'll have your LAMP server (or any other config you selected) ready to go.

Hope it helps!

Gg 

From: Anonymous at: 2012-08-14 08:42:36

after the command-

apt-get install mysql-server mysql-client

i wasnt prompted for the user name or password!!!

what could be the problem

From: Anonymous at: 2012-08-23 15:57:04

Thanks, it worked first time without any problem

From: INCH-5 at: 2012-08-29 12:20:33

Great!!!, all went well, tried it on my Vmware, just that i was confronted with "username/password" option - but after a while i figured it out

Username = root

Password = "yourrootsqlpassword" one provided during Msql installation.

From: Afandi at: 2012-09-06 20:58:19

Hi, many thanks

I have successed until no #4 but when create info.php, I can save the file into var/www

it said you do not have the permission.

Please advise thanks 

 

From: Kunal at: 2012-09-11 17:13:46

Use sudo before the command and enter password when prompted.

like

sudo vi /etc/www/info.php

From: Anonymous at: 2012-10-24 06:05:07

save your file on the desktop

try this on terminal

 

su

your password

cd Desktop

mv info.php /var/www

From: Randi\y III at: 2012-09-18 05:48:36

thanks for your help....
it is a big help for me....^^

From: KBC at: 2012-09-21 13:51:21

good job buddy nice and easy to understand guide. TNX

From: uday at: 2012-10-16 16:48:45

this is really a wonderful tutorial

From: Anonymous at: 2012-12-08 12:05:35

ottimo ha funzionato subito

perfetta e chiara, complimenti

From: Ravi Kumar at: 2012-12-08 07:01:02

Thank you very much for your nice guidelines.

It worked pretty well up to  step 6 but get stucked at 7th step. During 7th step it asked two questions, in first case i allowed - "Apache2" and in 2nd question which was long with with some other information also (e.g do you have any database etc) then i choose the option as "NO".

Then after refreshing the page with replacing info.php to phpmyadmin, there was not any sign of phpmyadmin page.

What should i do now ?. Thank you again for your help.

 

Ravi Kumar

From: Ravi Kumar at: 2012-12-10 17:43:02

Hi everyone,

After 2-3 days i got the simple solution of my problem. In my case upto step 6 (localhost/php.info) everything worked very well but 7th step i.e localhost/phpmyadmin didn't show anything.

 I came to know from another tutorial that phpmyadmin is poor in making automatic link with apache2, so you have to makeup a symbolic link as:

cd /var/www

sudo ln -s /usr/share/phpmyadmin phpmyadmin

and that's it. It worked !!!

I login as "username =root" and "password=rootpassword" , please enter the correct password while u entered during ur mysql installation. It worked for me, n i hope it will definitely work for you too. 

Ravi Kumar (BIC, IMTECH)

Chandigarh

 

From: Cristian at: 2012-12-26 20:39:25

Thanks Ravi!

From: Anonymous at: 2013-01-04 22:40:40

Thanks Ravi, had the same problem.

From: Kamil at: 2013-02-09 21:20:15

Thanks dude! I had the same problem and you helped me a lot!

From: osdarodi at: 2013-03-07 21:30:55

Thank you for your comment, it helpme to install phpmyadmin.

From: Namal at: 2013-06-04 12:53:50

Thanks a lot

From: sukanya jeyabal at: 2013-12-27 18:11:05

thank you sir, i too came across the same problem, and now it works

From: Sampath at: 2014-03-06 10:02:39

Ravi thank you very much.

From: Quan Tran at: 2013-04-11 14:03:01

So is this server availble for LAN only or i can access it outside LAN network?

From: Anonymous at: 2013-04-13 18:17:11

Wow, thanks a lot! It only took about 5 minutes to set everything up (almost as fast as easyphp on Win :).
And the solution for phpmyadmin that Ravi Kumar posted worked perfectly.
So everything works as expected

From: matmosis at: 2013-06-25 09:17:17

ha funzionato tutto e subito

bella guida, professionale, complimenti

From: fermax2020 at: 2013-07-06 14:32:06

thank you very much for the  great   info

From: san pedro at: 2013-09-13 00:03:23

very nice tutorial, work flawless...thank you very much!

From: vinsr at: 2013-09-19 04:39:54

Thank you very much it helped me completely...it works without any errors..

From: Anonymous at: 2013-10-10 12:40:22

Very good. Thank you.

From: chuck eckenroed at: 2013-10-23 00:03:29

Thank-you, Thank-you!!  This saved me a ton of time (and mistakes).  :)

From: Mohamed Alioui at: 2013-11-25 06:55:41

Thank you very much, that was very easy to implement. Saved me lots of trouble.

 

From: Anonymous at: 2013-11-29 22:52:19

what a great tutorial, glad I found it. saved me days of work most likely

From: Bishal at: 2013-12-08 17:15:56

Thank you very much. Saved me a lots of time. Thanks a lot.

From: Anonymous at: 2014-01-26 03:26:45

This Is Really very good? This Is The Best Site Yet, But I Must Add, After You Install Ubuntu 12.04 or what ever version you are running, you need to check the install third part software button before you install the system, you can leave the upgrade for later or after you have installed UBUNTU 12.04, Then You Need To Go To Upgrade Manager The Check For Upgrades If There Are Some There The Upgrade The System First, It May Take A While But It Is Worth It In The Long Run, My System Run Match Better After This. Then I Followed The 7 Steps Here And It Worked Grate For Me, The Best I Have Ever Done THANKS!

From: at: 2014-03-25 07:17:30

This tutorial is for Ubuntu 12.04 and has been written in 2012. So it uses the Software Versions that were the current Versions at that date for this ubuntu Version. If you follow the sme tutorial on a current Ubuntu Version like 13.10, then you will get a current PHP Version autmatically. So it depends all on the Ubuntu Version that you choose for your Server.

From: HG at: 2014-03-24 22:41:43

How come you guys doing tutorials never tell your readers that using the repos they will be installing software that is 3 - 5 years old?  PHP 5.3 support ends soon.  Who wants to run a 5 year old version of PHP?

From: knsjay at: 2014-05-27 00:57:48

awesome ., worked and installed great

linux lite., 

From: Pierre-Antoine at: 2015-04-01 12:13:19

Thanks. Works good.