如何让 PHP 在命令行中再次运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10557650/
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 can I get PHP working again in the command line?
提问by MikeTheCoder
I'm completely at loss here and am about to wipe my hard drive clean and start from a fresh OS install. I've been trying for two days to create a new yii app in the terminal and have finally figured out that the terminal or command line can not even execute PHP all of a sudden. I had no problem in past creating an executing php from the command line, But now it's not working. When I type which phpi get nothing. When I type php -vI get:
我在这里完全不知所措,正准备将我的硬盘擦干净并从全新的操作系统安装开始。我已经尝试了两天在终端中创建一个新的 yii 应用程序,最终发现终端或命令行甚至无法突然执行 PHP。我过去从命令行创建一个正在执行的 php 没有问题,但现在它不起作用。当我输入which php 时,我什么也没得到。当我输入php -v 时,我得到:
-bash: php: command not found.
And when I try to create a new yii application I get:
当我尝试创建一个新的 yii 应用程序时,我得到:
env: php: No such file or directory
I am using mac osx-lion and my path looks like this at the moment:
我正在使用 mac osx-lion,目前我的路径如下所示:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin
I've tried looking through the php manual and I'm getting nowhere. How can I reconfigure the command line to execute php? Any help is greatly appreciated.
我试过查看 php 手册,但一无所获。如何重新配置命令行以执行 php?任何帮助是极大的赞赏。
回答by MikeTheCoder
Hopefully this will save someone a lot of headache. If, for whatever reason, you are unable to locate php in your command line, and unable to execute php from the command line, below is a list of steps to get PHP up and running again.
希望这会为某人省去很多麻烦。如果由于某种原因,您无法在命令行中找到 php,并且无法从命令行执行 php,下面列出了让 PHP 重新启动并运行的步骤列表。
double check to make sure PHP is no where to be found by opening your terminal, and typing
find /usr -name phpand hit enter. The main thing you want to look for here is a path with/bin/phpat the end. In my case it's, now that I've installed it, it's/usr/local/php5-20120508-102213/bin/php. If you don't see anything like that then go to the next step. If you see something like that then make note of that path with the/bin/phpat the end, and go to step 4.Go to the terminal and type in
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.4, hit enter. It will ask for your password. Your installing a php package. After you enter your password just follow the steps like any other download. For more information on that download you can visit the binary package website.After you've installed php, open the terminal and type in
find /usr -name phpand hit enter. You should see a few lines of paths. Make note of the one that has/bin/phpat the end of the path. You will be needing that path for the next step.Next, open a text editor, I used TextWrangler for this purpose, go to file up in on the menu bar, and select Open file by name.Then type in
~/.bash_profile. Select Open and at the end of the .bash_profile file type inPATH=$PATH:/usr/local/php5-20120508-102213/bin/ export PATHthe
/usr/local/php5-20120508-102213/bin/part of that is the path that I mentioned to make note of, minus the php at the end. If your path was different, substitute it. Just remember to leave off the php at the end. save and exit.Last step, open the terminal and type in
php -v. Hit enter. You should see something like:PHP 5.4.2 (cli) (built: May 8 2012 09:48:57) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies with Xdebug v2.2.0rc2, Copyright (c) 2002-2012, by Derick Rethansif you're seeing that then everything is working.
通过打开终端,输入
find /usr -name php并按回车键,仔细检查以确保找不到 PHP 。您要在这里寻找的主要内容是/bin/php结尾的路径。就我而言,现在我已经安装了它,它是/usr/local/php5-20120508-102213/bin/php. 如果您没有看到类似的内容,请转到下一步。如果您看到类似的内容,请记下该路径/bin/php的末尾,然后转到步骤 4。转到终端并输入
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.4,按回车键。它会询问您的密码。您正在安装一个 php 包。输入密码后,只需按照任何其他下载的步骤操作即可。有关该下载的更多信息,您可以访问二进制包网站。安装php后,打开终端并输入
find /usr -name php并按回车键。您应该会看到几行路径。记下/bin/php路径尽头的那个。下一步将需要该路径。接下来,打开一个文本编辑器,为此我使用了 TextWrangler,转到菜单栏上的文件,然后选择按名称打开文件。然后输入
~/.bash_profile. 选择打开并在 .bash_profile 文件的末尾输入PATH=$PATH:/usr/local/php5-20120508-102213/bin/ export PATH这
/usr/local/php5-20120508-102213/bin/部分是我提到要记下的路径,最后减去 php。如果您的路径不同,请替换它。请记住最后不要使用 php。保存并退出。最后一步,打开终端并输入
php -v. 点击进入。你应该看到类似的东西:PHP 5.4.2 (cli) (built: May 8 2012 09:48:57) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies with Xdebug v2.2.0rc2, Copyright (c) 2002-2012, by Derick Rethans如果你看到了,那么一切正常。
NOTE:Here is a good resource for working with Command line PHP- located about 1/3 of the way down the page.
注意:这是一个很好的使用命令行 PHP 的资源- 位于页面下方约 1/3 处。
回答by Mike Purcell
There is one of two things going on here, either you didn't install PHP, or PHP is installed and not currently in any of system aware paths. First I would try to find PHP and make sure it exists:
这里发生了两件事之一,要么您没有安装 PHP,要么安装了 PHP,但当前不在任何系统感知路径中。首先,我会尝试找到 PHP 并确保它存在:
$ find / -name php -type f
You should see something like:
你应该看到类似的东西:
/path/to/your/php/bin/php
If PHP binary does exist, check your path:
如果 PHP 二进制文件确实存在,请检查您的路径:
$ echo $PATH
If it does not exist, recompile php.
如果不存在,重新编译php。
If PHP exists on your system, make sure the path to the /bin/php file is included. You can edit your ~/.bash_profileand add the custom path like this:
如果您的系统上存在 PHP,请确保包含 /bin/php 文件的路径。您可以~/.bash_profile像这样编辑和添加自定义路径:
PATH=$PATH:/path/to/your/php/bin/
....
export PATH
Then save and reload the file to ensure your current session has access to new path changes:
然后保存并重新加载文件以确保您的当前会话可以访问新的路径更改:
$ source ~/.bash_profile
With any luck you can now do a php -vand see the version response.
运气好的话,您现在可以执行 aphp -v并查看版本响应。
-- Update --
- 更新 -
Setting actual path:
设置实际路径:
$ vi ~/.bash_profile
...
# Add your custom php path
PATH=$PATH:/bitnami/mampstack-osx-x86/output/php/bin/
....
export PATH
Save and close, then source it:
保存并关闭,然后获取它:
$ source ~/.bash_profile
And now you should be able to run PHP from cli:
现在您应该可以从 cli 运行 PHP:
$ php -v
回答by Ali Schlereth
In response to @MikeTheCoder, the posted export path syntax didn't work for me, but the slightly modified one following did:
作为对@MikeTheCoder 的回应,发布的导出路径语法对我不起作用,但以下稍微修改的语法:
export PATH=/usr/local/php5-5.6.27-20161101-100213/bin/:$PATH
I'm using El Capitan 10.11.6 which defaults to an earlier PHP version.
我正在使用 El Capitan 10.11.6,它默认为较早的 PHP 版本。
回答by karthikeyan ganesan
Close your ssh tunnel and restart again for PHP is getting back and check $ php --version
关闭您的 ssh 隧道并重新启动 PHP 正在返回并检查 $ php --version
And type the command line sudo apt-get install php-fpm or search with newest version of php using " sudo apt-cache search php " then you find php-fpm or php-{version}-fpm and use the following command sudo service php-fpm restart
并键入命令行 sudo apt-get install php-fpm 或使用“ sudo apt-cache search php ”搜索最新版本的 php 然后你找到 php-fpm 或 php-{version}-fpm 并使用以下命令 sudo service php-fpm 重启
回答by Grant
(On macOS Mojave 10.14.6)
(在 macOS Mojave 10.14.6 上)
A shortcut for the mentioned 'source' command above is just the period. So you can do the following:
上面提到的“源”命令的快捷方式就是句号。因此,您可以执行以下操作:
vi ~/.bash_profile
Add in the lines
在行中添加
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
Hit colon W :wto write and colon Q to :qto quit VIM editor (bonus learning)
按冒号 W:w写入,按冒号 Q:q退出 VIM 编辑器(额外学习)
And now you "apply" it by entering into the terminal: . ~/.bash_profile
现在您通过进入终端来“应用”它: . ~/.bash_profile
You can doublecheck the new PHP version now with php -vor which php
您现在可以使用php -v或再次检查新的 PHP 版本which php

