使用 PHP-FPM 时,我可以覆盖 htaccess 中的 PHP 设置吗?

问题描述:

我的虚拟主机看起来像这样:

My vhost looks like this:

<Directory "/var/www">
  Options Indexes FollowSymlinks MultiViews
  AllowOverride All
  Require all granted

  <FilesMatch "\.php$">
    Require all granted
    SetHandler proxy:fcgi://127.0.0.1:9000
  </FilesMatch>
</Directory>

我正在尝试将 php_value 行添加到我的 .htaccess 文件中.一旦我这样做,我就会收到 500 个错误,这在我的 Apache 错误日志中:

I'm trying to add php_value lines to my .htaccess file. As soon as I do, I get 500 errors, and this in my Apache's errors log:

/var/www/.htaccess: 命令 'php_value' 无效,可能拼写错误或由未包含在服务器配置中的模块定义

/var/www/.htaccess: Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration

那么,问题是:在使用 PHP-FPM 时,有没有办法通过 .htaccess 覆盖 php.ini 设置?

So, the question: Is there any way to override php.ini settings via .htaccess, when using PHP-FPM?

tia.

正如其他人已经指出的那样,不,在使用 PHP-FPM 时,您不能通过 .htaccess 覆盖设置.

As others already pointed out, no, you can't override settings via .htaccess when using PHP-FPM.

但 PHP 似乎有类似的机制,名为 .user.ini 文件.

But there seems to be a similar mechanism from PHP, named .user.ini files.

自 PHP 5.3.0 起,PHP 支持基于每个目录的配置 INI 文件.这些文件仅由 CGI/FastCGI SAPI 处理.此功能废弃了 PECL htscanner 扩展.如果您使用的是 Apache,请使用 .htaccess 文件以获得相同的效果.

Since PHP 5.3.0, PHP includes support for configuration INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.

除了主要的 php.ini 文件之外,PHP 还会扫描每个目录中的 INI 文件,从请求的 PHP 文件的目录开始,一直到当前文档根目录(如 $_SERVER['DOCUMENT_ROOT']).如果 PHP 文件位于文档根目录之外,则仅扫描其目录.

In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.

在 .user.ini 样式的 INI 文件中,只有具有 PHP_INI_PERDIR 和 PHP_INI_USER 模式的 INI 设置才会被识别.

Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.

在下面的评论中

如果您使用的是 Apache,请使用 .htaccess 文件以获得相同的效果."

"If you are using Apache, use .htaccess files for the same effect."

澄清一下,这仅适用于 Apache 模块模式.如果您将 php 指令放在 Apache CGI/FastCGI 服务器上的 .htaccess 中,这将导致服务器炸毁 500 错误.

To clarify, this applies only to Apache module mode. If you put php directives in .htaccess on an Apache CGI/FastCGI server, this will bomb the server out with a 500 error.

因此您可以创建类似于 .htaccess 的 .user.ini 文件,但采用 ini 样式格式.

So you can create .user.ini files analogue to .htaccess, but in ini style format.