【发布时间】:2018-03-10 04:15:34
【问题描述】:
【问题讨论】:
-
SERVER_PORT 到 .env 有效,但不要忘记运行 cache:clean 因为这是直接从 env 而不是缓存中读取的。
【问题讨论】:
# php artisan serve --help
Usage:
serve [options]
Options:
--host[=HOST] The host address to serve the application on. [default: "127.0.0.1"]
--port[=PORT] The port to serve the application on. [default: 8000]
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
Serve the application on the PHP development server
【讨论】:
要更改 artisan serve 命令的默认主机和/或端口,您需要编辑 ServeCommand.php 文件:
$ sudo nano vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php
最后你会发现它们是否已配置:
protected function getOptions()
{
return [
['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on', '127.0.0.1'],
['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on', '8000'],
];
}
只需将主机 127.0.0.1 的默认值更改为所需主机的 ip。和 8000 为您所需的端口号。
我的情况: 我有一个运行在 GoogleCloud - ComputeEngine 上的 UbuntuServer 18.04 虚拟机,在将主机更改为 0.0.0.0 之前我无法看到 Laravel 的执行,当然我也将端口更改为默认为 80。
每次都执行替代方案:
$ sudo php artisan serve --host=0.0.0.0 --port=80
为了得到同样的结果。
【讨论】:
您可以使用以下解决方案来解决您的问题:
php artisan serve --host 127.0.0.1 --port 80
【讨论】:
在 Laravel 6 上,您可以在 .env 文件中创建变量 SERVER_PORT
例子:
SERVER_PORT=80
将产生:
$ php artisan serve
Laravel development server started: http://127.0.0.1:80
【讨论】:
以上答案都是可以接受的。
我正在回答“必须更改哪个文件”的问题。如果你想在没有--port={port_number}的情况下运行php artisan serve,可以在port()方法下更改ServeCommand.php中的端口号。
【讨论】:
作为@Macr1408 答案的补充,由于 Laravel 目前不提供主机名/ip 配置设置(如 SERVER_PORT 之一),您可以扩展 ServeCommand 类并重新定义父 getOptions 方法返回的内容。这是一个示例,它允许您设置 SERVER_HOST 环境配置值,当您运行 artisan serve 时该值将更改您的 IP/主机名:
namespace App\Console\Commands;
use Illuminate\Foundation\Console\ServeCommand as LaraServe;
class ServeCommand extends LaraServe
{
protected function getOptions()
{
$options = parent::getOptions();
$options[0][4] = env('SERVER_HOST');
return $options;
}
}
【讨论】: