Using PHP's built-in web server is particularly advantageous for quick testing and development. It supports features such as URL rewriting and can serve static files, making it a versatile tool for developers. In this tutorial, we will cover how to start the server, configure it for your project, and manage common tasks.

Prerequisites

Before you begin, ensure you have the following:

  • PHP installed on your machine (version 5.4.0 or higher).
  • A terminal or command prompt to execute commands.

Starting the Built-in Web Server

To start the PHP built-in web server, navigate to your project directory in the terminal and run the following command:

php -S localhost:8000

This command specifies that the server will run on localhost at port 8000. You can change the port number if needed.

Example Directory Structure

Assuming you have a simple project structure like this:

/my-php-app
    ├── index.php
    └── css
        └── styles.css

You would navigate to the my-php-app directory and run the server command.

Accessing Your Application

Once the server is running, open your web browser and go to http://localhost:8000. You should see the output of your index.php file.

Handling Static Files

The built-in server can serve static files such as CSS, JavaScript, and images. For example, if you have a styles.css file in a css directory, you can link it in your index.php like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/styles.css">
    <title>My PHP App</title>
</head>
<body>
    <h1>Welcome to My PHP App</h1>
</body>
</html>

When you refresh your browser, the CSS file will be applied to your HTML.

Customizing the Document Root

By default, the built-in server serves files from the current working directory. If you want to set a different document root, you can specify it with the -t option. For example:

php -S localhost:8000 -t public

This command sets the document root to the public directory. Ensure your project structure reflects this change:

/my-php-app
    ├── public
    │   ├── index.php
    │   └── css
    │       └── styles.css
    └── src

URL Rewriting

If your application requires URL rewriting (e.g., for clean URLs), you can create a router.php file to handle requests. Here’s a simple example of a router:

<?php
// router.php

$request = $_SERVER['REQUEST_URI'];

switch ($request) {
    case '/':
        require __DIR__ . '/index.php';
        break;
    case '/about':
        require __DIR__ . '/about.php';
        break;
    default:
        http_response_code(404);
        echo "404 Not Found";
        break;
}

Start the server with the router:

php -S localhost:8000 router.php

Now, navigating to http://localhost:8000/about will load the about.php file.

Error Handling

The built-in server outputs errors directly to the console, which is helpful for debugging. However, you can also customize error handling in your PHP scripts. For example, you can set a custom error handler:

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    echo "Error [$errno]: $errstr in $errfile on line $errline";
});

This will provide more detailed error messages when something goes wrong.

Security Considerations

While PHP's built-in server is excellent for development, it is not recommended for production use. It lacks many security features of full web servers. Always ensure you use a proper web server for live applications.

Conclusion

PHP's built-in web server is a powerful tool for local development, allowing for quick testing and iteration. By following the steps outlined in this tutorial, you can set up a development environment that is both efficient and effective.

Learn more with useful resources