84

I've been writing PHP applications using PHP for a while in WAMP. Now I'm installing PHP and Apache HTTP Server separately on my work PC. I've installed PHP 5, and the latest Apache. I go to localhost and see it works!

Now I add a file called test.php which displays:

<?php
    phpinfo();
?>

But in the browser it just displays plain text. Is there somewhere I have explicitly tell it to use PHP 5?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iamjonesy
  • 24,732
  • 40
  • 139
  • 206

7 Answers7

105

You should install the PHP 5 library for Apache.

For Debian and Ubuntu:

apt-get install libapache2-mod-php5

And restart the Apache:

service apache2 restart
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dav
  • 8,931
  • 15
  • 76
  • 140
79

You'll need to add this to your server configuration:

AddType application/x-httpd-php .php

That is assuming you have installed PHP properly, which may not be the case since it doesn't work where it normally would immediately after installing.

It is entirely possible that you'll also have to add the php .so/.dll file to your Apache configuration using a LoadModule directive (usually in httpd.conf).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kris
  • 40,604
  • 9
  • 72
  • 101
  • 1
    iamjonesy may also need to add a LoadModule command for php5_module. – xuinkrbin. Feb 16 '13 at 05:46
  • 1
    This is exactly what I need to eventually make php on mac osx 10.10 yosemite work. Thx a lot :-) – Golden Thumb Jul 14 '14 at 04:09
  • In a particular case where we downloaded a client's .htaccess file, there was an extra (and incorrect) instantiation directive "AddHandler application/x-httpd-php5 .php", which works for their old server configuration, but not ours. Once we commented this line out in .htaccess file for this site, PHP worked fine again. :-) – Marty McGee Jan 30 '15 at 21:45
  • This posts states that AddType should be text/html and addHandler should be application/x-httpd-php, which sounds reasonable. (https://serverfault.com/questions/394250/apache-configurations-for-php-addtype-text-html-php-or-addtype-application-x) What's correct? – user2345998 Jul 03 '19 at 14:19
  • @user2345998: Seems to me now that AddHandler would be semantically more "correct". Back in 2010, this is what worked for me and many others in whatever apache version we all had running. Clearly, the answer you linked references infinitely more documentation though, I consider it better than mine here. – Kris Jul 04 '19 at 06:05
  • @Kris Thanks for your reply. Maybe you should update the answer - with apache 2.4 this doesn't work for me and caused the browser to download the parsed php file. – user2345998 Jul 04 '19 at 07:25
16

Yet another reason (not for this case, but maybe it'll save some nerves for someone) is that in PHP 5.5 short open tags <? phpinfo(); ?> are disabled by default.

So the PHP interpreter would process code within short tags as plain text. In previous versions PHP this feature was enable by default. So the new behaviour can be a little bit mysterious.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roman Bekkiev
  • 3,010
  • 1
  • 24
  • 31
14

You need to configure Apache (the webserver) to process PHP scripts as PHP. Check Apache's configuration. You need to load the module (the path may differ on your system):

LoadModule php5_module "c:/php/php5apache.dll"

And you also need to tell Apache what to process with PHP:

AddType application/x-httpd-php .php

See the documentation for more details.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • 1
    thanks for your reply. The php5apache.dll file does not exist in my PHP directory. Is it always called php5apache? – iamjonesy Aug 24 '10 at 11:53
  • 1
    The `.dll` file is called `php5apache2_2.dll` for Apache 2.2 and `php5apache2_4.dll` for Apache 2.4 – user May 10 '14 at 03:45
9

You might also, like me, have installed php-cgi prior to installing Apache and when doing so it doesn't set up Apache properly to run PHP, removing PHP entirely and reinstalling seemed to fix my problem.

user3604332
  • 91
  • 1
  • 2
  • I also disagree with @JaroslavKadlec, I installed out of order and removing and reinstalling did the trick for me – Kabb5 Jul 22 '15 at 19:18
6

You will need to add handlers in Apache to handle php code.

Edit by command sudo vi /etc/httpd/conf/httpd.conf

Add these two handlers

  AddType application/x-httpd-php .php
  AddType application/x-httpd-php .php3

at position specified below

<IfModule mime_module>

 AddType application/x-compress .Z
 AddType application/x-gzip .gz .tgz

--Add Here--


</IfModule>

for more details on AddType handlers

http://httpd.apache.org/docs/2.2/mod/mod_mime.html

MysticForce
  • 1,253
  • 1
  • 16
  • 27
Satyanarayana
  • 139
  • 1
  • 4
4

Are you using the userdir mod?

In that case the thing is that PHP5 seems to be disabling running scripts from that location by default and you have to comment out the following lines:

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

in /etc/apache2/mods-enabled/php5.conf (on a ubuntu system)

  • Brand spanking new Ubuntu 18 install w apache2, and nowhere else did I see this mentioned! Can I send you a pizza? croth you-know-what-goes-after-a-name the dance dot net (no spaces). – Charles Roth Dec 15 '18 at 21:03