对一个命令使用不同的 PHP 版本 CLI 可执行文件

2024-08-10php开发问题
0

本文介绍了对一个命令使用不同的 PHP 版本 CLI 可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我安装了三个 PHP 版本的 Gentoo 盒子(没关系):

So I have Gentoo box with three PHP versions installed (nevermind the reasons):

  1. /usr/bin/php -> /usr/lib64/php5.4/bin/php
  2. /usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
  3. /usr/bin/php5.6 -> /usr/lib64/php5.4/bin/php
  1. /usr/bin/php -> /usr/lib64/php5.4/bin/php
  2. /usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
  3. /usr/bin/php5.6 -> /usr/lib64/php5.4/bin/php

我想使用 composer 安装 Laravel 框架:

I want to install Laravel framework using composer:

$ composer create-project laravel/laravel --prefer-dist

这会引发错误,因为 Laravel 需要 PHP > 5.5.9 并且默认的 php 解释器是 5.4.所以我发出另一个命令:

This however throws an error because Laravel requires PHP > 5.5.9 and the default php interpreter is 5.4. So I issue another command:

$ /usr/bin/php5.6 /usr/bin/composer create-project laravel/laravel --prefer-dist

这让我更进一步,但随后来自 Laravel 的 composer.json 的一些安装后命令开始发挥作用,导致安装崩溃.

This takes me one step further, but then some post-install commands from Laravel's composer.json comes into play, and installation crashes.

这是因为 composer.json 命令看起来像这样:

This is due to the fact, that composer.json commands look like this:

"post-install-cmd": [
    "php artisan clear-compiled",
    "php artisan optimize"
],

如您所见,默认"解释器又被使用了!

As you can see, the "default" interpreter is used again!

现在,正确的 PHP 文件以以下 shebang 开头:

Now, proper PHP files start with following shebang:

#!/usr/bin/env php

这是一个不错的功能,因为 PHP 解释器可以在不同系统的不同位置找到.不幸的是,在这种情况下 env 命令返回它在 $PATH 环境变量中找到的第一个可执行文件的路径.

This is nice feature as PHP interpreter can be found under different locations on different systems. Unfortunatelly, in this case env command returns path to the first executable it finds in $PATH environmental variable.

我怎么可能改变当前会话环境或执行什么样的技巧来执行整个 Laravel 安装过程 php 命令将调用 /usr/bin/php5.6 而不是 /usr/bin/php?

How could I possibly alter current session environment or what kind of trick to perform so for the execution of whole Laravel installation process php command would invoke /usr/bin/php5.6 instead of /usr/bin/php?

我不想更改 $PATH 变量或修改 composercomposer.json 或 Laravel 的 CLI 实用程序 等文件工匠.

I don't want to change $PATH variable or modify files like composer, composer.json or Laravel's CLI utility artisan.

还假设我想从普通用户帐户执行此操作(即没有 root 权限).

also assume that I want to do this from regular user account (i.e. with no root permissions).

推荐答案

也许你可以尝试修复环境!

Maybe you can try to fix the environnement!

$ php -v
PHP 5.4.x (cli) ...
$ set PATH="/usr/lib64/php5.6/bin:$PATH"
$ php -v
PHP 5.6.x (cli) ...

或者,如果您不想修改 shell 会话的 PATH,您可以仅针对当前命令进行更改:

Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:

$ php -v
PHP 5.4.x (cli) ...
$ env PATH="/usr/lib64/php5.6/bin:$PATH" php -v
PHP 5.6.x (cli) ...
$ php -v
PHP 5.4.x (cli) ...

这篇关于对一个命令使用不同的 PHP 版本 CLI 可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17