php 如何覆盖PHP的路径以使用MAMP路径?

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

How to override the path of PHP to use the MAMP path?

phpmacosshellpathmamp

提问by sf_tristanb

After screwing up entirely my PHP configuration on MAC trying to get the SOAP module working (-bash: /usr/bin/php: No such file or directory ....) I now have to use MAMP but each time I have to type the path

在完全搞砸了我在 MAC 上的 PHP 配置后,试图让 SOAP 模块正常工作(-bash: /usr/bin/php: No such file or directory ....)我现在必须使用 MAMP 但每次我必须输入路径

Applications/MAMP/bin/php5.3/bin/php to do command line.

How to just type php instead the entire path on MAC ?I double checked and i do not have a file named .profilenor bash_profile

如何只输入 php 而不是 MAC 上的整个路径?我仔细检查过,我没有一个名为的文件,.profile也没有bash_profile

Thanks

谢谢

PS: Here's what output echo $PATH :

PS:这是输出 echo $PATH :

echo $PATH
/Applications/MAMP/Library/bin/:/Applications/MAMP/bin/php5/bin/:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin

采纳答案by Ricardo Martins

Everytime you save MAMP config (PHP section), it saves the current version of PHP on ~/.profilefile and creates the alias for php, pear and pecl, to point to the current configured version. (Note: you need to check "Make this version available on the command line" option in MAMP)

每次保存 MAMP 配置(PHP 部分)时,它都会在~/.profile文件中保存当前版本的 PHP,并为 php、pear 和 pecl 创建别名,以指向当前配置的版本。(注意:您需要在 MAMP 中选中“使此版本在命令行上可用”选项)

However, you need to refresh your terminal (open another session) to get this file refreshed. You can also type source ~/.profileto refesh the aliases manually.

但是,您需要刷新终端(打开另一个会话)才能刷新此文件。您还可以键入source ~/.profile以手动刷新别名。

If you want to extract this curerent version in a PHP_VERSION variable - as commented above - for further use, you can do:

如果您想在 PHP_VERSION 变量中提取此当前版本 - 如上所述 - 以供进一步使用,您可以执行以下操作:

export PHP_VERSION=`grep "alias php" ~/.profile | cut -d"/" -f6 | cut -c4-`

And then you'll have $PHP_VERSION available with the current version of MAMP.

然后您将有 $PHP_VERSION 可用于当前版本的 MAMP。

Finally, if you want to run your php using the current configured version on mamp, you just need to add to your ~/.bash_profilethe following:

最后,如果你想在 mamp 上使用当前配置的版本运行你的 php,你只需要添加到你~/.bash_profile的以下内容:

export PHP_VERSION=`grep "alias php" ~/.profile | cut -d"/" -f6 | cut -c4-`
export PHPRC="/Library/Application Support/appsolute/MAMP PRO/conf/" #point to your php.ini folder to use the same php settings
export PATH=/Applications/MAMP/bin/php/php$PHP_VERSION/bin:$PATH

Now, even script that relies on /usr/bin/env phpwill read the correct version from Mamp config.

现在,即使是依赖的脚本/usr/bin/env php也会从 Mamp 配置中读取正确的版本。

回答by David Yell

In your home folder /Users/Davidfor exmaple, you can create a .bash_profile. In here you can export variables and then add them to your path.

/Users/David例如,在您的主文件夹中,您可以创建一个.bash_profile. 在这里,您可以导出变量,然后将它们添加到您的路径中。

Open up the file to edit it in your favourite editor, I use vim.

打开文件在你最喜欢的编辑器中编辑它,我使用 vim。

Then you can add in your path

然后你可以在你的路径中添加

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

You want your bit ahead of the $PATHas that already includes /usr/binwhich is where the system PHP lives. So the system will always find your MAMP version first.

你想要的你有点超前$PATH的,其中已经包含/usr/bin这是系统PHP生活在那里。所以系统总是会先找到你的 MAMP 版本。

Save this file and then reboot your Terminal and you'll see that you should get your MAMP version.

