问题
I am running php artisan serve
command
by default the result is :
Laravel development server started: http://127.0.0.1:8000
I want to change pointing to other ip
回答1:
# 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
回答2:
To change default host and/or port for the artisan serve command, you need to edit the ServeCommand.php file:
$ sudo nano vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php
At the end of it you will find were they are configured:
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'],
];
}
Just change de default value of the host 127.0.0.1 for the ip of your desired host. And 8000 for the number of your desired port.
My case: I have an UbuntuServer 18.04 VM running over GoogleCloud - ComputeEngine, I was unable to see the Laravel execution until I changed the host to 0.0.0.0, of course I changed the port as well to have the 80 as default.
The alternative was executing every time:
$ sudo php artisan serve --host=0.0.0.0 --port=80
in order to obtain the same result.
回答3:
You can use the following solution to solve your problem:
php artisan serve --host 127.0.0.1 --port 80
回答4:
On Laravel 6 you can create a variable SERVER_PORT
in your .env file
Example:
SERVER_PORT=80
Will produce:
$ php artisan serve
Laravel development server started: http://127.0.0.1:80
回答5:
The above answers are acceptable.
I am replying to the question "which file have to make changes". If you want to run php artisan serve
without --port={port_number}
, you can change the port number in ServeCommand.php
under port()
method.
回答6:
As an addition to @Macr1408 answer, since Laravel at this date does not offer a host name/ip config setting (like the SERVER_PORT one), you could extend the ServeCommand class and just redefine what parent getOptions method returns. Here is an example that allows you to set a SERVER_HOST env config value that will change your IP/host name when you run artisan serve:
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;
}
}
来源:https://stackoverflow.com/questions/46468809/how-to-change-default-url-from-localhost8000-to-other-ip-in-laravel-when-we-run