繁体   English   中英

当我们运行“php artisan serve”时如何将默认url从localhost:8000更改为laravel中的其他Ip

[英]How to change default url from localhost:8000 to other Ip in laravel when we run “php artisan serve”

我正在运行php artisan serve命令

默认结果是:

Laravel 开发服务器启动: http : //127.0.0.1 :8000

我想改变指向其他ip

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM