如何覆盖一些PHP默认配置?

时间:2016-09-23 20:41:00

标签: php linux configuration php-5.5

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

  • 修改默认的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上的值淹没?

3 个答案:

答案 0 :(得分:3)

是的,应该是可能的。首先,在文档根目录中的某个位置创建一个名为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服务器,更改将生效。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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