Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

php artisan serve starts but the app is not reachable, or the .env is ignored

Errorphp artisan serve runs but the browser cannot open the site from another device or from Docker

Short answer

php artisan serve listens on 127.0.0.1 port 8000 by default. If a port you passed is busy it will not move on by itself, and from another device or a container nothing reaches 127.0.0.1. Shell variables are also dropped when a .env file exists. Check host, port and variables first.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the server in the foreground and read its output. Then check who already listens on port 8000.

    php artisan serve
    lsof -nP -iTCP:8000 -sTCP:LISTEN
  2. 2Match what you see, then jump to its fix

    1. 1The command exits instead of starting, and you gave --port or set SERVER_PORTThe port you passed is already in useFix
    2. 2It works in the browser on the same computer but not from a phone, a VM or a Docker hostThe server listens on 127.0.0.1, so other machines cannot reach itFix
    3. 3A variable you export before php artisan serve does not appear in env() inside the appShell variables are not passed on because a .env file existsFix
    4. 4Only creating a single server.PHP_CLI_SERVER_WORKERS is set above 1Fix

What does the error look like?

  • php artisan serve runs but the browser cannot open the site from another device or from Docker
  • Server stops right after start with a non-zero exit code when --port is given and the port is busy
  • Only creating a single server.
  • A variable exported in the shell has no effect inside the app

Cause 1 of 4

The port you passed is already in use

1Check

The command only moves to the next port by itself when you did not pass --port. With an explicit port it gives up after the PHP server fails.

lsof -nP -iTCP:8000 -sTCP:LISTEN

2Fix

Stop the other process, or choose a free port.

php artisan serve --port=8001

3Verify

The command prints the new address and stays running.

curl -I http://127.0.0.1:8001

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The server listens on 127.0.0.1, so other machines cannot reach it

1Check

The default host is 127.0.0.1 unless SERVER_HOST is set. That address only accepts connections from the same machine.

php artisan serve --help

2Fix

Bind to all interfaces for local testing only, then open the machine's LAN address.

php artisan serve --host=0.0.0.0 --port=8000

3Verify

Open http://<your-lan-ip>:8000 from the other device.

curl -I http://<your-lan-ip>:8000

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

Shell variables are not passed on because a .env file exists

1Check

When a .env file exists and --no-reload is not used, the command forwards only a short list of variables (APP_ENV, PATH, XDEBUG_* and a few others) and sets the rest to false. Variables you want must be in .env.

grep -n 'MY_VARIABLE' .env

2Fix

Put the value in .env, or start the server with --no-reload so the full environment is passed through.

php artisan serve --no-reload

3Verify

Print the value from inside the app.

php artisan tinker
>>> env('MY_VARIABLE')

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

PHP_CLI_SERVER_WORKERS is set above 1

1Check

A value above 1 needs --no-reload. Without it the command warns and runs a single server.

echo $PHP_CLI_SERVER_WORKERS

2Fix

Either unset the variable, or add --no-reload when you really want several workers.

PHP_CLI_SERVER_WORKERS=4 php artisan serve --no-reload

3Verify

The warning no longer appears.

php artisan serve --no-reload

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Pick one dev port per project and write it into SERVER_PORT in .env.
  • Use 0.0.0.0 only on a trusted network; it exposes the dev server to everyone on it.
  • Keep configuration in .env instead of exporting variables in the shell.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.