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

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

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.

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?

Update: I agree with jjeaton below, here is a nice solution of creating an alias to MAMP's 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

回答1:

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

/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:

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

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

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


回答2:

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.



回答3:

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.

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

# nano ~/.bash_profile

Add this below your PATH statement:

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.

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



回答4:

Yes, I think it is here: /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)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!