如何在 PHP CLI 版本中更改 php.ini 的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10844641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:12:45  来源:igfitidea点击:

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

command-line-interfacephp

提问by Sufendy

The php that run on the webserver and the CLI version is not using the same php.ini file. If I do a command php --ini, it show this

在网络服务器上运行的 php 和 CLI 版本没有使用相同的 php.ini 文件。如果我执行命令php --ini,它会显示这个

Configuration File (php.ini) Path: C:\Windows
Loaded Configuration File:         C:\wamp\bin\php\php5.3.8\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

while my web version uses the php.ini in C:\wamp\bin\apache\Apache2.2.21\bin\php.ini. This is probably very common for people using wamp.

而我的网络版本在C:\wamp\bin\apache\Apache2.2.21\bin\php.ini. 这对于使用 wamp 的人来说可能很常见。

How do I change the Loaded Configuration File to read from C:\wamp\bin\apache\Apache2.2.21\bin\php.iniso I don't have to maintain 2 different php.ini versions?

如何更改加载的配置文件以进行读取,C:\wamp\bin\apache\Apache2.2.21\bin\php.ini以便我不必维护 2 个不同的 php.ini 版本?

采纳答案by Sufendy

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

每个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.

在以下位置搜索 php.ini(按顺序):

  • SAPI 模块特定位置(Apache 2 中的 PHPIniDir 指令,CGI 和 CLI 中的 -c 命令行选项,NSAPI 中的 php_ini 参数,THTTPD 中的 PHP_INI_PATH 环境变量)
  • PHPRC 环境变量。在 PHP 5.2.0 之前,这是在下面提到的注册表项之后检查的。
  • 从 PHP 5.2.0 开始,可以为不同版本的 PHP 设置 php.ini 文件的位置。按顺序检查以下注册表项:
    • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\xyz]、[HKEY_LOCAL_MACHINE\SOFTWARE\PHP\xy] 和 [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x],其中 x、y 和 z 表示 PHP 主要、次要和发行版本。如果这些键中的任何一个中有 IniFilePath 的值,则找到的第一个将用作 php.ini 的位置(仅限 Windows)。
    • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP],IniFilePath 的值(仅限 Windows)。
  • 当前工作目录(CLI 除外)。
  • Web 服务器的目录(对于 SAPI 模块)或 PHP 目录(否则在 Windows 中)。
  • Windows 目录(C:\windows 或 C:\winnt)(对于 Windows),或 --with-config-file-path 编译时选项。

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

对于 CLI,最好的办法可能是为$PHPRC将执行脚本的帐户设置环境变量,或者使用不同的--with-config-file-path配置设置重新编译 PHP 。

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

您还可以通过-c在调用 PHP 时指定选项来在每次执行的基础上覆盖 php.ini 搜索目录:

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

回答by latvian

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

通过指定环境变量 PHPRC 指向与 Apache 在 Ubuntu 上为我工作的同一个 php.ini

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

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

或者把它放在$HOME/.bashrc 每次启动cli时导入

回答by Andrew

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...

我花了大约 3 个小时试图完全按照您的问题进行操作。做不到。我放弃并将该 php.ini 复制到 C:\Windows 并将我的 ENV 变量“PHPRC”更改为 C:\Windows(以及在 httpd.conf 中)。这样我的 CLI php 和 Apache php e 都使用相同的文件。在我进行此更改之前,我无法让“php -m”显示我在我的 php.ini 中加载的模块。

回答by Benson

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.

我尝试使用 C:\YourPathto\PHP7\ 作为我的 PHPRC 环境变量,但失败了。但它必须设置为带有文件名的完整路径。我还发现我必须完全重新启动 php 才能获取新变量。

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

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

    You should see SUCCESS: Specified value was saved.

  2. Then restart your computer (really).

  1. 在 Windows 上,找到 php.ini 文件的路径,然后在命令/终端窗口中修改并运行以下命令:

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

    你应该看到 SUCCESS: Specified value was saved.

  2. 然后重启你的电脑(真的)。

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

或者通过常规方式编辑环境变量,然后重启:

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

PHPRC 环境变量设置为 C:\YourPathto\PHP7\php.ini

回答by maxHymanie

try running the phpinfo()function on both side

尝试phpinfo()在双方运行该功能

Webserver and CLI and find out the exact location of your php.inifile for both side

网络服务器和 CLI 并找出php.ini双方文件的确切位置

and maintain those files only

并仅维护这些文件