Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my PHP files showing as plain text? [duplicate]

Tags:

php

apache

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?

like image 890
iamjonesy Avatar asked Aug 24 '10 10:08

iamjonesy


People also ask

Why is my PHP code showing up as text?

You've written your first PHP program, but when you go to run it, all you see in your browser is the code—the program doesn't actually run. When this happens, the most common cause is that you are trying to run PHP somewhere that doesn't support PHP.


7 Answers

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
like image 185
dav Avatar answered Oct 04 '22 11:10

dav


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

like image 37
Kris Avatar answered Oct 04 '22 10:10

Kris


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.

like image 20
Roman Bekkiev Avatar answered Oct 04 '22 12:10

Roman Bekkiev


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.

like image 44
Piskvor left the building Avatar answered Oct 04 '22 12:10

Piskvor left the building


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.

like image 34
user3604332 Avatar answered Oct 04 '22 10:10

user3604332


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

like image 29
Satyanarayana Avatar answered Oct 04 '22 10:10

Satyanarayana


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)

like image 23
Henrik Sommerland Avatar answered Oct 04 '22 10:10

Henrik Sommerland