Backend Development 4 min read

Setting Up a Local PHP Development Server for Testing with the Built‑in PHP Server

This guide explains how to set up a local PHP development server—either on Windows or in a Linux VM—by creating a www directory, adding test scripts, using PHP's built‑in web server with optional root or router settings, and verifying the results via browser requests.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Setting Up a Local PHP Development Server for Testing with the Built‑in PHP Server

The author frequently runs small PHP snippets for testing and wants to avoid affecting the main office environment, so a separate PHP instance is installed locally on Windows or inside a Linux virtual machine.

First, create a www directory in the home folder, add test scripts such as index.php and info.php , and start the built‑in PHP web server:

# In home directory create www
mkdir www
cd www

# Create test scripts (index.php, info.php)
# Start a web server
php -S 192.168.204.151:8000
# Note: when running in a VM use the IP address; on Windows you can use localhost:8000

Open a browser and request http://192.168.204.151:8000/ to see the default page, and http://192.168.204.151:8000/info.php to view the script output.

To specify a different document root, create a subdirectory (e.g., test ) and start the server with the -t option:

# Create test directory and script
mkdir ~/www/test
php -S 192.168.204.151:8000 -t test/

For more advanced routing, a router.php file can be used to intercept requests. The router returns static files directly and otherwise prints a welcome message:

<?php
if (preg_match('/\.(?:png|jpg|jpeg|gif|txt)$/', $_SERVER["REQUEST_URI"])) {
    return false; // serve the requested file directly
} else {
    echo "<p>Welcome to PHP</p>";
}
?>

After creating the router and some test files, start the server with the router script:

php -S 192.168.204.151:8000 router.php

Requests such as http://192.168.204.151:8000/ now pass through router.php , while requests for static files (e.g., hello.txt ) return the file contents.

In summary, using PHP's built‑in server in a local or VM environment provides a convenient way to test PHP code without affecting production systems.

backendtestingPHPweb serverLocal Development
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.