Installing PHP5 and Apache on Ubuntu
If you are doing any kind of PHP development, you’ll almost always be using Apache along with it. Here’s the quick and easy way to get PHP up and running on your Ubuntu box.
First, you’ll want to add the extra repositories to Ubuntu’s sources.list file.
From a command shell, you will run the following commands:
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
sudo /etc/init.d/apache2 restart
Note that if apache is already installed you can omit the first line. Your web files will now be found in /var/www/

Daily Email Updates
You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


you don’t have to do the 3rd line there, it will automatically install that if you have apache2 package installed.
Thanks so much for this article!!! It works perfectly!
Worked perfectly on my machine. Although I didn’t add anything to me sources.list file because I working mostly with Feisty, and yes, the third line didn’t do anything as warren mentioned.
I tried it, and it doesnt parse php files.
Instead of that it offers me to download it as a .php file which is not parsed.
kubiak2;
In that case you have to enable the libapache2-mod-php5.
just run:
user@host:/# sudo a2enmod php5
user@host:/# /etc/init.d/apache2 force-reload
And the php files are parsed.
I had this all working fine then Ubuntu Update Manager did it’s thing, now I can’t parse php files. I have re-run all of the suggestions from above with no luck. The symptoms are as described by kubiak2 above. Anyone have a similar love of automatic updates !!
I had the problem with Firefox asking to save/open the .php file. BlackF1re’s suggestion for a force-reload did the trick for me (although I had to run that with sudo).
Installing php and cgi support for apache2 in Ubuntu
you need to have universe source list in your sources.list file
sudo apt-get install libapache2-mod-php4 php4-cli php4-common php4-cgi
Next we edit /etc/apache2/apache2.conf file and check the index files are correct
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml
If you want to enable some Apache modules (SSL, rewrite, suexec, and include)
sudo a2enmod ssl
sudo a2enmod rewrite
sudo a2enmod suexec
sudo a2enmod include
Restart Apache to make sure all is well.
sudo /etc/init.d/apache2 restart
After I installed php5 and restarted my ubuntu machine, the php.test file I made didnt work. It just made it like a file download in firefox. So I did apache2 restart and now it works!
sudo /etc/init.d/apache2 restart
just so you know, if you run plesk, that will totally remove it.
Thats worked wayyy to easy, but i still need help configuring nola pro, give me a call warren or benj!
Thanks for the help…works great! But I also need MySQL server, which package do you install for that?
to install mysql server
apt-get install mysql-server mysql-client
If you have the same problem as I did, “couldn’t find package apache2″ use:
apt-get update
Hello!
I’m install the Ubuntu 8.04 server on the server system! How install php5 width soap disable?
Please help me!
Thanks
Hi everyone,
After following all steps mentioned above, I still couldn’t parse php pages. Then after clearing the browser history in Firefox, it worked.
Hi,
Im new to KUbuntu, the procedure i followed above was great and works fine.
It displayed the “It works!” when i browse localhost.
my problem is how can I deploy my own php pages.
thanks
Hi etomakque,
Try to learn how to create virtual hosts under ubuntu using apache webserver and you will put your php pages in a directory & include it in the virtual host settings to run it, another thing that you need to identify the required php modules to run your php scripts.
etomakque, if what Mohamed Zaian seggested doesnt work, it means that the php module isnt running. So what you do is go into the terminal and type.
sudo nautilus cd /etc/apache2
this’ll pop up a file browser with root privilages. In there you should see various folders and files. there are two folders we are interested here; mods-available and mods-enabled
Go into mods-available, scroll down to php5.conf and php5.load
select both and right click, and click copy
go back to the previous directory and go into mods-enabled. and then paste the copied files in there
Close the file browser and go into the terminal (Or alternatively, open another terminal)
after that type
sudo /etc/init.d/apache2 restart
and that should fix it for you. Or atleast it did for me.
I’m still not able to parse PHP files when I attempt to open them in a browser, I get the option to download them instead. I’ve tried all of the above steps and nothing works.
Can anyone help, this is really quite frustrating.
i have the same problem as Chad. i am new to Ubuntu. i double click on index.hmtl in var/www and it opens in the browser. if i type in the address for a php file= http:/localhost/processorder.php it will open in the browser. but that is the only way it will. if i call processorder.php from order.html it ask the redundant question “of copy or open”. two weeks of this. oh yes cannot find redirectmatch in apache2.conf file.
Hello everybody
and nothing worked
(lack of experience).
Im a newbie to Ubuntu and wanted to install php5 and apache2 + mysql
I think I installed alot of things there
So can any body help me to show to install all of these packges and make them work perfectly from the scratch .
Thank you all
Hello Jim/Sizar,
I finally got my configuration to work. I’m going to give you the whole entire story, just for reference.
First, I followed the chapter in a Dummies book for installing LAMP – doing so in pieces one at a time. This is when I first encountered the PHP download error, other than that, everything worked just fine. I couldn’t find good documents, so I decided to completely purge my system and try again.
I then installed LAMP by using TASKSEL in the terminal, which installs everything at once. Unfortunately, no Apache config files were installed and after doing many Google searches I could not find a source as to where to get them.
So, I purged again.
I then went back to installing everything one piece at a time (starting with Apache, then PHP5, then MySQL). I got Apache working first, then installed PHP5. After doing some additional research, I found a manual instructing me to run the following command:
sudo a2enmod php5
The above command may be enough for it to work, but in my case it returned the following (or something very similar, i’m going on memory here):
$ package does not exist!
So, I then removed the following library:
sudo apt-get remove libapache2-mod-php5
Then, I re-installed it:
sudo apt-get install libapache2-mod-php5
Finally, I enabled the mod once again:
sudo a2enmod php5
Then re-stared Apache:
sudo /etc/init.d/apache2 restart
This cleared it up for me, though still very frustrating. Try it out, let us know what happened.
Thanks Chad! I followed your steps exactly. It worked perfectly.
I had the same problem, but as Jod earlier on I cleared all the private data from firefox and only then it started working.. of course initially I followed all the suggested steps.
all is well here , but how to set privs to create files in var/www ?
n/m solved that …….. but still getting it to ask me to download and not run the php file
Hey joh you totally save my ass, I was dealing w/this problem for maybe 3 weeks now, i reinstall apache and php several times :S
And it was the firefox cache! i cleared it and now it works perfect!
thanks to all the people that gave a little time to share their problems and solutions.
BlackF1re’s comment did the trick for me too…
Thanks a ton dude !!!!!!!!!
Grrr.
Hey JOH, thanks for the tip about clearing your browser history in firefox.
I spent about 20 minutes banging my head against apache. Everything looked like it was configured right and php seemed to be installed right but it still was asking me if I wanted to download PHP pages.
I was about to completely rip apache and php out of the system and compile from scratch when I noticed your comment.
Once I cleared my browser history and cache it worked fine.
You rock! I had everything done but the restart of apache!
Heres my problem:
I get the “it worked message”. So everything install semi correctly at least. However, when I try to save files in the /var/www/ folder, I cant. It just wont let me. I think its a permission thing, cause I can’t edit the file already in there. Any ideas?
I’m such a noob with ubuntu I’m having the same problem as ryan. What i do? I can’t edit the index.html It says i don’t have permission. Which is just great to have a web site saying “IT WORKS” When it doesn’t work.lol Any help will be appreciated.
Hey Jae-
I figured out how to fix it (granted bit rough but it works)
Terminal:
sudo chown USERNAME /var/www/
it will than ask you for your password, which wont appear as you type. That fixed it for me.
Thank you. Ryan!
My browser still tries to open the php files
I tried restarting/re-installing/clearing firefox
any other idea?
Stupid CACHE in Firefox! ARGHHH!!!
Nabeel, try opening it in IE.
I have just installed Apache and PHP, copied a web site into /var/www and everything works fine when I go to http://localhost. BUT I asked a friend to try to go to my static IP address and he could not see it. I tried turning off the firewall but it still did not work. Am I missing something?
how to find php in ubuntu ?
i have installed php on my ubuntu
How do I remove SOAP Extension from PHP5 Ubuntu 9-4 Server?
Thanks BlackFire, I was having the same problem and the a2enmod command worked flawlessly – my blog is back up.
Gotta love the Ubuntu user community
I tried to copy the PH5 load and conf files. But they are both empty. 0 bytes?????
what to do???
thanks
Nicsmr
Weird question.
For some reason, I have two instances of Apache:
/etc/apache2
/usr/local/apache2
Only the former got php.conf, php.load when I installed PHP5 and ran a2enmod php5. Also, the former is the version referenced in /etc/init.d/
1) Why do I have two copies of apache ?! (yes, I come from a Windows shop and am noobish)
2) How can I enable the latter to use PHP – as it is the server root, httpd.conf, htdocs and perms that I’ve been using.
Thanks!
Hi, I’m new to Linux, I installed the packages successfully, localhost redirecting to /var/www/ and there is a default virtual folder named apache2-default available. I want to create a another new virtual folder in the directory. I couldn’t create any folder or file inside the /var/www folder. Can anyone help me to create a virtual folder and to create files with the same.
Note: I am using Xp as a Host Machine and Ubuntu as Virtual Machine with Sun xVM Virtual Box.
Thanks in advance.
-sakthi
thx dude
Is there an answer someplace as how to Unlock the /etc/apache2 folders. I really need to edit that stuff.
Thanks bro..your article work perfectly!
your rock!
Than You so much. i always hought this would be hard
Thanks a lot for the commands. I was trying to install the packages from the KDE Software Management but it is really annoying
and thanks for mysql install as well.
Happy Linuxing
I am new to Linux as well and was having the same issues. However, my friend hosted a site for me on his *nix machine for many years so I was able to learn a few commands. I first issued a chown command to give my account access, sudo-ing all the copy commands won’t do.
Now my account has full access to it. The I clicked the php script and chose properties -> Permissions -> Allow executing file as program.
Worked like a charm.
Yeah… clearing firefox’s cache makes all the difference