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


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

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

  • 编辑默认php.ini文件
  • 在每个.php文件中使用PHP块来覆盖它们
  • 使用.htaccess文件和指令
我想创建一个名为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 ?

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

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

在该文件中,添加任何您想要的额外配置,重新启动Apache或web服务器,更改将生效

如果你不使用Apache,你正在使用CGI/FastCGI SAPI,你可以使用.user.ini文件如下:

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

同样有趣的是: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。