php 在 apache2 中无法运行

php 在 apache2 中无法运行

从 ubuntu 16.04 更新到 18.04 后,我注意到 php-curl 无法正常工作,但 php 工作正常。我以为问题是 apache2 仍在使用 php7.0 而不是 php7.2,所以我运行了

sudo a2dismod php7.0
sudo a2enmod php7.2
sudo systemctl restart apache2

现在 php 不起作用,但是 html 代码可以呈现...

这将返回一个空白页:

<?php phpinfo(); ?> 

我使用 php:phpinfo -manual 页面中找到的版本仔细检查了 phpinfo() 的实现,如下所示,仍然无法与 apache 一起使用,但当我在 bash 中运行以下命令时它确实有效

my-machine:~$ php public_html/info.php

以下是编辑版本http://php.net/manual/en/function.phpinfo.php

<?php    
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>

我也试过

sudo update-alternatives --set php /usr/bin/php7.2

我按照一页中的指示编辑了 /etc/php/7.2/apache2/php.ini,如下所示:

short_open_tag = On

这是有关我的设置的一些信息。

php 版本

php -v
PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul  4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies

查询

a2query -m php7.2
php7.2 (enabled by site administrator)

apache2 模块列表,注意:出现“AH01574:模块 php7_module 已加载,跳过”的情况是因为我还运行了 =sudo a2enmod php7.0= 希望回到我认为我已经拥有的状态...我确实禁用了 php7.2 并启用了 php7.0 并使用 sudo systemctl restart apache2 重新启动 apache2,但 php 没有被呈现。

apache2ctl -M
[Sun Sep 02 18:03:43.185183 2018] [so:warn] [pid 4166] AH01574: module php7_module is already loaded, skipping
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 dnssd_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php7_module (shared)
 reqtimeout_module (shared)
 setenvif_module (shared)
 status_module (shared)
 userdir_module (shared)

已安装的 php 软件包列表

sudo apt list --installed | grep -i php

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libapache2-mod-php/bionic,bionic,now 1:7.2+60ubuntu1 all [installed]
libapache2-mod-php7.0/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
libapache2-mod-php7.2/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed]
libphp-phpmailer/bionic,bionic,now 5.2.14+dfsg-2.3 all [installed,automatic]
php/bionic,bionic,now 1:7.2+60ubuntu1 all [installed]
php-bz2/bionic,bionic,now 1:7.2+60ubuntu1 all [installed,automatic]
php-cli/bionic,bionic,now 1:7.2+60ubuntu1 all [installed,automatic]
php-common/bionic,bionic,now 1:60ubuntu1 all [installed]
php-curl/bionic,bionic,now 1:7.2+60ubuntu1 all [installed]
php-db/bionic,bionic,now 1.9.2-2ubuntu1 all [installed]
php-gd/bionic,bionic,now 1:7.2+60ubuntu1 all [installed,automatic]
php-getid3/bionic,bionic,now 1.9.15+dfsg-1 all [installed,automatic]
php-gettext/bionic,bionic,now 1.0.12-0.1 all [installed,automatic]
php-mbstring/bionic,bionic,now 1:7.2+60ubuntu1 all [installed,automatic]
php-mcrypt/now 1:7.0+35ubuntu6.1 all [installed,local]
php-mysql/bionic,bionic,now 1:7.2+60ubuntu1 all [installed]
php-pear/bionic,bionic,now 1:1.10.5+submodules+notgz-1ubuntu1 all [installed]
php-php-gettext/bionic,bionic,now 1.0.12-0.1 all [installed,automatic]
php-phpseclib/bionic,bionic,now 2.0.9-1 all [installed,automatic]
php-tcpdf/bionic,bionic,now 6.2.13+dfsg-1ubuntu1 all [installed,automatic]
php-tidy/bionic,bionic,now 1:7.2+60ubuntu1 all [installed]
php-xml/bionic,bionic,now 1:7.2+60ubuntu1 all [installed,automatic]
php-zip/bionic,bionic,now 1:7.2+60ubuntu1 all [installed,automatic]
php5-common/now 5.5.9+dfsg-1ubuntu4.19 amd64 [installed,local]
php5-json/now 1.3.2-2build1 amd64 [installed,local]
php5-mysql/now 5.5.9+dfsg-1ubuntu4.19 amd64 [installed,local]
php7.0/now 7.0.30-0ubuntu0.16.04.1 all [installed,local]
php7.0-cli/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-common/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-gd/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-json/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-mbstring/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-mcrypt/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-mysql/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-opcache/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-readline/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.0-xml/now 7.0.30-0ubuntu0.16.04.1 amd64 [installed,local]
php7.2/bionic-updates,bionic-updates,bionic-security,bionic-security,now 7.2.7-0ubuntu0.18.04.2 all [installed,automatic]
php7.2-bz2/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-cli/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-common/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-curl/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed]
php7.2-gd/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-json/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-mbstring/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-mysql/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-opcache/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-readline/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-tidy/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-xml/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
php7.2-zip/bionic-updates,bionic-security,now 7.2.7-0ubuntu0.18.04.2 amd64 [installed,automatic]
phpmyadmin/bionic,bionic,now 4:4.6.6-5 all [installed]

