PHP not working in Apache2 after system upgrade

Recently upgraded my system from Karmic to Lucid. This involved Apache and PHP being updated I believe.

Now my server is unable to handle PHP files, any navigation to them is triggering downloading rather than parsing.

The PHP module is still loaded in Apache mods-enabled and I have AddType application/x-httpd-php .php in my httpd.conf.

There are no meaningful messages in Apache access or error logs so I am stumped.

Any help would be appreciated. Thanks.


/var/log/apache2/error.log should show a line like the below if PHP was succesfully loaded:

[Tue Aug 30 12:53:36 2011] [notice] Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.9 with Suhosin-Patch configured -- resuming normal operations

Pay attention to the date, if it's something from 2009, you've misconfigured something.

AddType is useless, you need to use:

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

See also the installation instructions on php.net for Apache 2.x.

You should not have a need to add that manually, the package libapache2-mod-php5 installs /etc/apache2/mods-available/php5.conf containing:

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    # 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_value engine Off
        </Directory>
    </IfModule>
</IfModule>

Since it's a conffile, it's possible that it's not installed with the upgrade. To fix that purge it and then install it again:

sudo apt-get purge libapache2-mod-php5
sudo apt-get install libapache2-mod-php5

To enable PHP, you have to run:

sudo a2enmod php5

Restart the webserver after:

sudo /etc/init.d/apache2 restart

Other things to do:

  • check for conflicting settings in /etc/apache2.
  • have you built Apache from source?
  • Run sudo apache2ctl configtest to check your configuration for syntax errors
  • Open http://localhost/server-info to check the loaded configuration and modules. The status module must be loaded for that. If you're running a remote server, you need to add yourself to the Allow from list in /etc/apache2/mods-enabled/status.conf and restart the server afterwards

Well after nothing else worked and Apache was getting very confusing I finally just purged all of the Apache packages and reinstalled them.

Seems to be working fine now, who knows what was wrong with it....

EDIT

I found out what was wrong when I was setting up my Apache configuration settings again. [This blog entry][1] explains it.

Turns out that although the userdir module is not actively in use, the rest of the sites files reside in my ~/public_html folder, and php was being disabled for everything in there.

I guess this must have been a recent addition to the php module which wasn't present in my Karmic settings.


the php5.conf file in /etc/apache2/mods-enabled has a few lines at the bottom that keep php from executing inside of a user directory, so depending on your configuration, this could be your issue also. You can just comment these lines out and apache2 reload