1

php files do not load in the browser, but are downloaded. I have read through many solutions to this problem on both serverfault.com and stackoverflow. (Many of the posts are several years old with older versions of Apache and php, and some of the config files and their locations have changed.) I've found a common set of solutions to the problem, but none of them have worked for me. The following links contain examples of the suggestions I have tried that didn't solve my problem.

apache2 on ubuntu - php files downloading

Apache shows PHP code instead of executing it

https://serverfault.com/questions/25227/why-is-php-script-downloaded-instead-of-executed

https://serverfault.com/questions/286882/apache-is-not-interpreting-php-files

I installed apache2. If I go to "localhost" in my browser, it serves up the "Apache2 Ubuntu Default Page". I installed php. "php7.4.conf" and "php7.4.load" appears in both /etc/apache2/mods-available and /etc/apache2/mods-enabled. I verify that php is enabled with sudo a2enmod php7.4, which gives

Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.4:
Module php7.4 already enabled

Based off multiple replies in different questions, I have ended up with the following in my /etc/apache2/apache2.conf file (note this is not the entire file):

Include /etc/phpmyadmin/apache.conf

AddType application/x-httpd-php .php

# Use for PHP 7.x:
LoadModule php7_module        modules/libphp7.4.so
AddHandler php7-script php 
AddType application/x-httpd-php-source .phps
AddHandler application/x-httpd-php .phps
AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .php

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php

Notes: I have verified that the apache2.conf file is being executed, by adding a bad line to the file and attempting to restart apache, which resulted in an error. libphp7.4.so is located in /usr/lib/apache2/modules.

And my /etc/apache2/mods-available/php7.4.conf and /etc/apache2/mods-enabled/php7.4.conf files look like this:

<FilesMatch ".+\.ph(ar|p|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
#    # Deny access to raw php sources by default
#    # To re-enable it's recommended to enable access to the files
#    # only in specific virtual host or directory
#    Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
#     Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
# 
# 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.
# <IfModule mod_userdir.c>
#     <Directory /home/*/public_html>
#         php_admin_flag engine Off
#     </Directory>
# </IfModule>

After every change I have made, I have restarted apache with sudo service apache2 restart. I do a test and still php files are downloaded. I'm using a simple php file called verify.php, which contains the following:

<?php
  phpinfo();
?>

I've never worked with these things before so I could be missing something obvious? For some additional context, I'm working on a school project where I plan to use a php page to query a database and generate a table with the results.

Self Dot
  • 352
  • 3
  • 14

1 Answers1

0

In my case of similar hardship:

  1. I do not need those statements in apache2.conf:List item

  2. I have similar content in my /etc/apache2/mods-available/php7.4.conf and /etc/apache2/mods-enabled/php7.4.conf

  3. I ran:

    sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7.4
    

However what got me to work is a misspelled 'php' as 'phd' (in SetHandler application/x-httpd-php of etc/apache2.conf):

<FilesMatch \.php$>
SetHandler  application/x-httpd-php
</FilesMatch>

I think that is the only needed command to call PHP as handler.

p.s. I have more than one instance of messed up config files.. i haven't found a good control template for all those linux configuration process. (this time the install was started by somebody, i took over the second part)

(This section contains notes and hints specific to Apache 2.x installs of PHP on Unix systems. https://www.php.net/manual/en/install.unix.apache2.php)

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
r poon
  • 633
  • 7
  • 7