如何在另一个 php.ini 文件中包含一个 php.ini 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1391808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:22:59  来源:igfitidea点击:

How do I include a php.ini file in another php.ini file?

phpconfiguration

提问by Itay Moav -Malimovka

How do I include a php.ini file in another php.ini file?

如何在另一个 php.ini 文件中包含一个 php.ini 文件?

采纳答案by Pascal MARTIN

I don't think you can "include" .ini files from the main php.ini file.

我认为您不能从主 php.ini 文件中“包含”.ini 文件。

One possible solution, though, might be to use this option on the configure line, when compiling PHP :

但是,一种可能的解决方案可能是在编译 PHP 时在配置行上使用此选项:

--with-config-file-scan-dir=PATH                                                        
    Set the path where to scan for configuration files

If this option is used at compile-time, PHP will look for every .ini file in this directory, in addition to the "normal" php.ini file.

如果在编译时使用此选项,除了“正常”的 php.ini 文件外,PHP 还会查找此目录中的每个 .ini 文件。

I suppose this is what is used by Ubuntu, for instance, which uses a different .ini file for each downloaded extension, instead of modifying php.ini.

我想这就是 Ubuntu 所使用的,例如,它为每个下载的扩展使用不同的 .ini 文件,而不是修改 php.ini。

The path to the php.ini file being defined with this option, on the configure line :

使用此选项定义的 php.ini 文件的路径,在配置行:

--with-config-file-path=PATH                                                            
    Set the path in which to look for php.ini [PREFIX/lib]

Still, it probably means you'll have to re-compile PHP -- which is not that hard, btw -- the hardest part being to get the dependencies you need.

尽管如此,这可能意味着您必须重新编译 PHP —— 这并不难,顺便说一句 —— 最难的部分是获得所需的依赖项。

And, here is a post on the internals@ mailling-list that says the same thing as I do : config files and PHP_CONFIG_FILE_SCAN_DIR

而且,这是 internals@ mailling-list 上的一篇文章,它说的和我做的一样:配置文件和PHP_CONFIG_FILE_SCAN_DIR

回答by Martin Zeitler

one can also define the path in the ~/.bashrc

还可以定义路径 ~/.bashrc

export PHP_INI_SCAN_DIR=/usr/local/lib/php.d

回答by hockey_dave

I installed memcached for php and wasn't sure how to make sure that its ini was included in my php.ini, but as it turns out it automatically is. You can validate what is being loaded by running php --ini.

我为 php 安装了 memcached,但不确定如何确保它的 ini 包含在我的 php.ini 中,但事实证明它是自动的。您可以通过运行验证正在加载的内容php --ini

php --ini
Configuration File (php.ini) Path: /opt/local/etc/php5
Loaded Configuration File:         (none)
Scan for additional .ini files in: /opt/local/var/db/php5
Additional .ini files parsed:      /opt/local/var/db/php5/memcached.ini

回答by ondelettes

Actually YOU CAN. I just tested it on Debian 9 with PHP-FPM. From some .ini/.conf file, use this syntax:

其实你可以。我刚刚在 Debian 9 上使用 PHP-FPM 对其进行了测试。在一些 .ini/.conf 文件中,使用以下语法:

include=/path/to/special-php.ini

or even

甚至

include=/path/to/special-dir-full-of-conf-files/*.conf 

as it is used in

因为它用于

/etc/php/7.0/fpm/php-fpm.conf
include=/etc/php/7.0/fpm/pool.d/*.conf

By the way, this will be most useful if you split your settings by topic, and or if you want a set for dev and another one for production. Then you could do it the Debian/Apache style like

顺便说一下,如果您按主题拆分设置,或者如果您想要一组用于开发而另一组用于生产,这将是最有用的。然后你可以像 Debian/Apache 风格那样做

/etc/php/conf-available/
/etc/php/conf-enabled/

with symliks from the second to the other and an include to that one.

符号从第二个到另一个,并包含到那个。

回答by sjas

Seems you cannot include one ini file into another so it gets referenced and loaded. But you can set php up to load several files by telling it which folders to look into.

似乎您不能将一个 ini 文件包含到另一个文件中,因此它会被引用和加载。但是您可以通过告诉它要查看哪些文件夹来设置 php 以加载多个文件。

When using a FastCGI setup (possibly in FPM, too, though I don't know that for sure) you can export environment variables from within the php wrapper.

当使用 FastCGI 设置时(也可能在 FPM 中,虽然我不确定),您可以从 php 包装器中导出环境变量。

There you could do:

在那里你可以这样做:

export PHP_INI_SCAN_DIR=/etc/php5/cgi/conf.d:/var/www/mydomain.net/etc

/var/www/mydomain.net/etcis just an example, it's the folder where you put your additional ini files into. It seems this can be a :separated list.

/var/www/mydomain.net/etc只是一个例子,它是您将其他 ini 文件放入的文件夹。似乎这可以是一个:单独的列表。

Use a phpinfo.php(file called arbitrarily containing only <?php phpinfo();), open the corresponding URL in your browser and check the list of directories that are parsed and the list of files that get loaded in the top area of it.

使用phpinfo.php(称为任意包含 only 的文件<?php phpinfo();),在浏览器中打开相应的 URL 并检查解析的目录列表以及在其顶部区域加载的文件列表。

/etc/php5/cgi/conf.dshould always be included (I guess because it was compiled into the php executable) and possibly not really be needed.

/etc/php5/cgi/conf.d应该始终包括在内(我猜是因为它被编译到 php 可执行文件中)并且可能不是真的需要。

回答by adatapost

You can't. Read online pages:

你不能。阅读在线页面:

The configuration file

配置文件

SUMMARY: The configuration file (php.ini) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI version, it happens on every invocation.

总结:PHP 启动时读取配置文件(php.ini)。对于 PHP 的服务器模块版本,这只会在 Web 服务器启动时发生一次。对于 CGI 和 CLI 版本,它会在每次调用时发生。

.user.ini files

.user.ini 文件

SUMMARY: 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']). Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.

总结:除了主要的 php.ini 文件之外,PHP 还会扫描每个目录中的 INI 文件,从请求的 PHP 文件的目录开始,一直到当前文档根目录(如 $_SERVER['DOCUMENT_ROOT '])。只有具有 PHP_INI_PERDIR 和 PHP_INI_USER 模式的 INI 设置才能在 .user.ini 样式的 INI 文件中识别。

回答by Stephen

You could try to simulate it making use of the ini_setfunction. But as the "man page" indicates, not all ini options can be changed using ini_set. It's definitely a useful function, though.

您可以尝试使用ini_set函数来模拟它。但正如“手册页”所示,并非所有 ini 选项都可以使用 ini_set 更改。不过,这绝对是一个有用的功能。