What Happens When You Run php artisan serve in Laravel — The Complete Breakdown”

What Happens When You Run php artisan serve in Laravel — The Complete Breakdown”

What Actually Happens When You Run php artisan serve in Laravel

By Raja Babu Mahato • Laravel Deep Dive Series

Every Laravel developer — including you and me — runs this command dozens of times:

php artisan serve

It looks simple. You press Enter, the terminal says “Starting Laravel development server on http://127.0.0.1:8000” and — boom — your app is live locally.

But what’s really happening underneath that? Is Laravel spinning up Apache? Or Nginx? Or something else entirely?

Let’s break it down — from terminal to browser.

Step 1: The Command That Starts Everything

When you type the command php artisan serve, you’re asking Laravel’s command-line tool, Artisan, to run a specific class behind the scenes.

vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php

Inside this file, there’s a handle() method. This is the heart of the serve command.

public function handle()
{
    $this->comment('Starting Laravel development server...');
    
    passthru('php -S localhost:8000 -t public');
}
      

That’s it. That passthru() command is literally what’s running when you type php artisan serve.
It launches PHP’s built-in web server and tells it:

  • host: localhost (your local machine)
  • port: 8000 (by default)
  • serve from: the public directory

Step 2: Why the public Directory?

You might wonder — why doesn’t Laravel just serve the entire project folder?

The answer is security.

The public folder is the only place in your Laravel app that’s meant to be accessed directly by a browser. Everything else — your routes, controllers, models, and sensitive files like .env — are safely hidden.

Inside public/index.php, you’ll find the actual entry point for every Laravel request.
require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';
      

These two lines do something powerful:

  • autoload.php — loads all Composer dependencies
  • bootstrap/app.php — boots up the Laravel framework

Step 3: Booting the Laravel Engine

Inside bootstrap/app.php, Laravel creates its main Application instance:

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
      

Think of this as Laravel’s “engine” — it registers everything: the HTTP kernel, Console kernel, and Exception handler. Once this app is ready, it’s sent back to index.php where Laravel handles requests.

Step 4: Capturing and Handling the Request

This is the moment Laravel takes control. It captures your HTTP request, passes it through middleware, routes, controllers, and finally sends a response.

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();
      

That’s the full HTTP lifecycle in motion — from the browser hitting your local server to the Laravel engine sending back a rendered page.

Step 5: The Full Journey Summarized

  • You type php artisan serve
  • Laravel executes ServeCommand.php
  • PHP’s built-in server starts at localhost:8000
  • The public/index.php file boots Laravel
  • Laravel captures and processes your request
  • The response is rendered back in your browser

All of that — triggered by a single terminal command. Pretty awesome, right?

💡 Final Thoughts

Next time you run php artisan serve, remember — it’s not just launching a server. It’s starting a chain reaction: PHP server → Laravel bootstrap → HTTP handling → response rendering.

One command. Full framework startup. ⚡

Comments (1)

Raja Babu mahato 2 months ago

Well There is a few things I want to mention,the pass through method that your seeing it was used in old version, in latest version laravel uses symphony systems for this

Leave a Comment