apt 策略 libapache2-mod-php7.2

sudo apt policy libapache2-mod-php7.2
libapache2-mod-php7.2:
  Installed: 7.2.7-0ubuntu0.18.04.2
  Candidate: 7.2.7-0ubuntu0.18.04.2
  Version table:
 *** 7.2.7-0ubuntu0.18.04.2 500
        500 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages
        100 /var/lib/dpkg/status
     7.2.3-1ubuntu1 500
        500 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 Packages

ls -l /etc/apache2/mods-enabled/php*

ls -l /etc/apache2/mods-enabled/php*
lrwxrwxrwx 1 root root 29 Sep  2 18:48 /etc/apache2/mods-enabled/php7.2.conf -> ../mods-available/php7.2.conf
lrwxrwxrwx 1 root root 29 Sep  2 18:48 /etc/apache2/mods-enabled/php7.2.load -> ../mods-available/php7.2.load

apache2 日志样本

[Mon Sep 03 00:06:52.805155 2018] [mpm_prefork:notice] [pid 5161] AH00163: Apache/2.4.29 (Ubuntu) configured -- resuming normal operations
[Mon Sep 03 00:06:52.805181 2018] [core:notice] [pid 5161] AH00094: Command line: '/usr/sbin/apache2'
[Sun Sep 02 13:39:30.728738 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Only variables should be assigned by reference in /home/go/public_html/index.php on line 115
[Sun Sep 02 13:39:30.739232 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Undefined variable: year in /home/go/public_html/calendar.php on line 38
[Sun Sep 02 13:39:30.739253 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Undefined variable: month in /home/go/public_html/calendar.php on line 40
[Sun Sep 02 13:39:30.739256 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Undefined variable: year in /home/go/public_html/calendar.php on line 42
[Sun Sep 02 13:39:30.739260 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Undefined variable: year in /home/go/public_html/calendar.php on line 46
[Sun Sep 02 13:39:30.739278 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Undefined variable: month in /home/go/public_html/calendar.php on line 52
[Sun Sep 02 13:39:30.739281 2018] [:error] [pid 4109] [client 127.0.0.1:36468] PHP Notice:  Undefined variable: month in /home/go/public_html/calendar.php on line 56
[Sun Sep 02 13:39:35.889200 2018] [:error] [pid 4111] [client 127.0.0.1:36478] PHP Notice:  Only variables should be assigned by reference in /home/go/public_html/show-reading-list.php on line 7, referer: http://localhost/~go/

日志中提到的变量都在这些 php 文件中...而且我在更新之前生成的以前的日志中看到了相同的错误。

答案1

php 和 php-curl 现在可以正常工作。

让我找到答案的原因是注意到 /etc/apache2/mods-available/ 中的 php7.0.conf 和 php7.2.conf 之间存在差异,这只会影响 php 在 userdir 中的执行情况。然后我检查了 php 是否在服务器上运行,结果发现它确实运行正常。

总而言之,我认为从 Ubuntu 16.04 和 18.04 更新后,我需要

  • 禁用 php7.0 并启用 php7.2:这会导致用户目录 php 脚本出现问题,但确实修复了服务器下的 php-curl。换句话说,此时 php 和 php-curl 在服务器地址上运行正常;
  • 编辑 php7.2.conf 文件并注释掉该<IfModule mod_userdir.c>部分;
  • 对于那些阅读整个问题的人来说,我没有保留对 php.ini 文件的编辑;
  • 清除了 Firefox 的缓存:更改并没有解决 userdir 问题,直到我执行以下操作:打开菜单->首选项->隐私和安全->历史记录->使用自定义历史记录设置->清除历史记录->设置->历史记录->缓存(所有其他框均未选中)

php 和 php-curl 现在在 userdir 中运行。

感谢每一位留下评论的人。

答案2

[添加此答案仅是为了方便格式化] 在 SSD 上安装全新 Ububtu18.04 后,我一直在努力解决这个问题。 ~/public_html directory 在我的用户帐户中将其用作文档根目录。Apache 未解析任何 PHP 代码。

最后,上面的详细问题帮助我确定,虽然它 php ~/public_html/test.php 正在工作,但只是从浏览器访问 php 文件不起作用。

解决方案:允许 apache2 在用户目录中执行 PHP。(就像上面的答案一样)。查找并编辑 php7.2.conf 文件。在我的例子中,它位于/etc/apache2/mods-enabled/php7.2.conf。需要注释掉以下几行:

# 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> 

重新启动 apache2,现在就可以正常工作了。您可能需要从浏览器缓存中清除该页面。

感谢@tempestcat 希望这能在未来帮助到其他人。

相关内容