[php] Apache is downloading php files instead of displaying them

OS and server information:

  • CentOS 6.4 (Final)
  • Apache 2.2.15
  • PHP 5.5.1

I previously had php 5.3.x installed but decided to upgrade. I first uninstalled the php 5.3.x and then installed php 5.5.1 but after the installation completed apache did not parse the php files it just downloaded them. I have checked similar questions here in stackoverflow but none of them have helped me so far.

For the record I have the following lines in my httpd.conf and php.conf that should make php work but don't:

AddHandler application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml
AddType application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml
AddType application/x-httpd-php-source .phps
AddHandler php5-script .php

I would really appreciate any help. Thank you.

EDIT:

I have these lines in the php.conf

<IfModule !worker.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>

EDIT:

By removing the

AddType application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml

apache no longer downloads the file. Now apache is showing the source code, but not all of it just part. I added

AddType text/html .php

but no luck.

This question is related to php apache centos

The answer is


I had a similar problem to the OP when upgrading php5 from an older version, to 5.5.9, which is the version installed with Mint 17.

I'm running a LAMP setup on a machine on my local network, which I use to preview changes to websites before I upload those changes to the actual live server. So I maintain a perfect local mirror of the actual site.

After the upgrade, files which run and display perfectly on the actual site would not display, or would only display html on the local machine. PHP was not parsed. The phpinfo() command worked, so I knew php was otherwise working. The log generated no errors. Viewing the page source showed me the actual php code.

I had constructed a test.php page that contained the following code:

<?php phpinfo(); ?>

This worked. Then I discovered when I changed <?php to <? the command no longer worked. All my php sites use <? instead of <?php which might not be ideal, but it's the reality. I fixed the problem by going to /etc/php5/apache2 , searching for "short_open_tag" and changing the value from Off to On.


If none of the above works,

try commenting out the line

SetHandler ....

and restart apache using

/etc/init.d/httpd restart

It should work!


I had this problem. It turned out that I had both nginx and apache installed and automatically starting on boot. The problem was that nginx was binding to the http port first which prevented apache from starting.


If you have virtualmin try to comment out these lines in your apache configuration in /etc/apache2/sites-available

  #RemoveHandler .php
  #RemoveHandler .php7.0
  #php_admin_value engine Off

I came across this issue today and none of the solutions described worked for me. So here is another possible cause:

If you have something like

AddHandler x-mapp-php6 .php3 .php4 .php .phtml

in a .htaccess file of your web content folder, it can cause your PHP scripts to stop working. In my case the server did not know the x-mapp-php6 type, since that .htaccess file was something I imported from a different web host when I transferred the website content.

Just removing the AddHandler line from the .htaccess file solved it for me.


In case someone is using php7 under a Linux environment

Make sure you enable php7

sudo a2enmod php7

Restart the mysql service and Apache

sudo systemctl restart mysql
sudo systemctl restart apache2

I got this kind of problem. This is how I solve it. After installed Apache then I installed PHP using this command.

sudo apt-get install php libapache2-mod-php

it executes correctly but I request .php file from Apache, it gives without executing the PHP script.

Then I check PHP is enabled.

