如何用自定义 .ini 文件覆盖某些 PHP 配置默认值?

How to overwrite some PHP configuration defaults with a custom .ini file?

假设我已经在 Ubuntu 中安装了 PHP 5.5.x 并且它带有默认配置。我想覆盖一些配置,但我不想(这就是我知道这是可能的):

我想创建一个名为 custom-php.ini 的文件,其中包含以下行(作为示例):

; Basic configuration override
expose_php = Off
memory_limit = 512M
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = UTC
max_execution_time = 120

; Error reporting
display_errors = stderr
display_startup_errors = Off
error_reporting = E_ALL

; A bit of performance tuning
realpath_cache_size = 128k

; OpCache tuning
opcache.max_accelerated_files = 32000
; Temporarily disable using HUGE PAGES by OpCache.
; This should improve performance, but requires appropriate OS configuration
; and for now it often results with some weird PHP warning:
; PHP Warning:  Zend OPcache huge_code_pages: madvise(HUGEPAGE) failed: Invalid argument (22) in Unknown on line 0
opcache.huge_code_pages=0

; Xdebug
[Xdebug]
xdebug.remote_enable = true
xdebug.remote_host   = "192.168.3.1" // this IP should be the host IP
xdebug.remote_port   = "9001"
xdebug.idekey        = "XDEBUG_PHPSTORM"

有什么地方可以让我写这个文件并且默认 PHP 值会被 custom-php.ini 上的值覆盖吗?

如果您不使用 Apache 而使用 CGI/FastCGI SAPI,则可以使用 .user.ini 文件,如下所示:

upload_max_filesize     = "300M" 
post_max_size           = "100M"

是的,应该可以。首先,在文档根目录的某处创建一个名为 info.php 的 PHP 文件,并将 <?php phpinfo() ?> 放入其中。然后从浏览器中调用它并查找 Scan this dir for additional .ini files。在 Ubuntu 上,它可能类似于 /etc/php5/apache2/conf.d

在该目录中,您可以放置​​自己的自定义 ini 文件(将其命名为 99.overrides.php.ini)以便最后解析。

在那个文件中,添加你想要的任何额外配置,重新启动 Apache 或网络服务器,更改就会生效。

同样有趣的是: https://www.howtoforge.com/how-to-specify-a-custom-php.ini-for-a-website-apache2-with-mod_php

显然您可以通过 PHPINIDir 指令设置自定义 php.ini 文件。您也可以指定每个虚拟主机。

<VirtualHost 1.2.3.4:80> [...] PHPINIDir /var/www/web1 [...] </VirtualHost>

但仅适用于 Apache。