Set Default Host and Port for Laravel Dev Server
In your .env
file, set the SERVER_PORT
and SERVER_HOST
variables to specify the default host and port for the Laravel dev server:
SERVER_PORT=3000
SERVER_HOST=192.168.178.31
Now, when you run php artisan serve
, the server will start on the specified host and port:
$ php artisan serve
# output
INFO Server running on [http://192.168.178.31:3000].
Press Ctrl+C to stop the server
I was presented with the issue that I work on several projects at once and do not have the possibility to run them containerized. This means I run them on the host system using the laravel dev server.
It's a bit annoying that I have to remember which project I started with wich port. I was looking into extending the serve
command when I stumbled upon the original implementation of the serve
command already having the feature to set the host and port:
Apparently SERVER_PORT
was added in 5.8.24 in 2019 and SERVER_HOST
and was added in 9.8.0 in 2022.