保存这个文件,然后重启你的终端,你会看到你应该得到你的 MAMP 版本。

To test I use php -vas OSX Lion uses 5.3.10 and my MAMP is using 5.3.6
You can also test using which phpwhich will output the path to your current php executable.

为了测试我使用php -vOSX Lion 使用 5.3.10 而我的 MAMP 使用 5.3.6
您还可以测试 using which phpwhich 将输出当前 php 可执行文件的路径。

回答by Andrew Patton

The fact that the previously accepted answer refers to php 5.3.6, while the current version of MAMP ships with 7.2.1 as the default (as of early 2018), points out that this is not a very sustainable solution. You can make your path update automatically by adding an extra line to your .bash_profileto get the latest version of PHP from /Applications/MAMP/bin/php/and export that to your path. Here's how I do it:

先前接受的答案指的是 php 5.3.6,而当前版本的 MAMP 以 7.2.1 作为默认版本(截至 2018 年初),这一事实指出这不是一个非常可持续的解决方案。您可以通过向您添加额外的行来自动更新您的路径,.bash_profile以从中获取最新版本的 PHP/Applications/MAMP/bin/php/并将其导出到您的路径。这是我的方法:

# Use MAMP version of PHP
PHP_VERSION=`command ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH

(Use source ~/.bash_profileafter making your changes to make sure they take effect.)

source ~/.bash_profile在进行更改后使用以确保它们生效。)

As others have mentioned, you will likely also want to modify your shell to use MAMP's mysql executable, which is located in /Applications/MAMP/Library/bin. However, I do notrecommend exporting that folder, because there are a bunch of other executables there, like libtool, that you probably don't want to be giving priority to over your system installed versions. This issue prevented me from installing a node package recently (libxmljs), as documented here.

正如其他人所提到的,您可能还想修改您的 shell 以使用 MAMP 的 mysql 可执行文件,它位于/Applications/MAMP/Library/bin. 但是,我建议导出该文件夹,因为那里还有一堆其他可执行文件,例如libtool,您可能不希望优先于系统安装的版本。这个问题阻止了我最近安装节点包 (libxmljs),如此处所述

My solution was to define and export mysqland mysqladminas functions:

我的解决方案是定义和导出mysqlmysqladmin作为函数:

# Export MAMP MySQL executables as functions
# Makes them usable from within shell scripts (unlike an alias)
mysql() {
    /Applications/MAMP/Library/bin/mysql "$@"
}
mysqladmin() {
    /Applications/MAMP/Library/bin/mysqladmin "$@"
}
export -f mysql
export -f mysqladmin

I used functions instead of aliases, because aliases don't get passed to child processes, or at least not in the context of a shell script. The only downside I've found is that running which mysqland which mysqladminwill no longer return anything, which is a bummer. If you want to check which mysql is being used and make sure everything is copacetic, use mysql --versioninstead.

我使用函数而不是别名,因为别名不会传递给子进程,或者至少不会在 shell 脚本的上下文中传递。我发现的唯一缺点是运行which mysql并且which mysqladmin不再返回任何东西,这是一个无赖。如果您想检查正在使用哪个 mysql 并确保所有内容都是 copacet ,请mysql --version改用。

Note: @julianromera points out that zsh doesn't support exporting functions, so in that case, you're best off using an alias, like alias mysql='/Applications/MAMP/Library/bin/mysql'. Just be aware that your aliases might not be available from subshells (like when executing a shell script).

注意:@julianromera 指出 zsh 不支持导出函数,因此在这种情况下,您最好使用别名,例如alias mysql='/Applications/MAMP/Library/bin/mysql'. 请注意,您的别名可能无法从子 shell 中使用(例如在执行 shell 脚本时)。

回答by Opentuned

I found that on Mavericks 10.8 there wasn't a .bash_profile and my paths were located in /etc/paths

我发现在 Mavericks 10.8 上没有 .bash_profile 并且我的路径位于 /etc/paths

In order to have the new path (whether this is a mamp or brew install of php) take effect it needs to be above the default /usr/bin/php in this paths file. eg.

为了使新路径(无论这是 php 的 mamp 安装还是 brew 安装)生效,它需要高于此路径文件中的默认 /usr/bin/php。例如。

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

AFter the change, open a new terminal window and run 'which php' that should now point to your updated path

更改后,打开一个新的终端窗口并运行现在应该指向更新路径的“which php”

回答by whatsnewsisyphus

you might still run into mysql binary not being found in that manner

您可能仍然会遇到无法以这种方式找到的 mysql 二进制文件

open terminal, type
touch ~/.bash_profile; open ~/.bash_profile

edit as follows below, save, quite and restart terminal or alternately

如下编辑,保存,关闭并重新启动终端或交替

source ~/.bash_profile

to execute new PATH without restarting terminal

在不重启终端的情况下执行新的 PATH

and in the fashion of the DavidYell's post above, also add the following. You can stack various variables by exporting them followed by a single PATH export which I demonstrated below

并按照上面 DavidYell 的帖子的方式,还添加以下内容。您可以通过导出各种变量来堆叠各种变量,然后是我在下面演示的单个 PATH 导出

export MAMP_PHP=/Applications/MAMP/bin/php/php5.6.2/bin
export MAMP_BINS=/Applications/MAMP/Library/bin
export USERBINS=~/bins
export PATH="$USERBINS:$MAMP_PHP:$MAMP_BINS:$PATH"

cheers

干杯

回答by Kissaki

If you have to type

如果你必须输入

/Applications/MAMP/bin/php5.3/bin/php

in your command line then add

在您的命令行中然后添加

/Applications/MAMP/bin/php5.3/bin

to your PATH to be able to call php from anywhere.

到您的 PATH 以便能够从任何地方调用 php。

回答by rckd

This one worked for me:

这个对我有用:

sudo mv /usr/bin/php /usr/bin/~php
sudo ln -s /Application/XAMPP/xamppfiles/bin/php /usb/bin/php

回答by Lexsoul

For XAMPP users you can use this:

对于 XAMPP 用户,您可以使用它:

# Use XAMPP version of PHP
export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
source ~/.bash_profile

And you can check it with:

你可以检查它:

php -v

回答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! See attached screenshot:)

最新版本的 MAMP(版本 5+)提供了一种使 MAMP PHP 版本可用于命令行的简单方法。只需在侧栏菜单中选择“PHP”并选中“在命令行上提供此版本”。十分简单!见附件截图:)

screenshot

截屏

回答by Bhaskar Pramanik

Probably too late to comment but here's what I did when I ran into issues with setting php PATH for my XAMPP installation on Mac OSX

评论可能为时已晚,但这是我在 Mac OSX 上为 XAMPP 安装设置 php PATH 遇到问题时所做的

  1. Open up the file .bash_profile (found under current user folder) using the available text editor.
  2. Add the pathas below:
  1. 使用可用的文本编辑器打开文件 .bash_profile(在当前用户文件夹下找到)。
  2. 添加路径如下:

export PATH=/path/to/your/php/installation/bin:leave/rest/of/the/stuff/untouched/:$PATH

导出路径= /path/to/your/php/installation/bin: leave/rest/of/the/stuff/untouched/:$PATH

  1. Save your .bash_profile and re-start your Mac.
  1. 保存您的 .bash_profile 并重新启动您的 Mac。

Explanation: Terminal / Mac tries to run a search on the PATHSit knows about, in a hope of finding the program, when user initiates a programfrom the "Terminal", hence the trick here is to make the terminal find the php, the user intends to, by pointing it to the user's version of PHP at some bin folder, installed by the user.

说明:终端/ Mac上尝试运行在一个搜索路径下就知道,找到程序,当用户发起的希望节目从“终端”,因此这里的技巧是让终端找到PHP中,用户打算通过将其指向用户安装的某个 bin 文件夹中的用户版本的 PHP。

Worked for me :)

为我工作:)

P.S I'm still a lost sheep around my new Computer ;)

PS 我在我的新电脑周围仍然是一只迷路的羊;)