Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the path to php.ini in PHP CLI version

Per http://php.net/configuration.file:

php.ini is searched for in these locations (in order):

  • SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
  • The PHPRC environment variable. Before PHP 5.2.0, this was checked after the registry key mentioned below.
  • As of PHP 5.2.0, the location of the php.ini file can be set for different versions of PHP. The following registry keys are examined in order:
    • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z], [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x], where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
    • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP], value of IniFilePath (Windows only).
  • Current working directory (except CLI).
  • The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows).
  • Windows directory (C:\windows or C:\winnt) (for Windows), or --with-config-file-path compile time option.

For CLI, your best bet is probably either to set the $PHPRC environment variable for the account that will be executing scripts, or recompile PHP with a different --with-config-file-path configuration setting.

You can also override the php.ini search dir on a per-execution basis by specifying the -c option when invoking PHP:

> php --help
Usage: php [options] [-f]  [--] [args...]
...
  -c | Look for php.ini file in this directory


By specifying environment variable PHPRC to point to the same php.ini as the one Apache uses worked for me on Ubuntu

export PHPRC=/etc/php5/apache2/php.ini

Or put it in the $HOME/.bashrc to import every time cli is started


I have spent about 3 hours trying to do exactly what your question asks. Couldn't do it. I gave up and copied that php.ini to C:\Windows and changed my ENV variable "PHPRC" to C:\Windows (and in httpd.conf). That way both my CLI php and Apach php e are using the same file. I could not get "php -m" to display the modules I had loaded in my php.ini for the life of me until I made this change...


I tried and failed with using C:\YourPathto\PHP7\ as my PHPRC Environmental Variable. But it must be set to the full path with filename. I also found that I had to do a full restart for php to pick up the new variable.

  1. On Windows, find the path to your php.ini file and modify and run the following command in a command window:

    setx PHPRC "C:\YourPathto\PHP7\php.ini"

    You should see SUCCESS: Specified value was saved.

  2. Then restart your computer (really).

Or edit environmental variables through the regular way, then restart:

PHPRC Environmental Variable Set to C:\YourPathto\PHP7\php.ini