20

Apache is rendering PHP files as text after Ubuntu 16.04 upgrade.

Apache is installed and running. PHP7 is running. When I execute the following command I get the correct output:

php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";'

However when I access http://localhost/info.php or any other php file I get the file contents instead of the page.

Most of the help online is for older versions of both Ubuntu and PHP, so I' m not certain they are reliable or not.

UPDATE: Apache won't start after installing libapache2-mod-php7.0 and enabling php7.0. See output:

douglas@douglas-acer:~$ sudo service apache2 restart
douglas@douglas-acer:~$ sudo apt-get install libapache2-mod-php
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libapache2-mod-php
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/2.960 B of archives.
After this operation, 15,4 kB of additional disk space will be used.
Selecting previously unselected package libapache2-mod-php.
(Reading database ... 263897 files and directories currently installed.)
Preparing to unpack .../libapache2-mod-php_1%3a7.0+35ubuntu6_all.deb ...
Unpacking libapache2-mod-php (1:7.0+35ubuntu6) ...
Setting up libapache2-mod-php (1:7.0+35ubuntu6) ...
douglas@douglas-acer:~$ sudo service apache2 restart
douglas@douglas-acer:~$ sudo a2enmod php7.0
Considering conflict php5 for php7.0:
Enabling module php7.0.
To activate the new configuration, you need to run:
  service apache2 restart
douglas@douglas-acer:~$ sudo service apache2 restart
Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
douglas@douglas-acer:~$ sudo apt-get purge php5
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'php5' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

The output of systemctl status apache2.service :

● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: failed (Result: exit-code) since Dom 2016-04-24 08:21:35 BRT; 14s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 9216 ExecStart=/etc/init.d/apache2 start (code=exited, status=1/FAILURE)

Abr 24 08:21:35 douglas-acer apache2[9216]:  * The apache2 configtest failed.
Abr 24 08:21:35 douglas-acer apache2[9216]: Output of config test was:
Abr 24 08:21:35 douglas-acer apache2[9216]: [Sun Apr 24 08:21:35.235583 2016] [:crit] [pid 9226:tid 140666367190912] Apache is running a threade
Abr 24 08:21:35 douglas-acer apache2[9216]: AH00013: Pre-configuration failed
Abr 24 08:21:35 douglas-acer apache2[9216]: Action 'configtest' failed.
Abr 24 08:21:35 douglas-acer apache2[9216]: The Apache error log may have more information.
Abr 24 08:21:35 douglas-acer systemd[1]: apache2.service: Control process exited, code=exited status=1
Abr 24 08:21:35 douglas-acer systemd[1]: Failed to start LSB: Apache2 web server.
Abr 24 08:21:35 douglas-acer systemd[1]: apache2.service: Unit entered failed state.
Abr 24 08:21:35 douglas-acer systemd[1]: apache2.service: Failed with result 'exit-code'.

The Output of journalctl -xe

-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit apache2.service has finished shutting down.
Abr 24 08:37:59 douglas-acer systemd[1]: Starting LSB: Apache2 web server...
-- Subject: Unit apache2.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit apache2.service has begun starting up.
Abr 24 08:37:59 douglas-acer apache2[10261]:  * Starting Apache httpd web server apache2
Abr 24 08:37:59 douglas-acer apache2[10261]:  *
Abr 24 08:37:59 douglas-acer apache2[10261]:  * The apache2 configtest failed.
Abr 24 08:37:59 douglas-acer apache2[10261]: Output of config test was:
Abr 24 08:37:59 douglas-acer apache2[10261]: [Sun Apr 24 08:37:59.748900 2016] [:crit] [pid 10271:tid 139911432607616] Apache is running a threa
Abr 24 08:37:59 douglas-acer apache2[10261]: AH00013: Pre-configuration failed
Abr 24 08:37:59 douglas-acer apache2[10261]: Action 'configtest' failed.
Abr 24 08:37:59 douglas-acer apache2[10261]: The Apache error log may have more information.
Abr 24 08:37:59 douglas-acer systemd[1]: apache2.service: Control process exited, code=exited status=1
Abr 24 08:37:59 douglas-acer systemd[1]: Failed to start LSB: Apache2 web server.
-- Subject: Unit apache2.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit apache2.service has failed.
-- 
-- The result is failed.
Abr 24 08:37:59 douglas-acer systemd[1]: apache2.service: Unit entered failed state.
Abr 24 08:37:59 douglas-acer systemd[1]: apache2.service: Failed with result 'exit-code'.
Abr 24 08:37:59 douglas-acer sudo[10258]: pam_unix(sudo:session): session closed for user root
Abr 24 08:38:02 douglas-acer sudo[10276]:  douglas : TTY=pts/18 ; PWD=/home/douglas ; USER=root ; COMMAND=/bin/journalctl -xe
Abr 24 08:38:02 douglas-acer sudo[10276]: pam_unix(sudo:session): session opened for user root by (uid=0)
Abr 24 08:38:38 douglas-acer sudo[10276]: pam_unix(sudo:session): session closed for user root
Abr 24 08:39:01 douglas-acer CRON[10297]: pam_unix(cron:session): session opened for user root by (uid=0)
Abr 24 08:39:01 douglas-acer CRON[10299]: (root) CMD (  [ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean)
Abr 24 08:39:01 douglas-acer CRON[10297]: pam_unix(cron:session): session closed for user root
Abr 24 08:39:01 douglas-acer CRON[10298]: pam_unix(cron:session): session opened for user root by (uid=0)
Abr 24 08:39:01 douglas-acer CRON[10300]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && /usr/lib/php/sessionclean)
Abr 24 08:39:02 douglas-acer CRON[10298]: pam_unix(cron:session): session closed for user root
Abr 24 08:39:51 douglas-acer sudo[10122]: pam_unix(sudo:session): session closed for user root
Abr 24 08:39:53 douglas-acer sudo[10368]:  douglas : TTY=pts/5 ; PWD=/home/douglas ; USER=root ; COMMAND=/bin/journalctl -xe
Abr 24 08:39:53 douglas-acer sudo[10368]: pam_unix(sudo:session): session opened for user root by (uid=0) 

