【问题标题】:Why does it use PHP5 instead of PHP7 for my script为什么我的脚本使用 PHP5 而不是 PHP7
【发布时间】:2017-02-05 03:00:32
【问题描述】:

我看了十几十个线程,但无法找出问题所在。我需要在一台机器上安装 php5 和 php7,并希望文件夹中的脚本由 php7 解释。找不到配置的错误,请看一下

Apache:Apache/2.4.23 (Debian)

操作系统:Debian 测试

两个 PHP 版本均已安装:

/var/www/test# ls -l /usr/bin/php*

lrwxrwxrwx 1 root root      21 Okt  6  2015 /usr/bin/php -> /etc/alternatives/php
-rwxr-xr-x 1 root root 9065192 Jul 26 10:33 /usr/bin/php5
-rwxr-xr-x 1 root root 4422704 Sep 18 10:38 /usr/bin/php7.0
lrwxrwxrwx 1 root root      28 Okt  6  2015 /usr/bin/php-config -> /etc/alternatives/php-config
-rwxr-xr-x 1 root root    5237 Jul 26 10:33 /usr/bin/php-config5
lrwxrwxrwx 1 root root      24 Okt  6  2015 /usr/bin/phpize -> /etc/alternatives/phpize
-rwxr-xr-x 1 root root    4730 Jul 26 10:33 /usr/bin/phpize5

为这个文件夹设置了正确的 AddHandler

nano /var/www/test/.htaccess

AddHandler application/x-httpd-php7 .php

并且设置了 AllowOverwrite

nano /etc/apache2/apache2.conf

...
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow 
# access here, or in any related virtual host.
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
...

而php7应该是默认的anway

update-alternatives php

  Auswahl      Pfad             Priorität Status
------------------------------------------------------------
* 0            /usr/bin/php7.0   70        automatischer Modus
  1            /usr/bin/php5     50        manueller Modus
  2            /usr/bin/php7.0   70        manueller Modus

在命令行中有效

php -v

PHP 7.0.11-1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.11-1, Copyright (c) 1999-2016, by Zend Technologies

但不在服务器上下文中!

w3m http://localhost/test/info.php

PHP Version 5.6.24-0+deb8u1

System           Linux ber-eagle02vm 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64
Build Date       Jul 26 2016 08:17:13
Server API       Apache 2.0 Handler
Virtual
Directory        disabled
Support
Configuration
File (php.ini)   /etc/php5/apache2
Path
Loaded
Configuration    /etc/php5/apache2/php.ini
File
Scan this dir
for additional   /etc/php5/apache2/conf.d
...

有什么想法吗?

【问题讨论】:

  • apache配置中是否存在/mods-enabled/php7.conf和/mods-enabled/php7.load?
  • 那是错误的来源,谢谢!不过,我一次只能启用一个模组,这是一个问题(见下文),但至少我现在知道缺少了什么部分。

标签: php apache


【解决方案1】:

看起来你正在使用mod_php (Server API =&gt; Apache 2.0 Handler),这意味着 php 作为一个模块嵌入到 apache 中。要回答这个问题,据我所知,您不能同时加载多个 mod_php,这就是为什么通过网络服务器提供的 php 始终是版本 5。

当您在命令行上使用 php 时,它与网络服务器无关,它根据您的 update-alternatives 使用 php7。 php 命令转到 /usr/bin/php7

一种方法是将 cgi (fastcgi, php-fpm) 用于不同的 php 版本。我还阅读了有关通过使用不同的虚拟主机进行设置的信息,但我自己从未尝试过,我猜这将是另一个问题。 希望对您有所帮助!

【讨论】:

  • 是的,多亏了 andre 的评论,我在同一时刻发现了同样的情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多