在Ubuntu 16.04升级之后,PHP呈现为文本

在Ubuntu 16.04升级之后,Apache将PHP文件呈现为文本。

Apache已安装并正在运行。 PHP7正在运行。 当我执行以下命令时,我得到正确的输出:

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

但是当我访问http://localhost/info.php或任何其他php文件时,我得到文件内容而不是页面。

大多数在线帮助是针对Ubuntu和PHP的旧版本,所以我不确定它们是否可靠。

更新:Apache安装libapache2-mod-php7.0并启用php7.0后无法启动。 见输出:

 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. 

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

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) 

关于错误Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP我在https://wiki.archlinux.org/index.php/Apache_HTTP_Server中找到了一些有关此问题的帮助,但是文件路径与Ubuntu中的路径不匹配,所以我没有尝试应用建议的修复程序。

php命令由Ubuntu 16.04上的php7.0-cli包提供。 运行CLI与Web服务器配置正交。

您需要安装和配置其中一个Web SAPI – apache2,fpm或cgi。 最简单的是安装apache2 SAPI。

  1. 要安装apache2 SAPI,请运行apt-get install libapache2-mod-php (这将拉出默认的PHP版本,目前为libapache2-mod-php7.0 )。 该软件包应启用自身并切换到apache2 prefork MPM,这是唯一支持的MPM。 如果没有,尝试运行a2enmod php7.0并查找错误。 安装应该自动从事件MPM切换到prefork MPM,但如果没有,你可以通过sudo a2dismod mpm_event然后sudo a2enmod mpm_prefork手动切换它。

  2. 要安装FPM SAPI,请运行apt-get install php-fpm ,然后通过运行a2enconf php7.0-fpm启用FPM。 FPM SAPI更安全,但更难配置。

  3. 要安装CGI SAPI,请运行apt-get install php-cgi ,然后运行a2enconf php7.0-cgi启用CGI。 请注意,不建议使用CGI如何运行PHP,但在特殊部署中它可能会派上用场。

我通过安装libapache2-mod-php7.0让我再次运行。 希望这可以帮助。

我也以不同的方式被这个问题所困扰:

默认情况下,PHP脚本不会在用户目录中运行。 因此,如果您的服务器支持用户主文件夹中的public_html并希望让用户在其中运行php,则应删除php7.0.conf中禁用它的部分。

在最新的Apache 2.0中,对于Ubuntu 16.04.2,我发现Apache预先配置了线程,但默认的PHP 7不是。 如果PHP没有使用threadsafe编译,则需要切换到不使用线程的prefork MPM。

要执行此操作,请先禁用:

 sudo a2dismod mpm_event 

然后启用:

 sudo a2enmod mpm_prefork 

您必须按正确的顺序执行此操作,因为它们是互斥的。

然后重启Apache:

 sudo service apache2 restart 

你现在应该没有问题。