Regarding the error Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP I did find some help in https://wiki.archlinux.org/index.php/Apache_HTTP_Server regarding this issue, however the file paths didn't match the ones in Ubuntu, so I didn't try to apply the recommended fix.

4 Answers 4

27

The php command is provided by php7.0-cli package on Ubuntu 16.04. Running the CLI is orthogonal to the web server configuration.

You need to install and configure one of the web SAPIs - apache2, fpm or cgi. The most easy is to install apache2 SAPI.

  1. To install apache2 SAPI, run apt-get install libapache2-mod-php (this will pull the default PHP version, currently libapache2-mod-php7.0). The package should enable itself and switch to apache2 prefork MPM that's the only MPM supported. If not, try to run a2enmod php7.0 and look for errors. The installation should switch from event MPM to prefork MPM automatically, but if it doesn't, you can switch it manually by doing sudo a2dismod mpm_event followed by sudo a2enmod mpm_prefork.

  2. To install FPM SAPI, run apt-get install php-fpm and then enable FPM by running a2enconf php7.0-fpm. The FPM SAPI is more secure, but harder to configure correctly.

  3. To install CGI SAPI, run apt-get install php-cgi and then enable CGI by running a2enconf php7.0-cgi. Please not that CGI is not recommended way how to run PHP, but it might come handy in special deployments.

8
  • I've edited my question in order to provide more details.
    – machadoug
    Apr 24, 2016 at 11:57
  • @machadoug edited the answer to include description how to switch MPMs
    – oerdnj
    Apr 24, 2016 at 12:18
  • Do you have any idea on how to get it fixed?
    – machadoug
    Apr 24, 2016 at 13:58
  • 1
    @machadoug Yes, I added "The installation should switch from event MPM to prefork MPM automatically, but if it doesn't, you can switch it manually by doing sudo a2dismod mpm_event followed by sudo a2enmod mpm_prefork."
    – oerdnj
    Apr 24, 2016 at 16:21
  • 1
    Is there a step missing from option 2? I've installed apache2 and php-fpm, and run the a2enconf command. All succeeded, and both services are running, but my PHP files are being output directly without php processing them. No errors in any logs for Apache or PHP-FPM. Jun 6, 2016 at 20:57
1

I made mine running again by installing libapache2-mod-php7.0. Hope this helps.

0

I also got bitten by this issue in a different way:

By default PHP scripts will not run in user directories. So if your server supports public_html in the users' home folder and want to let users run php in it, you should remove the part in php7.0.conf that disables it.

0

In the latest Apache 2.0, for Ubuntu 16.04.2 I found that Apache is pre-configured with threads enabled, but the default PHP 7 is not. If the PHP is not compiled with threadsafe, you will need to switch to the prefork MPM, which does not use threads.

To do this, first disable:

sudo a2dismod mpm_event 

Then enable:

sudo a2enmod mpm_prefork 

You must do this in the right order as they are mutually exclusive.

Then restart Apache:

sudo service apache2 restart

You should be good to go without issues now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .