如何强制命令行的 PHP 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7767447/
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 force PHP Version for Command Line?
提问by Bluemagica
I am hosted with 1and1.com, and I have setup my files to be parsed with php5 using .htaccess
.
我在 1and1.com 上托管,并且我已将我的文件设置为使用 .php5 解析.htaccess
。
But that only works in apache, and not in command line, which defaults to the server default php4.
但这仅适用于 apache,而不适用于命令行,命令行默认为服务器默认 php4。
So currently I can not setup cron jobsto run my code as php5. Any ideas?
所以目前我无法设置cron 作业来将我的代码作为 php5 运行。有任何想法吗?
采纳答案by DaveRandom
If both are installed, all you need to do is run the script using the relevant PHP binary.
如果两者都安装了,您需要做的就是使用相关的 PHP 二进制文件运行脚本。
So for example:
例如:
// Runs using the PHP binary located at /usr/bin/php
* * * * * root /usr/bin/php -n "/path/to/script.php"
or
或者
// Runs using the PHP binary located at /var/php5
* * * * * root /var/php5 -n "/path/to/script.php"
All you need to know is the full file system path of the PHP CLI binaries, and call the relevant one to run your code.
您需要知道的只是 PHP CLI 二进制文件的完整文件系统路径,并调用相关路径来运行您的代码。
回答by dicotout
In accordance to https://community.1and1.com/using-php-composer-at-1and1/, create .profile in your root projet and add the line :
根据https://community.1and1.com/using-php-composer-at-1and1/,在您的根项目中创建 .profile 并添加以下行:
alias php='/usr/bin/php5.5-cli'
It works for me. Log out and log back in SSH and do :
这个对我有用。注销并重新登录 SSH 并执行以下操作:
php -v
Result :
结果 :
PHP 5.5.32 (cli) (built: Feb 15 2016 16:13:44)
PHP 5.5.32 (cli)(构建时间:2016 年 2 月 15 日 16:13:44)
回答by hakre
If you can execute PHP scripts directly in the shell like:
如果您可以直接在 shell 中执行 PHP 脚本,例如:
$ script.php
you can specify the binary that will execute the script in it's first line:
您可以在第一行指定将执行脚本的二进制文件:
#!/usr/bin/php
<?php
That line is called shebang. The line might differ on your system, you need to know the full file system path of the PHP CLI binary you want to use for that script.
那条线叫做shebang。该行在您的系统上可能有所不同,您需要知道要用于该脚本的 PHP CLI 二进制文件的完整文件系统路径。
If you execute that file in the shell, the specified binary will be used. Same for cron.
如果在 shell 中执行该文件,将使用指定的二进制文件。对 cron 也是一样。
If you execute that file via your webserver, PHP will drop that line silently.
如果您通过网络服务器执行该文件,PHP 将静默删除该行。
See as well:Features: Using PHP from the command line
另请参阅:功能:从命令行使用 PHP
回答by Emir Akayd?n
there must be two PHP directories and one of them should be the default one. try to find out php5's path from the root of your server and use full path at your cron job.
必须有两个 PHP 目录,其中之一应该是默认目录。尝试从服务器的根目录中找出 php5 的路径,并在您的 cron 作业中使用完整路径。
回答by Carlo Rodríguez
This is another approach to force PHP from Command Line on 1and1.
这是在 1and1 上从命令行强制 PHP 的另一种方法。
Log in with ssh on your server and create a new file called .profile
in there you will write the following line
alias php='/usr/local/bin/php5'
Log back in with ssh and check php version with php -v
you should see
the version is now 5.
在您的服务器上使用 ssh 登录并在其中创建一个新文件.profile
,您将编写以下行
alias php='/usr/local/bin/php5'
使用 ssh 重新登录并检查 php 版本,php -v
您应该看到版本现在是 5。
回答by Ryan
I finally got this working. The problem was a few-fold.
我终于得到了这个工作。问题有几个方面。
First, the PHP being used was in /etc/bin instead of the MAMP version. I was able to change this and use the MAMP version instead.
首先,正在使用的 PHP 位于 /etc/bin 中,而不是 MAMP 版本中。我能够改变这一点并改用 MAMP 版本。
Second, to use php via the CLI you have to make sure to use the FULL path to php and not just php itself. I.e. use /MAMP/bin/php/php5.3.x/php in your exec() call. This is now working for me.
其次,要通过 CLI 使用 php,您必须确保使用 php 的完整路径,而不仅仅是 php 本身。即在您的 exec() 调用中使用 /MAMP/bin/php/php5.3.x/php 。这现在对我有用。
You can check out my GIST here...https://gist.github.com/1861487
你可以在这里查看我的 GIST... https://gist.github.com/1861487