82

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?

0

7 Answers 7

103

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
7
  • 33
    for php7 apt-get install libapache2-mod-php7.0 Oct 4, 2016 at 18:05
  • 4
    libapache2-mod-php will install libapache2-mod-php7.0. We don't need to specify the version explicitly for php7
    – Junaid
    Jul 30, 2017 at 12:22
  • 4
    There is also now apt-get install libapache2-mod-php7.2 for those with 7.2.3 May 2, 2018 at 19:23
  • 4
    sudo apt-get install libapache2-mod-php* for php7 kinda versions to auto install without the 5 specifically Apr 3, 2019 at 20:06
  • 1
    for php 7.4: apt-get install libapache2-mod-php7.4
    – gray
    Mar 7, 2021 at 20:20
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).

6
  • 1
    iamjonesy may also need to add a LoadModule command for php5_module.
    – xuinkrbin.
    Feb 16, 2013 at 5:46
  • 1
    This is exactly what I need to eventually make php on mac osx 10.10 yosemite work. Thx a lot :-) Jul 14, 2014 at 4: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. :-) Jan 30, 2015 at 21:45
  • This posts states that AddType should be text/html and addHandler should be application/x-httpd-php, which sounds reasonable. (serverfault.com/questions/394250/…) What's correct? Jul 3, 2019 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 4, 2019 at 6:05
15

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.

2
  • 1
    To clarify, a "short open tag" is <?. Instead, you can replace this with <?php, if you don't want to change the default configuration. A closing tag is apparently no longer needed then. Source: php.net/manual/en/language.basic-syntax.phptags.php Nov 17, 2019 at 23:04
  • This solved my problem after upgrading PHP from version 7.1 to 7.4. I had the short tags in a script, changed to the full php tags and it solved it! Thanks!
    – J. Volkya
    Dec 19, 2020 at 14:13
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.

2
  • 1
    thanks for your reply. The php5apache.dll file does not exist in my PHP directory. Is it always called php5apache?
    – iamjonesy
    Aug 24, 2010 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, 2014 at 3: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.

1
  • I also disagree with @JaroslavKadlec, I installed out of order and removing and reinstalling did the trick for me
    – Kabb5
    Jul 22, 2015 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

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

1
  • 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). Dec 15, 2018 at 21:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.