如何正确设置 PHP 环境变量以在 Git Bash 中运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17727436/
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-25 16:20:30  来源:igfitidea点击:

How to properly set PHP environment variable to run commands in Git Bash

phpwindowsenvironment-variablesgit-bash

提问by ohiock

There are a few similar questions as this throughout the site, but none of them are giving me the answer I'm looking for.

整个网站都有一些类似的问题,但没有一个给我我正在寻找的答案。

What I'm trying to do is install Composer via Git Bash on a Windows machine that has WAMP.

I'm using the following command:

我想要做的是在具有 WAMP 的 Windows 机器上通过 Git Bash 安装 Composer。

我正在使用以下命令:

curl -s http://getcomposer.org/installer | php

This is not working, as 'php' is not recognized. So I looked into the problem and I realized that Windows does not know what 'php' is, and I need to set an environment variable.

这不起作用,因为无法识别“php”。所以我调查了这个问题,我意识到 Windows 不知道 'php' 是什么,我需要设置一个环境变量。

I go into the environment variable dialogue and enter 'php' as the variable and C:\wamp\bin\php\php5.3.8as the value. Is this correct? Should I be targeting a specific file or the directory as a whole?

我进入环境变量对话框并输入“php”作为变量和C:\wamp\bin\php\php5.3.8值。这样对吗?我应该针对特定文件还是整个目录?

After doing this, I try the command again and it fails because it still does not recognize 'php'. I have also tried putting the file path into the command directly, but that didn't work either.

执行此操作后,我再次尝试该命令,但它失败了,因为它仍然无法识别“php”。我也尝试将文件路径直接放入命令中,但这也不起作用。

So I am curious as to what I am doing incorrectly. Is my path incorrect?

所以我很好奇我做错了什么。我的路径不正确吗?

回答by Squig

Adding the path to your PATH variable should fix that.

将路径添加到您的 PATH 变量应该可以解决这个问题。

Right click My Computer, go to advanced settings, click Environment Variables then edit the PATH system variable.

右键单击我的电脑,转到高级设置,单击环境变量,然后编辑 PATH 系统变量。

Add a semi-colon and then the path to your PHP binary, i.e. ";C:\wamp\bin\php\php5.3.8"

添加分号,然后添加 PHP 二进制文件的路径,即“;C:\wamp\bin\php\php5.3.8”

Finally, restart the Git Bash so that it updates the PATH variable.

最后,重新启动 Git Bash 以更新 PATH 变量。

回答by Nick Weavers

If you prefer to have it all in the unixy context of your bash cmd window:

如果您更喜欢在 bash cmd 窗口的 unixy 上下文中使用它:

  1. Open the bash window and you find by default you're in the root directory

    $ pwd
    /
    
  2. change to your user directory

    $ cd ~
    $ pwd
    /c/Users/nickw
    
  3. create a .bash_profile file or append to an existing one (use single quotes or $PATH will get interpolated)

    $ echo 'PATH=$PATH:/i/wamp64/bin/php/php5.6.19' >> .bash_profile
    
  4. check the file has the entry

    $ cat .bash_profile
    PATH=$PATH:/i/wamp64/bin/php/php5.6.19
    
  5. close the bash window and open a new one to check

    $ php --version
    PHP 5.6.19 (cli) (built: Mar  2 2016 20:09:42)
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    
  1. 打开 bash 窗口,您会发现默认情况下您在根目录中

    $ pwd
    /
    
  2. 切换到您的用户目录

    $ cd ~
    $ pwd
    /c/Users/nickw
    
  3. 创建一个 .bash_profile 文件或附加到现有文件(使用单引号或 $PATH 将被插入)

    $ echo 'PATH=$PATH:/i/wamp64/bin/php/php5.6.19' >> .bash_profile
    
  4. 检查文件是否有条目

    $ cat .bash_profile
    PATH=$PATH:/i/wamp64/bin/php/php5.6.19
    
  5. 关闭 bash 窗口并打开一个新窗口进行检查

    $ php --version
    PHP 5.6.19 (cli) (built: Mar  2 2016 20:09:42)
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    

回答by mikeyc7m

If you're in git bash, just type PATH="path to php goes here"

如果你在 git bash 中,只需输入 PATH="path to php goes here"

It might be useful to copy the existing path and modify it, so you don't lose other useful paths. Type in exportto see the path.

复制现有路径并修改它可能很有用,这样您就不会丢失其他有用的路径。输入export以查看路径。

The new path is only valid for the session.

新路径仅对会话有效。

回答by Hyder B.

You need to add the PHP directory to your path. On the command line, it would look like this:

您需要将 PHP 目录添加到您的路径中。在命令行上,它看起来像这样:

SET PATH=%PATH%;C:\wamp\bin\php\php5.5

if in doubt, it's the directory containing the php.exe.

如果有疑问,它是包含php.exe.

You can also pre-set the path in Windows' control panel. See hereon how to do this in Windows 7 for example.

您还可以在 Windows 的控制面板中预先设置路径。例如,请参阅此处了解如何在 Windows 7 中执行此操作。

Be aware that if you call the PHP executable from an arbitrary directory, that directory will be the working directory. You may need to adjust your scripts so they use the proper directories for their file operations (if there are any).

请注意,如果您从任意目录调用 PHP 可执行文件,该目录将是工作目录。您可能需要调整脚本,以便它们使用正确的目录进行文件操作(如果有)。