$ cd /etc/apache2
$ ls -l mods-*/*php*

but it didn't show any results. I check installed PHP packages.

$ dpkg -l | grep php| awk '{print $2}' |tr "\n" " "

Different type of PHP versions installed to my computer. Then I remove some PHP packages from my previous list, using apt-get purge.

sudo apt-get purge libapache2-mod-php7.0 php7.0 php7.0-cli php7.0-common php7.0-json

I reinstall PHP

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

Verify that the PHP module is loaded

$ a2query -m php7.0

if not enabled with:

$ sudo a2enmod php7.0

Restart Apache server

$ sudo systemctl restart apache2

Finally, I check PHP process on Apache

create an empty file

sudo vim /var/www/html/info.php

Add this content to info.php & save.

<?php
  phpinfo();
?>

Check on browser:

http://localhost/info.php

it shows correctly.I think this will help anyone.


PHP56

vim /etc/httpd/conf/httpd.conf

LoadModule php5_module        libexec/apache/libphp5.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

After updating PHP to 7.3, PHP scripts where run with www-data instead of $USER like before.

I needed to reinstall and activate PHP-FPM :

sudo apt-get install php-fpm
sudo a2enmod proxy_fcgi setenvif
sudo service apache2 restart
sudo a2enconf php7.3-fpm
sudo service apache2 restart

To ensure everything was ok for Virtualmin, i used the Re-Check Configuration wizard /virtual-server/check.cgi, under Vitualmin/System Settings.

After that, Apache/PHP was downloading php files instead of running them. So i needed to edit /etc/apache2/mods-available/php7.3.conf to comment the row SetHandler application/x-httpd-php like below :

<FilesMatch ".+\.ph(ar|p|tml)$">
#    SetHandler application/x-httpd-php
</FilesMatch>

After restarting Apache, it solved my issue, hope this help.

Take care of browser cache too.

My system :

Ubuntu          16.04.6 LTS
Webmin version      1.932
Usermin version     1.780
Virtualmin version  6.08
Apache version      2.4.41
PHP versions        7.3.12
PHP-FPM         7.3.12 Server

If Your .htaccess have anything like this

AddType application/x-httpd-ea-php56 .php .php5 .phtm .html .htm

When i upgraded from PHP 7.2 to PHP 7.4, i also got same issue. Worked by doing following:-

  1. In [domain].conf file, commented following:

    php_admin_value engine Off
    

    And Added:

    AddType  application/x-httpd-php-source  .phps
    AddType text/html .php
    
  2. Disable mod 7.2 and enable 7.4 by following:

    a2dismod php7.2
    a2enmod php7.4
    
  3. In /etc/apache2/mods-enabled/php7.4.conf file, comment following:

    SetHandler application/x-httpd-php
    php_admin_flag engine Off
    

this solved the problem for me (I have php7 installed):

sudo apt-get install libapache2-mod-php7.0

sudo service apache2 restart


I had the same problem after using zypper rm php* to uninstall PHP and installing it again with zypper in php7 php7-gd php7-gettext php7-mbstring php7-mysql php7-pear

I solved it by enabling the apache2 module and restarting the webserver:

a2enmod php7 && service apache2 restart

If Your .htaccess have anything like this ... AddHandler application/x-httpd-php53 .php .php5 .php4 .php3 then comment it and try again refreshing this worked for me...


I had this problem and if you actually never played with your server configuration settings, then your problem is 90% in your .htaccess file

You either modify .htaccess file LOCALLY, ore delete it (LOCALLY)


I previously has a similar issue, after upgrading from 5.3 to 5.4. But my setup looks a little bit different as that I'm running Debian and using fcgid to server the PHP pages, and not the PHP5 apache/cgi module. So after I upgraded, it also installed php5_cgi, which collided with my fcgid setup, and would not execute PHP files anymore.

I had to disable the Apache Module and restart Apache

a2dismod php5_cgi
/etc/init.d/apache2 restart

Once the php5_cgi module was out of the way, fcgid was able to serve PHP pages again.


This might be happening due to the missing modules required for your php. Assuming you have php7 installed, search available php7 modules using

sudo apt-cache search php7-*

Above command will list all available PHP7 modules for installation. You can begin installation of modules like,

sudo apt-get install libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json

For people who have found this post from Google almost 6 years in the future (and beyond!), you may run into this problem with Apache 2 and PHP 7 while also using the UserDir module.

Another possible cause of this problem could be that you are trying to run the script in a "user directory" from the the UserDir module. Running PHP scripts in user directories is disabled by default. You will run into this problem if the script is in the public_html directory in your home folder and you are trying to access it from http://localhost/~your_username.

To fix this, open up /etc/apache2/mods-enabled/php7.2.conf. You must comment or delete the tag block at the bottom that reads

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

Please take a look at your addtype directives.

It looks to me like Apache is telling the browser that it's sending a document type of application/php for scripts with extensions like .php5. In fact Apache is supposed to tell the browser that the script is outputting text/html.

Please try this:

AddType text/html .php

Regarding the suggestion above that you should tell the browser that you are outputting a PHP script: It seemed like an unusual idea to me. I googled it and found that there is quite a bit of discussion about it on the web. Apparently there are cases where you might want to say that you are sending a PHP script (even though Apache is supposed to execute the script and emit text/html,) and there are also cases where the browser simply doesn't recognize that specific Mime Type.

Clearing your browser cache is always a good idea.

In case it's helpful here's a copy of my /etc/httpd/conf.d/php.conf file from a server running CentOS 5.9:

#        
# PHP is an HTML-embedded scripting language which attempts to make it                                             
# easy for developers to write dynamically generated webpages.                                                  
#
<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>

#
# Cause the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps

Ok... I know that there are 1.000.000 answers to this questions already, - but I have spent at least 6 effective hours, figuring this one out; and I have googled it hundreds of times and not found a single post about it. So I figured that I would add the solution to my problem here.

The conclusion

If I commented these two lines out in my .conf-files in the /etc/apache2/[[SERVER-NAME].conf-file:

php_admin_value engine Off
IPCComTimeout 31

I have no idea what they do or how they got there, - but it is in every one of my .conf-files. And if I remove those lines and ensure that there is a symlink in /etc/apache2/sites-enabled/-folder, then it doesn't download the index.php - and every works as it should.

The entire story

I have VirtualMin installed on an Ubuntu 16.04 VPS. I upgraded to PHP version 7.2. Shortly after that, I updated the Ubuntu-version and struck a 'Kernel Offset: Disabled'-error. So I had to go delete the latest Ubuntu-version, - and when my OS booted up again: BOOM! I got the error that his post talks about: For every site on my VPS, it simply downloaded the index.php instead of showing it.

I tried all kinds of stuff:

  • Removed PHP7.2 and installed PHP5.6 (I know now, that the PHP-version has nothing to do with it; it's the apache-configuration that needs work).
  • Tried enabling and disabling apache modules, on the existing installation, but without luck.
  • Then I removed apache completely and installed it again, where-after the problem was still there!
  • Tried playing around with the Virutal Server setup in VirtualMin ( Webmin >> Servers >> Apache Webserver ).
  • Checked the configuration on a single Virtual-server ( Virtualmin >> System Settings >> Re-Check Configuration )... This step was pretty nice, since it told which module in Apache was missing; where-after I could enable it with a2enmod [MODULE_NAME]. And I found the module name by Googling around. I had to active about 6-8 modules, before I got past that step in the validation - and it took a couple of minutes before the cache ran out, - so doing this was a tedious step.
  • And lastly, I figured out above-written conclusion - together with the symlinks, - and then I got it to work. I had to go through it for every site on my VPS, though.

I spent two days tracking this and found out that I was putting my PHP scripts in the wrong directory.

On my standard Ubuntu installation, I was putting the scripts in /var/www. They should have been in /var/www/html.

I just started PHP work, so I don't know if my solution relates to the version change you went through.


It's also possible that you have nginx running but your php is set up to run with apache. To verify, run service nginx status and service apache2 status to see which is running. In the case that nginx is running and apache is not, just run sudo service nginx stop; sudo service apache2 start and your server will now serve php files as expected.


After struggling a lot I finally solved the problem.

If you are prompted to download a .php file instead of executing it, then here is the perfect solution: I assume that you have installed PHP5 already and still getting this error.

$ sudo su
$ a2enmod php5

This is it.

But If you are still getting the error :

Config file php5.conf not properly enabled: /etc/apache2/mods-enabled/php5.conf is a real file, not touching it

then do the following:

Turns out files shouldn't be stored in mods-enabled, but should rather be stored in mods-available. A symlink should then be created in mods-enabled pointing to the file stored in mods-available.

First remove the original:

$ mv /etc/apache2/mods-enabled/php5.conf /etc/apache2/mods-available/

Then create the symbolic link:

$ ln -s /etc/apache2/mods-available/php5.conf /etc/apache2/mods-enabled/php5.conf

I hope your problem is solved.


I had similar symptoms, yet another solution: in /etc/apache2/mods-enabled/php5.conf there was a helpful advice in the comment, which I followed:

# To re-enable php in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.

I have the same problem. Apache doesn't load php files from a certain website, just downloaded it. I read this post and the answers and I have seen I've got this line into the last place of the .htaccess file:

AddHandler x-mapp-php5.5  .php

I have commented it and everything works fine.

Thanks to all !!!


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to apache

Enable PHP Apache2 Switch php versions on commandline ubuntu 16.04 Laravel: PDOException: could not find driver How to deploy a React App on Apache web server Apache POI error loading XSSFWorkbook class How to enable directory listing in apache web server Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details How to enable php7 module in apache? java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer

Examples related to centos

How to uninstall an older PHP version from centOS7 Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details pip install - locale.Error: unsupported locale setting ssh : Permission denied (publickey,gssapi-with-mic) How to change the MySQL root account password on CentOS7? Completely remove MariaDB or MySQL from CentOS 7 or RHEL 7 ffprobe or avprobe not found. Please install one How to check all versions of python installed on osx and centos Cannot find java. Please use the --jdkhome switch VirtualBox: mount.vboxsf: mounting failed with the error: No such device