如何使用 MAMP 从命令行运行 PHP 脚本?

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

How to run a PHP script from the command line with MAMP?

macosmampphp

提问by Andrew

I have MAMP installed. Now I am trying to run a script from the command line, but I can't seem to get it to work.

我安装了 MAMP。现在我试图从命令行运行脚本,但我似乎无法让它工作。

How should I set up my environment so that I can run a script from the command line and use the PHP version I installed with MAMP?

我应该如何设置我的环境,以便我可以从命令行运行脚本并使用我随 MAMP 安装的 PHP 版本?

Update:I agree with jjeaton below, here is a nice solutionof creating an alias to MAMP's PHP:

更新:我同意下面的 jjeaton,这是为 MAMP 的 PHP 创建别名的一个很好的解决方案

# add this to your ~/.bash_profile
alias phpmamp='/Applications/MAMP/bin/php/php5.3.6/bin/php'

Now you can use it from the command line:

现在您可以从命令行使用它:

$ phpmamp --help

采纳答案by pdeli

Please note that with version 2.0.5 of MAMP, the path has changed. It is now one of the following:

请注意,在 MAMP 2.0.5 版中,路径已更改。它现在是以下之一:

/Applications/MAMP/bin/php/php5.2.17/bin/
/Applications/MAMP/bin/php/php5.3.6/bin/

Therefore the command to add MAMP's php command should probably look like this:

因此,添加 MAMP 的 php 命令的命令应该如下所示:

export PATH=/Applications/MAMP/bin/php/php5.2.17/bin/:$PATH

or like this (depending on which version of PHP you want to use):

或像这样(取决于您要使用的 PHP 版本):

export PATH=/Applications/MAMP/bin/php/php5.3.6/bin/:$PATH

回答by sstringer

Another way that works that may be a little cleaner with regard to PHP versions is to create an alias in your bash profile that points to the specific php binary that you want to run when you run things like composer or other cli tools. This has the benefit of avoiding some potential library and php.ini config compatibility issues with the installed version of php in OSX.

另一种可能对 PHP 版本更清晰的方法是在 bash 配置文件中创建一个别名,该别名指向您在运行 Composer 或其他 cli 工具之类的东西时要运行的特定 php 二进制文件。这有利于避免一些潜在的库和 php.ini 配置与 OSX 中安装的 php 版本的兼容性问题。

For instance, if you want to point to php 5.4.1 in MAMP, edit your .bash_profile file in your editor of choice (nano, vi, etc.):

例如,如果您想在 MAMP 中指向 php 5.4.1,请在您选择的编辑器(nano、vi 等)中编辑您的 .bash_profile 文件:

# nano ~/.bash_profile

Add this below your PATH statement:

在你的 PATH 语句下面添加这个:

alias php=/Applications/MAMP/bin/php/php5.4.10/bin/php

Save and quit (CTRL+X in nano, :wq in vi). Quit Terminal. The next time you try to call php from the cli, you'll be using the 5.4.10 version installed with MAMP.

保存并退出(nano 中的 CTRL+X,vi 中的 :wq)。退出终端。下次尝试从 cli 调用 php 时,您将使用随 MAMP 安装的 5.4.10 版本。

Remember to update this path if you update MAMP with a more recent version of PHP.

如果您使用更新版本的 PHP 更新 MAMP,请记住更新此路径。

回答by phidah

Run this in your Terminal:

在你的终端中运行这个:

export PATH=/Applications/MAMP/bin/php5/bin/:$PATH

Should do the trick. It will - as Tom Haigh mentioned - add the MAMP PHP executable to the path so you can use "php" instead of the full path.

应该做的伎俩。它将 - 正如 Tom Haigh 提到的 - 将 MAMP PHP 可执行文件添加到路径中,以便您可以使用“php”而不是完整路径。

回答by Tom Haigh

Yes, I think it is here: /Applications/MAMP/bin/php5/bin/php

是的,我认为它在这里: /Applications/MAMP/bin/php5/bin/php

You can either add /Applications/MAMP/bin/php5/bin/to the front of your path or create a symlink in /usr/bin(there probably is one there already for the default PHP installation)

您可以添加/Applications/MAMP/bin/php5/bin/到路径的前面或创建一个符号链接 /usr/bin(可能已经有一个用于默认 PHP 安装)