• ARTICLES
SEARCH

How-To Geek

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/

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/11/06

Comments (87)

  1. warren

    you don’t have to do the 3rd line there, it will automatically install that if you have apache2 package installed.

  2. Benji

    Thanks so much for this article!!! It works perfectly!

  3. Oscar

    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.

  4. kubiak2

    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.

  5. BlackF1re

    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.

  6. TheDane

    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 !!

  7. bvo

    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).

  8. Mohamed Zaian

    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

  9. Boris

    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

  10. yannick

    just so you know, if you run plesk, that will totally remove it.

  11. Scott

    Thats worked wayyy to easy, but i still need help configuring nola pro, give me a call warren or benj!

  12. Blake

    Thanks for the help…works great! But I also need MySQL server, which package do you install for that?

  13. Mohamed Zaian

    to install mysql server
    apt-get install mysql-server mysql-client

  14. Dustin

    If you have the same problem as I did, “couldn’t find package apache2″ use:
    apt-get update

  15. Laszlo Toth

    Hello!

    I’m install the Ubuntu 8.04 server on the server system! How install php5 width soap disable?

    Please help me!

    Thanks

  16. joh

    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.

  17. etomakque

    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

  18. Mohamed Zaian

    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.

  19. Sora Hjort

    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.

  20. Chad

    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.

  21. Jim Lang

    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.

  22. Sizar

    Hello everybody
    Im a newbie to Ubuntu and wanted to install php5 and apache2 + mysql
    I think I installed alot of things there :) and nothing worked :( (lack of experience).
    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

  23. Chad

    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.

  24. Dave

    Thanks Chad! I followed your steps exactly. It worked perfectly.

  25. Ioan Paul Pirau

    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.

  26. guyz

    all is well here , but how to set privs to create files in var/www ?

  27. guyz

    n/m solved that …….. but still getting it to ask me to download and not run the php file

  28. Ser

    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.

  29. Rahul

    BlackF1re’s comment did the trick for me too…

    Thanks a ton dude !!!!!!!!!

  30. Jon

    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.

    :-)

  31. Michael

    You rock! I had everything done but the restart of apache! :-D

  32. Ryan

    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?

  33. jae

    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.

  34. Ryan

    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.

  35. jae

    Thank you. Ryan!

  36. Nabeel

    My browser still tries to open the php files :(
    I tried restarting/re-installing/clearing firefox
    any other idea?

  37. tonyrocks

    Stupid CACHE in Firefox! ARGHHH!!!

    Nabeel, try opening it in IE.

  38. Nookawarra

    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?

  39. deepti

    how to find php in ubuntu ?
    i have installed php on my ubuntu

  40. Skid

    How do I remove SOAP Extension from PHP5 Ubuntu 9-4 Server?

  41. Feoh

    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 :)

  42. nicsmr

    I tried to copy the PH5 load and conf files. But they are both empty. 0 bytes?????

    what to do???

    thanks
    Nicsmr

  43. Jon

    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!

  44. Sakthi

    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

  45. chris

    thx dude :-)

  46. dave

    Is there an answer someplace as how to Unlock the /etc/apache2 folders. I really need to edit that stuff.

  47. waxs

    Thanks bro..your article work perfectly!
    your rock!

  48. Austin

    Than You so much. i always hought this would be hard

  49. Wesam Alalem

    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 ;)

  50. Ryan

    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.

  51. Jose Trigueros

    Yeah… clearing firefox’s cache makes all the difference :D

  52. grv

    i’m learnig linux n i installed apache 2web server n what are the commands to stop and run my server of my own ,i need to know that how should i run my basic.html on internet which is located i /var/www n i want to display my name my rollno. all stuff i want and how it could be run on various ports like 80 6060

  53. Ted

    I agree with Jose. I had the same problems with the php pages not being displayed but given option to download as a file. Clearing the cache solved that problem.

  54. Olle

    Had the same problem with download popup for php files, but joh’s suggestion to clear firefox’s history solved the issue.

  55. Sage

    Ser was right. Try clearing Firefox’s cache.
    (That worked for me!)

  56. Joseph

    Okay,stupid question but here goes.
    This is the first article I have found that helped me install a local host.
    I am a brand new linux user and am lost.I still do all my design work in Windows because of comfort.
    I am trying to convert to full out Ubuntu user but am lost.
    It looks like Apache ran and installed and in my browser I get “It Worked!”
    So its there,but how do I control it?
    Where is my control panel to start Mysql and PHP so I can get to the control page?
    I can’t find it under system or applications any where.
    I am a newbie and don’t really get the shell thing at all so please be kind,I am learning.
    Thanks

  57. qm

    Thx it works great.

  58. Krishna

    I am trying to install apache2/php5 and run 2 different sites from the same debian server.

    I installed php5, apache2 and got php working (phpinfo()). Than, I setup virtual hosts and got those working. however, php won’t execute from within a virtual host…but will execute from the server directly…

    ie:

    http://173.203.103.24/phpversion.php works but
    http://new.labautomate.net/phpversion.php makes the browser try to download the php file…

    I tried clearing firefox’s cache and even tried chrome…nothing seems to work. Please help.

    Krishna

  59. kamrul

    worked fine for me in ubuntu 9.10.

  60. DragonD

    Thnx ………..

  61. FSP

    In my case i had server error
    >You don’t have permission to access on this server.
    so after i added a permission to read/write to the /var/www/ directory the problem was solved
    sudo chmod -R 755 /var/www/

  62. Kosgei

    I run Lucid. I get this error
    “laptop:~$ /etc/init.d/apache2 restart
    * Restarting web server apache2 apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
    apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
    (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    Unable to open logs”
    Somebody please email me how to configure hostnames and IPS. I’ve installed LAMP

  63. Gustavo Silva

    I was beating my head against the wall trying to install PMB. Aparently I had a permission problem. The command sudo chmod -R 755 /var/www/ worked for me! Thanks.

  64. Keith

    firefox http://localhost/~keith doesnot work but localhost works any ideas why .

  65. Mikal

    Hi @Keith. Has this same problem what I done is comment few lines in /etc/apache2/mods-avalible/php5.conf there is an option what turn off php in users dir just comment and will go ! :)

    good Luck

  66. eZp4N

    @Kosgei you need to run it as root, and in this directory:

    sudo /usr/local/apache/bin/apachectl restart

    Also, in httpd.con (usr/local/apache/conf/httpd.conf) edit the Listen line:

    Listen 127.0.0.1:80

  67. ramputhusseri

    Thank you for php support

  68. PM

    Thanks!

  69. Khuram Javaid

    HI Good tutorial

    I think that you should add

    sudo chown username /var/www

    as the last line of the commands.

    Great work anyways.

  70. depstan

    the code is ok know please help me the php file is not copied into the /var/www/
    what to do how to run the name.php file after installing all the above mentioned help me please urgent

  71. Jesse

    depstan,
    Please let me know if you fixed it. I’m having the same issue. I’ve tried everything above on Maverick, but still not solved. Thanks

    Jesse,

  72. Alas

    It is alway a pleasure to have good people around.
    Thank you guys!

    Renato

  73. Suku

    Ok, I see that this one is a bit old article, but I just have to thank you, Mikal, for your suggestion helped me out after an hour’s effort to make my php scripts run. Thanks a lot!

    Suku

  74. Massimo

    Than you! great I have solved my problem in 5 minutes!

  75. Daniel

    some guys still recommend the pre-fork (multiprocess rather than multithreaded) apache2 version because they say some of the php libraries are not thread safe…

    also to add that any additional php module can be added also with apt-get install, all of them are prefixed with php5-. I needed to install gd like that.

  76. geek

    Thanks for brief explaination…

  77. 9sd

    I can remember trying to setup php and Apache on my Windows machine and it not working. This took 10 minutes and it works like a charm. Thank You! Now I can actually get round to learning php……

  78. Noorino1977

    Thank you very Much, I Had been searched for 1 week every night. it is solved in here

  79. Amit

    On Ubuntu natty my apache2 php was working fine. Then to maybe add perl cgi support modified the site1 file and added AddHandler cgi-script .cgi .pl
    Now firefox asks to download php, chromium downloads and shows at bottom.
    Since then have reinstalled apache2, php5, sudo ensite site1, moved site1 to older version ( though public_html folder not /var/www ) sudo enmod php5 ( which says already enabled ). Maybe missing some other change.

  80. Amit

    phpmyadmin runs fine ( localhost/phpmyadmin ), isn’t this also php code calling index.php, file permissions of publc_html 755, owner though is user

  81. Amit

    Have reinstalled natty as needed php quickly, data was saved as it was in /home partition, maybe had messed up the sites-available and installed packages more than once, now have got perl cgi-bin working fine and also setup multiple virtual hosts for multiple cgi-bins

  82. Huskynarr

    Thanks…. one hour of searchs…… than that and it runs.:)
    It great. :)

  83. Briant

    I tried this code [sudo /etc/init.d/apache2 restart] and I got this. Any ideas on how to fix it?

    * Restarting web server apache2 apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

    … waiting apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

    [ OK ]

  84. Dan

    Hello,
    I just fixed my error
    i had it all working except php files would show up blank.

    Me being an idiot and not realising it could be an error in the script. i enabled errors in php.ini and now it all works

    Thanks guys :P

  85. jyothi

    how can we know if xml is there in my system or not(mine is natty narhwal)

  86. ravicioso

    Thanks, it worked for me too.

  87. Thiyagarajan Veluchamy

    Thanks, its worked well, good work.

Enter Your Email Here to Get Access for Free:

Go check your email!