![](/img/trans.png)
[英]In localhost when use I php artisan serve and npm run watch, output of url('/') is http://localhost:8000
[英]How to change default url from localhost:8000 to other Ip in laravel when we run “php artisan serve”
# 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 是您想要的端口號。
我的情況:我有一個 UbuntuServer 18.04 VM 在 GoogleCloud - ComputeEngine 上運行,在我將主機更改為 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 env 配置值的示例,該值將在您運行 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.