Apache displaying php as text

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:


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

Apache displaying php as text

asked Aug 24, 2010 at 10:56

iamjonesyiamjonesy

24.2k40 gold badges135 silver badges206 bronze badges

0

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

Apache displaying php as text

answered Jun 11, 2015 at 5:29

7

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

Apache displaying php as text

answered Aug 24, 2010 at 11:00

KrisKris

39.2k9 gold badges73 silver badges100 bronze badges

6

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

Apache displaying php as text

answered Nov 4, 2013 at 13:14

Apache displaying php as text

Roman BekkievRoman Bekkiev

2,8201 gold badge21 silver badges29 bronze badges

2

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.

Apache displaying php as text

answered Aug 24, 2010 at 11:00

Apache displaying php as text

2

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.

answered May 5, 2014 at 12:50

1

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



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

--Add Here--



for more details on AddType handlers

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

Apache displaying php as text

MysticForce

1,1431 gold badge14 silver badges27 bronze badges

answered Feb 3, 2016 at 7:25

0

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:


    
        php_admin_flag engine Off
    

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

answered Jul 27, 2015 at 12:29

1

Why is my PHP code showing up as text?

If for some reason, PHP is not installed at server OR PHP is not configured to work with your web server, it is no more being treated as a PHP file. It's just seen as a text file being rendered as HTML text in browser.

Why PHP code is printing instead of executing?

It might be PHP is not enabled in your apache configuration. You can uncomment PHP module by removing '#' sign at the beginning from httpd. conf file. Save file and then restart the web server.

How do I get PHP to work with Apache?

Note that there are several ways to configure Apache and PHP, but this is possibly the quickest method..
Step 1: Download the PHP files. ... .
Step 2: Extract the files. ... .
Step 3: Configure php. ... .
Step 4: Add C:\php to the path environment variable. ... .
Step 5: Configure PHP as an Apache module. ... .
Step 6: Test a PHP file..

Does Apache handle PHP?

Apache does not natively support PHP scripts without a special module. The module that tells Apache how to handle PHP scripts is referred to as a PHP handler. Without a properly configured module, Apache will just send you the PHP file as a download since it doesn't know what else to do.