【问题标题】:PHP-FPM requires restart to access generated file when visiting webpagePHP-FPM 访问网页时需要重启才能访问生成的文件
【发布时间】:2017-10-27 21:07:07
【问题描述】:

作为我从事的基于 Web 的项目的一部分,我们生成新的“站点”,通过将不同的参数传递给 $_GET 来访问这些“站点”。所有网站都位于/usr/local/sites。我们使用的用户(hwphpwww-data 仍然是默认的 apache 用户)不拥有任何站点(用户或组),但用户在组中,因此可以读取目录中的所有文件。

我有一个生成站点的 python 脚本,在运行它之后,有一半的时间我尝试通过转到 http://localhost/index.php?site=newsite 访问它,我得到 File not found: /var/local/sites/newsite/config/config.ini 虽然运行 PHP CLI 它可以找到文件和 hwphp 用户可以很好地阅读它,只是当我通过 PHP-FPM/Apache 时它失败了。

这是我的池配置:

; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[submitty]

user = hwphp
group = hwphp

listen = /run/php/php7.0-fpm-submitty.sock

listen.owner = www-data
listen.group = www-data
;listen.mode = 0660

pm = dynamic

pm.max_children = 5

pm.start_servers = 2

pm.min_spare_servers = 1

pm.max_spare_servers = 3

和我的 apache 配置:

<VirtualHost 192.168.56.101:80>
    AddDefaultCharset utf-8
    ServerAdmin ADMIN@DOMAIN.HERE
    ServerName 192.168.56.101

    DocumentRoot /usr/local/submitty/site/public
    DirectoryIndex index.html index.php index.htm index.cgi

    SuexecUserGroup hwphp hwphp

    <IfModule mod_fastcgi.c>
        AddHandler php7-fcgi .php
        Action php7-fcgi /php7-fcgi
        Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
        FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm-submitty.sock -pass-header Authorization

        <FilesMatch ".+\.ph(p[345]?|t|tml)$">
            SetHandler php7-fcgi
        </FilesMatch>

        <Directory /usr/lib/cgi-bin>
            Require all granted
        </Directory>
    </IfModule>

    <Files .*>
        Order allow,deny
        Deny from all
    </Files>

    <Files *~>
        Order allow,deny
        Deny from all
    </Files>

    <Files #*>
        Order allow,deny
        Deny from all
    </Files>

    <Directory />
        AllowOverride None
    </Directory>

    <Directory /usr/local/submitty/site/public>
        Require all granted
        Order allow,deny
        Allow from all
    </Directory>

    LogLevel error
    ErrorLog ${APACHE_LOG_DIR}/submitty.log
    CustomLog ${APACHE_LOG_DIR}/submitty.log combined
</VirtualHost>

只要我这样做:

service php7.0-fpm restart

它工作正常并且找到了文件。不知道为什么会这样,因为它没有任何意义(以及为什么简单的重启可以解决它)。

服务器是 Ubuntu 16.04,使用 PHP 7 和 Apache 2.4,一切都来自股票 apt-get 安装。

【问题讨论】:

  • 当您说“一半时间”时——您的意思是这是间歇性的吗?或者您可以通过使用唯一的“newsite”名称随意重现此问题,该名称通过重新启动来解决?你到底在哪里得到错误?你能显示处理$_GET['site']参数的控制器的代码吗?
  • 是的,它并不总是发生。但是,当我访问一个不存在的站点时,它似乎确实会发生,得到一个关于它不存在的错误,然后创建它,然后尝试去那个站点,我得到一个错误。正在捕获的相关 PHP 代码只是一个 if (!file_exists($ini_file)) 块,其中 $ini_file = /var/local/sites/newsite/config/config.ini。但是,重新启动 php7.0-fpm 并且一切都按预期工作。这些错误也仅在从 14.04 到 16.04 时从 suPHP 切换到 PHP-FPM 后开始。
  • 我对正在运行的用户也很有信心,因为我在index.php 的顶部放置了一个print(exec('whoami'));,它会打印出hwphp,它可以访问上述config.ini文件(并使用 PHP CLI,file_exists($ini_file) 按预期返回 true。
  • 可能是 ZendOpCache 或 APC 之类的缓存有问题?
  • 是的,这是 opcache 的问题,我没想到因为它只讨论 *.php 文件,但猜测它缓存了通过 file_exists() 的任何内容。谢谢!

标签: php apache php-7


【解决方案1】:

问题最终是 PHP 正在对文件读取进行一些缓存,这导致尝试打开文件时出现问题。重新启动 php-fpm 会刷新缓存并使一切正常工作。

【讨论】:

    猜你喜欢
    • 2011-08-29
    • 2017-09-13
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多