How do I include a php.ini file in another php.ini file?
-
2I'm interested how RHEL/Fedora accomplishes the /etc/php.d/* include. Did they patch PHP to make this possible?– gahooaSep 8, 2009 at 4:17
-
@gahooa that's the question that I had leading me here. Did you ever learn how they accomplish the /etc/php.d/* include?– Jeff PuckettSep 7, 2016 at 1:50
-
1see the accepted answer below, this answers your question too. U can also do php -i (phpinfo() ) and see how the php was built– Itay Moav -MalimovkaSep 7, 2016 at 1:58
7 Answers
I don't think you can "include" .ini files from the main php.ini file.
One possible solution, though, might be to use this option on the configure line, when compiling 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.
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.
The path to the php.ini file is being defined with this option, on the configure line:
--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.
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
-
In addition to compile time setting, there is also an environment variable
PHPRC
which makes possible to change defaultphp.ini
(and possible other ini files) directory. Jul 29, 2011 at 17:19 -
1When you say on the configure line, what does that mean? Is that a file somewhere?– tomjungMar 12, 2015 at 21:37
-
Note that Debian-packaged PHP builds are configured to scan a web-server-specific directory at startup for additional config files. Any
*.ini
file found in the directory is treated as if it were part ofphp.ini
. It is likely Debian-derived distros such as Ubuntu also do this. Example directory:/etc/php/7.4/apache2/conf.d/
Reference:/usr/share/doc/php7.4-common/README.Debian.gz
May 2, 2022 at 17:33
One can also define the path in ~/.bashrc
export PHP_INI_SCAN_DIR=/usr/local/lib/php.d
-
1
I installed Memcached for php and wasn't sure how to make sure that its ini was included in my php.ini file, but as it turns out, it automatically is. You can validate what is being loaded by running 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
-
1it sounds like config file path returned by "php --ini" maybe not the same as returned from web page (<?php phpinfo(); ?>) .– gonglongApr 17, 2017 at 3:52
EDIT: My answer was mistaken. This only works in .conf
files, which is not the question asked. Better testing showed that it won't work in php.ini
files, where include
statement is ignored.
I just tested it on Debian 9 (Stretch) with PHP-FPM. From some .conf file, use this syntax:
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 development and another one for production. Then you could do it the Debian/Apache style like
/etc/php/conf-available/
/etc/php/conf-enabled/
with symliks from the second to the other and an include to that one.
-
2
-
1This does not apply to
php.ini
, which is what the question is about. May 2, 2022 at 17:29
It 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.
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.
There you could do:
export PHP_INI_SCAN_DIR=/etc/php5/cgi/conf.d:/var/www/mydomain.net/etc
/var/www/mydomain.net/etc
is just an example. It's the folder where you put your additional ini files into. It seems this can be a :
separated list.
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.
/etc/php5/cgi/conf.d
should always be included (I guess because it was compiled into the PHP executable) and possibly not really be needed.
You can't. Read online pages:
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.
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.
-
4I read those before posting, says nothing about including whether it is possible or not. Sep 8, 2009 at 3:53
You could try to simulate it making use of the ini_set function. But as the "man page" indicates, not all ini options can be changed using ini_set. It's definitely a useful function, though.