CakePHP内置的Web服务器:您能指定所使用的PHP二进制文件吗?

huangapple go评论45阅读模式
英文:

CakePHP built in web server: can you specify the PHP binary that's used

问题

CakePHP中有一个内置的Web服务器,用于开发。

可以使用bin/cake server启动该服务器,默认情况下会在http://localhost:8765上提供应用程序。

在我的情况下,我有一个旧的CakePHP 3.8.13应用程序。在我的MacBook上,全局php可执行文件是PHP 8版本:

% php -v
PHP 8.0.27 (cli) (built: Jan 12 2023 15:36:37) ( NTS )

这是有问题的,因为CakePHP 3.8.13不设计运行在PHP 8上,它适用于PHP 7。

在我的MacBook上,我安装了PHP版本7.1、7.2、7.3和8.0,它们位于/usr/local/opt/目录下。这些是通过brew安装的。所以,如果我想执行PHP 7.2,可以这样做:

% /usr/local/opt/php@7.2/bin/php -v
PHP 7.2.34 (cli) (built: Jan 21 2023 06:18:05) ( NTS )

我有一个问题:如何使bin/cake server使用二进制文件,例如PHP 7.2,而不是全局安装的版本PHP 8?

我尝试过alias php=/usr/local/opt/php@7.2/bin/php,最初看起来似乎可以正常工作,因为随后执行php时会使用适当的版本:

% alias php=/usr/local/opt/php@7.2/bin/php
% php -v
PHP 7.2.34 (cli) (built: Jan 21 2023 06:18:05) ( NTS )

然而,如果我停止然后重新启动内置的CakePHP Web服务器,日志中会显示以下内容:

% bin/cake server
built-in server is running in http://localhost:8765/
You can exit with `CTRL-C`
[Wed Apr 19 09:35:19 2023] PHP 8.0.27 Development Server (http://localhost:8765) started

请注意,这使用的是PHP 8.0.27,这不是我想要的。

英文:

In CakePHP there is a built in web server that can be used for development.

This can be started with bin/cake server which then serves the application by default on http://localhost:8765

In my case I have an old CakePHP 3.8.13 application. On my MacBook the global php executable is PHP version 8:

% php -v
PHP 8.0.27 (cli) (built: Jan 12 2023 15:36:37) ( NTS )

This is problematic because CakePHP 3.8.13 is not designed to run on PHP 8. It works on PHP 7.

On my MacBook I have PHP versions 7.1, 7.2, 7.3 and 8.0 installed in /usr/local/opt/. These have been installed with brew. So if I want to execute PHP version 7.2 I can do it like this:

% /usr/local/opt/php@7.2/bin/php -v
PHP 7.2.34 (cli) (built: Jan 21 2023 06:18:05) ( NTS )

The question I have is: how can I make bin/cake server use a binary such as PHP 7.2, rather than the globally installed version PHP 8?

I tried alias php=/usr/local/opt/php@7.2/bin/php and this initially appears to work in that when subsequently executing php it uses the appropriate version:

% alias php=/usr/local/opt/php@7.2/bin/php
% php -v
PHP 7.2.34 (cli) (built: Jan 21 2023 06:18:05) ( NTS )

However, if I then stop and restart the inbuilt CakePHP web server it says the following in the logs:

% bin/cake server
built-in server is running in http://localhost:8765/
You can exit with `CTRL-C`
[Wed Apr 19 09:35:19 2023] PHP 8.0.27 Development Server (http://localhost:8765) started

Notice that this is using PHP 8.0.27 which isn't what I want.

答案1

得分: 2

服务器命令通过PHP的system()调用创建一个新的Shell,但别名不会传播到子Shell。

从CakePHP 4.1.6开始,服务器命令识别PHP环境变量,因此你可以像这样操作:

export PHP=/path/to/php ; bin/cake server

在早期版本中,你可以创建自己的CakePHP命令,使用环境变量,手动启动PHP内置服务器(/path/to/php -S localhost:8765 -t /path/to/webroot/),或者使用Homebrew等工具来管理PHP版本,正如@Cbroe在评论中提到的那样。

英文:

The server command creates a new shell via PHP's system() call, and aliases do not propagate into sub-shells.

As of CakePHP 4.1.6 the server command recognizes the PHP environment variable, so that you could do something like:

export PHP=/path/to/php ; bin/cake server

With earlier versions you could either create your own CakePHP command that uses an environment variable, start the PHP built-in server manually (/path/to/php -S localhost:8765 -t /path/to/webroot/), or manage PHP versions with for example Homebrew as mentioned by @Cbroe in the comments.

huangapple
  • 本文由 发表于 2023年4月19日 16:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定