如何使用 MAMP 的 PHP 版本而不是 OSX 上的默认版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4262006/
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
How to use MAMP's version of PHP instead of the default on OSX
提问by Josh Fraser
I would like to use MAMP's version of PHP instead of the default installed on my mac. I tried using
我想使用 MAMP 的 PHP 版本,而不是我的 mac 上安装的默认版本。我尝试使用
ln -s /Applications/MAMP/bin/php5.3/bin/php php
ln -s /Applications/MAMP/bin/php5.3/bin/php php
but I get a "File exists" error. What's the best way to work around this so I can just type php instead of the full path?
但我收到“文件存在”错误。解决这个问题的最佳方法是什么,这样我就可以输入 php 而不是完整路径?
回答by Heiko K.
I have created a symlink at the original php location.
我在原来的 php 位置创建了一个符号链接。
1. Locate your osx php version with:
1. 使用以下命令找到您的 osx php 版本:
which php
The result should be:
结果应该是:
/opt/local/bin/php
2. Backup (move) your original php binary:
2. 备份(移动)你原来的 php 二进制文件:
sudo mv /opt/local/bin/php /opt/local/bin/php.bak
3. Create the symlink:
3. 创建符号链接:
sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /opt/local/bin/php
4. Run your new php version:
4. 运行你的新 php 版本:
php -v
PS:
PS:
In order for this to work on El-Capitan
为了让它在 El-Capitan 上工作
- Reboot your Mac to RecoveryMode (hold Cmd+R on boot)
- Open Terminal and enter:
csrutil disable
- Reboot
- either :
sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /opt/local/bin/php
orsudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /usr/bin/php
- Reboot again to RecoveryMode and re-enable security:
csrutil enable
- 将 Mac 重新启动到恢复模式(启动时按住 Cmd+R)
- 打开终端并输入:
csrutil disable
- 重启
- 要么:
sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /opt/local/bin/php
要么sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /usr/bin/php
- 再次重新启动到恢复模式并重新启用安全性:
csrutil enable
回答by John Kramlich
I would not recommend trying to modify the default version of PHP that is called on the command line. Doing so may break other parts of your system as well as provide you with problems in the future, should you decide to upgrade your OS.
我不建议尝试修改在命令行上调用的默认 PHP 版本。如果您决定升级操作系统,这样做可能会破坏系统的其他部分,并在将来为您带来问题。
There is an alternative that may meet your needs. You can create an alias to your copy of MAMP's php 5.3. In my case I named the alias phpmamp. Open your terminal and type:
有一种替代方案可以满足您的需求。您可以为 MAMP 的 php 5.3 副本创建别名。就我而言,我将别名命名为 phpmamp。打开你的终端并输入:
alias phpmamp='/Applications/MAMP/bin/php5.3/bin/php'
Now, typing phpmamp at the command line will launch the MAMP php interperter. Verify this by typing:
现在,在命令行输入 phpmamp 将启动 MAMP php interperter。通过键入以下内容验证这一点:
phpmamp --help
You will most likely want to store this, and any other alias, in a ~/.bash_profile
This will allow the aliases to persist across reboots. Otherwise, the alias should only last for the particular terminal session you are in. More information about creating a .bash_profile
file can be found here:
您很可能希望将此以及任何其他别名存储在 a~/.bash_profile
这将允许别名在重新启动后保持不变。否则,别名应该只适用于您所在的特定终端会话。有关创建.bash_profile
文件的更多信息可以在此处找到:
http://www.redfinsolutions.com/redfin-blog/creating-bashprofile-your-mac
http://www.redfinsolutions.com/redfin-blog/creating-bashprofile-your-mac
回答by Ibrahim AshShohail
I prefer not to tamper with the current files, so I just prepend the MAMP PHP bin folder to the $PATH
env variable.
我不想篡改当前文件,所以我只是将 MAMP PHP bin 文件夹添加到$PATH
env 变量中。
You can edit ~/.bash_profile
and add the the following line to the top
您可以编辑~/.bash_profile
以下行并将其添加到顶部
export PATH="/Applications/MAMP/bin/php/php5.6.1/bin:$PATH"
export PATH="/Applications/MAMP/bin/php/php5.6.1/bin:$PATH"
Just change the PHP version to the current version you are using.
只需将 PHP 版本更改为您正在使用的当前版本即可。
Don't forget to do source ~/.bash_profile
after you edit the file.
source ~/.bash_profile
编辑完文件后不要忘记执行此操作。
回答by ioCron
I wasn't pleased with the results / solutions I've found on the net so far, because the php.ini configs weren't loaded properly in all cases and on all systems, espacially when you need modules like ioncube and others (it's even more confusing on MAMP Pro). That's why I've created my own php version aliases (with configs), so I've come up with the following solution, as example (based on MAMP Pro, remember to adjust the php.ini paths to your needs):
到目前为止,我对我在网上找到的结果/解决方案并不满意,因为 php.ini 配置在所有情况下和所有系统上都没有正确加载,尤其是当您需要 ioncube 等模块时(它是在 MAMP Pro 上更令人困惑)。这就是为什么我创建了自己的 php 版本别名(带配置),所以我想出了以下解决方案,作为示例(基于 MAMP Pro,请记住根据您的需要调整 php.ini 路径):
Edit your .bash_profile
编辑您的 .bash_profile
vim ~/.bash_profile
And add the following entries:
并添加以下条目:
alias php55="/Applications/MAMP/bin/php/php5.5.26/bin/php -c '/Library/Application Support/appsolute/MAMP PRO/conf/php5.5.26.ini'"
alias php56="/Applications/MAMP/bin/php/php5.6.10/bin/php -c '/Library/Application Support/appsolute/MAMP PRO/conf/php5.6.10.ini'"
alias php56cgi="/Applications/MAMP/bin/php/php5.6.10/bin/php-cgi -c '/Library/Application Support/appsolute/MAMP PRO/conf/php5.6.10.ini'"
Re-Initialize the .bash_profile in the current terminal session (otherwise you won't see any changes, unless you restart the terminal):
在当前终端会话中重新初始化 .bash_profile (否则您将看不到任何更改,除非您重新启动终端):
source ~/.bash_profile
If you have some additional modules installed, then you can test it with php56 -v and you should get a output of the ioncube, etc. modules. Otherwise test it with php56 -i | grep "yourModuleNameOrSomethingElse"
如果你安装了一些额外的模块,那么你可以用 php56 -v 测试它,你应该得到 ioncube 等模块的输出。否则用 php56 -i | 测试它 grep "你的模块名称或其他东西"
Now you are able to easily use one of the php versions like "php56" in your terminal with all configs loaded. So it's perfect for testing and building your applications through all iterations of versions including the right php.ini configs through the terminal.
现在,您可以在加载了所有配置的终端中轻松使用诸如“php56”之类的 php 版本之一。因此,它非常适合通过所有版本的迭代测试和构建您的应用程序,包括通过终端正确的 php.ini 配置。
For normal MAMP Users, the configs should be located in /Applications/MAMP/conf/ I think. Happy programming.
对于普通 MAMP 用户,我认为配置应该位于 /Applications/MAMP/conf/ 中。快乐编程。
回答by StevenHill
Well, the 'file exists' error is probably because you attempted to create a sym-link with the name of a file that was already there. I assume you were in the directory containing the php version you were trying to replace or that this was a second attempt and you did not first remove the existing sym-link. I agree with the others with regard to not "replacing/modifying" the default version of php.
好吧,“文件存在”错误可能是因为您尝试使用已经存在的文件名创建符号链接。我假设您在包含您尝试替换的 php 版本的目录中,或者这是第二次尝试并且您没有首先删除现有的符号链接。我同意其他人关于不“替换/修改”默认版本的 php。
Based on the second part of the question, the best way to get around having to type the full path, the answers suggesting an alias are right on point with that. When multiple versions are involved though, that means having to call something other than php to run the version you want to run.
基于问题的第二部分,避免输入完整路径的最佳方法,建议别名的答案恰到好处。但是,当涉及多个版本时,这意味着必须调用 php 以外的东西来运行您想要运行的版本。
I have a script that lets me "select" the version of php that I would like to work with which then creates a sym-link to that version and lets me simply enter 'php' as my command when I want to use it. I wrote a blog about it herewhere you can get the script. Based on the answer given by @ioCron I may need to revisit my script to account for the different config folders associated with each version.
我有一个脚本,可以让我“选择”我想要使用的 php 版本,然后创建一个指向该版本的符号链接,并让我在想要使用它时简单地输入“php”作为我的命令。我在这里写了一篇关于它的博客,您可以在其中获取脚本。根据@ioCron 给出的答案,我可能需要重新访问我的脚本以说明与每个版本关联的不同配置文件夹。
回答by pgee70
Well none of this was working for me with OSX10.12.5
好吧,对于 OSX10.12.5,这些都不适合我
i have mac ports php70 installed at /opt/local/bin
我在 /opt/local/bin 安装了 mac 端口 php70
which php showed:
哪个 php 显示:
/usr/bin/php
I set up the aliases and local paths etc, which mostly worked for me, but other programs were failing (like composer) so the solution for me was to prepend:
我设置了别名和本地路径等,这主要对我有用,但其他程序失败(如作曲家),所以我的解决方案是预先添加:
/opt/local/bin
/opt/local/sbin
to the file /etc/paths
到文件 /etc/paths
then it all worked a charm!
那么这一切都很有魅力!
回答by hsaada
The latest version of MAMP (Version 5+) offers an easy way to make the MAMP PHP version available to the command line. Just select "PHP" in the the side bar menu and check "Make this version available on the command line". Easy peasy! :)
最新版本的 MAMP(版本 5+)提供了一种使 MAMP PHP 版本可用于命令行的简单方法。只需在侧栏菜单中选择“PHP”并选中“在命令行上提供此版本”。十分简单!:)