如何在 OSX 上使用 MAMP 的 PHP 版本而不是默认版本
我想使用 MAMP 版本的 PHP,而不是我的 mac 上默认安装的版本。我尝试使用
ln -s /Applications/MAMP/bin/php5.3/bin/php php
但出现“文件存在”错误。解决这个问题的最佳方法是什么,这样我就可以输入 php 而不是完整路径?
I would like to use MAMP's version of PHP instead of the default installed on my mac. I tried using
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我在原始 php 位置创建了一个符号链接。
1.使用以下命令找到您的 osx php 版本:
结果应该是:
2.备份(移动)您的原始 php 二进制文件:
3.创建符号链接:
4.运行您的新 php 版本:
PS:
为了使其在 El-Capitan 上运行
csrutil禁用
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
I have created a symlink at the original php location.
1. Locate your osx php version with:
The result should be:
2. Backup (move) your original php binary:
3. Create the symlink:
4. Run your new php version:
PS:
In order for this to work on El-Capitan
csrutil disable
sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /opt/local/bin/php
or
sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /usr/bin/php
csrutil enable
我不建议尝试修改在命令行上调用的默认 PHP 版本。如果您决定升级操作系统,这样做可能会破坏系统的其他部分,并在将来给您带来问题。
有一个替代方案可以满足您的需求。您可以为 MAMP 的 php 5.3 副本创建别名。就我而言,我将别名命名为 phpmamp。打开终端并输入:
现在,在命令行输入 phpmamp 将启动 MAMP php 解释器。通过键入以下内容来验证这一点:
您很可能希望将其以及任何其他别名存储在
~/.bash_profile
中。这将允许别名在重新启动后保持不变。否则,别名应该仅适用于您所在的特定终端会话。有关创建.bash_profile
文件的更多信息可以在此处找到:http://www.redfinsolutions.com/redfin-blog/creating-bashprofile-your-mac
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.
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:
Now, typing phpmamp at the command line will launch the MAMP php interperter. Verify this by typing:
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:http://www.redfinsolutions.com/redfin-blog/creating-bashprofile-your-mac
我不想篡改当前文件,因此我只是将 MAMP PHP bin 文件夹添加到
$PATH
环境变量中。您可以编辑
~/.bash_profile
并将以下行添加到顶部export PATH="/Applications/MAMP/bin/php/php5.6.1/bin:$PATH"
只需将 PHP 版本更改为您当前使用的版本即可。
编辑文件后,不要忘记执行
source ~/.bash_profile
。I prefer not to tamper with the current files, so I just prepend the MAMP PHP bin folder to the
$PATH
env variable.You can edit
~/.bash_profile
and add the the following line to the topexport PATH="/Applications/MAMP/bin/php/php5.6.1/bin:$PATH"
Just change the PHP version to the current version you are using.
Don't forget to do
source ~/.bash_profile
after you edit the file.2024 - 对于那些使用 ohmyzsh 的用户,要编辑的文件是:
因此,您可以编辑此文件并添加路径:
将“php8.2”替换为 MAMP 中的 php 版本。
2024 - For those using ohmyzsh, the file to edit is:
so, you can edit this file and add the path:
replace 'php8.2' with you php version in your MAMP.
我对迄今为止在网上找到的结果/解决方案并不满意,因为 php.ini 配置在所有情况下和所有系统上都没有正确加载,特别是当您需要 ioncube 等模块时(它是在 MAMP Pro 上更令人困惑)。这就是为什么我创建了自己的 php 版本别名(带有配置),因此我提出了以下解决方案,作为示例(基于 MAMP Pro,请记住根据您的需要调整 php.ini 路径)
: .bash_profile
并添加以下条目:
在当前终端会话中重新初始化 .bash_profile(否则您将看不到任何更改,除非重新启动终端):
如果安装了一些附加模块,则可以使用以下命令对其进行测试php56 -v 你应该得到 ioncube 等模块的输出。否则使用 php56 -i | 进行测试grep "yourModuleNameOrSomethingElse"
现在,您可以在加载了所有配置的终端中轻松使用 PHP 版本之一,例如“php56”。因此,它非常适合通过所有版本迭代来测试和构建应用程序,包括通过终端进行正确的 php.ini 配置。
对于普通的 MAMP 用户,我认为配置应该位于 /Applications/MAMP/conf/ 中。快乐的编程。
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):
Edit your .bash_profile
And add the following entries:
Re-Initialize the .bash_profile in the current terminal session (otherwise you won't see any changes, unless you restart the terminal):
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"
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.
For normal MAMP Users, the configs should be located in /Applications/MAMP/conf/ I think. Happy programming.
最新版本的 MAMP(版本 5+)提供了一种使 MAMP PHP 版本可用于命令行的简单方法。只需在侧边栏菜单中选择“PHP”并选中“使此版本在命令行上可用”即可。简单易行! :)
屏幕截图
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! :)
screenshot
如果您的终端使用 zsh (oh-my-zosh),如附件所示。
检查图片
执行以下操作。
Mac Big Sur 使用“zsh”Oh-my-zosh 作为终端。所以,我做了以下事情。
导出 MAMP_PHP=/Applications/MAMP/bin/php/php7.4.12/bin
导出 PATH="$MAMP_PHP:$PATH"
如果您需要帮助,请告诉我。
If your terminal is using zsh (oh-my-zosh) as shown in the attachment.
check image
Do the following.
Mac Big Sur uses "zsh" Oh-my-zosh for the terminal. so, I did the following.
export MAMP_PHP=/Applications/MAMP/bin/php/php7.4.12/bin
export PATH="$MAMP_PHP:$PATH"
let me know if you need help.
适用于 Mac 操作系统 Catalina。找到目录
/Users//.zprofile
并在重新启动后添加(例如)
,在终端中添加
新的 php 版本
/Applications/MAMP/bin/php/php7.4.2/bin /php
For Mac OS Catalina. Find directory
/Users/<user_name>/.zprofile
and add (for example)
after reboot, in terminal
new php version
/Applications/MAMP/bin/php/php7.4.2/bin/php
好吧,“文件存在”错误可能是因为您尝试使用已存在的文件名创建符号链接。我假设您位于包含您尝试替换的 php 版本的目录中,或者这是第二次尝试,并且您没有首先删除现有的符号链接。我同意其他人关于不“替换/修改”php 的默认版本的观点。
根据问题的第二部分,避免输入完整路径的最佳方法,建议别名的答案是正确的。但是,当涉及多个版本时,这意味着必须调用 php 以外的其他内容来运行您想要运行的版本。
我有一个脚本,可以让我“选择”我想要使用的 php 版本,然后创建到该版本的符号链接,并让我在想要使用它时只需输入“php”作为我的命令。我在这里写了一篇关于它的博客,您可以在其中获取脚本。根据 @ioCron 给出的答案,我可能需要重新访问我的脚本以考虑与每个版本关联的不同配置文件夹。
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.
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.
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 here where 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.
好吧,这些都不适用于我的 OSX10.12.5
我在 /opt/local/bin 安装了 mac 端口 php70,
其中 php 显示:
我设置了别名和本地路径等,这对我来说大部分工作,但其他程序失败(就像作曲家一样)所以我的解决方案是在
文件 /etc/paths 前面
加上:然后这一切都很有魅力!
Well none of this was working for me with OSX10.12.5
i have mac ports php70 installed at /opt/local/bin
which php showed:
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:
to the file /etc/paths
then it all worked a charm!