Codectionary / Developer documentation / PHP

Learn how php -S serves a local project, what localhost and ports mean, how to read errors, and why this server is not for production.

A server for local learning

PHP includes a small web server with its command-line interface. It lets you test a PHP page through an HTTP URL without first setting up Apache or Nginx. A browser request reaches the server; the server executes a requested PHP script and returns its output. It can also serve ordinary static files such as CSS or images from the document root.

The PHP manual is explicit: this server is for development, testing or controlled demonstrations. It is not a full-featured web server and must not be used on a public network. Keep it bound to localhost for the exercises in this guide.

Understand localhost and port 8000

localhost is a name for your own computer’s loopback interface. Visiting http://localhost:8000/ from that same computer connects to a server listening there. It is not a public website address. The number after the colon is a port: it identifies the particular listening service on that computer. Another local service could use a different port, such as 8001.

The URL and server command must agree on host and port. The http:// part identifies the protocol; localhost:8000 identifies where the browser should connect; a trailing path such as /index.php selects a resource in the server’s document root. The built-in server does not set up HTTPS simply because it uses localhost.

Start the server from the project folder

Open a terminal in the folder containing index.php, then run the command below. php starts the installed PHP command-line program. -S tells it to start the built-in web server. localhost is the local host name and 8000 is the listening port. Keep the terminal open and visit http://localhost:8000/ in a browser. When the URL has no filename, the server can find an index.php or index.html in that directory.

By default, the document root is the current working directory at the moment the server starts. A request for /index.php reads the index.php under that directory. If your project deliberately keeps publicly served files in a public subfolder, run the server from the project root with -t public to select that folder instead; only do this when the folder exists.

Start in the current project folder

php -S localhost:8000

Optional: serve an existing public subfolder

php -S localhost:8000 -t public

View the page and diagnose errors

Open http://localhost:8000/ or http://localhost:8000/index.php. The browser renders the returned content. The terminal running PHP reports requests and server messages, so leave it visible while testing. PHP syntax errors can be checked without the server by running php -l index.php in another terminal. Runtime error detail depends on your PHP configuration; do not rely on every error appearing in the browser.

If you see a 404, check the current directory, requested path and filename. If the browser says it cannot reach the site, confirm the PHP process is still running and that the browser URL matches the address and port shown in the terminal. If you see PHP source instead of the generated page, you may have opened a file:// URL or served the file with a server that does not process PHP.

Check a file for syntax errors in another terminal

php -l index.php

Stop the server or change the port

To stop the development server, focus its terminal and press Ctrl+C. The localhost URL will then stop responding until you start the server again. If PHP reports that it cannot listen on localhost:8000, another process may already occupy the port. Stop the old development server if it belongs to you, or choose another free port and use the matching browser URL.

If localhost does not resolve as expected on your machine, try binding to 127.0.0.1 and opening http://127.0.0.1:8000/ on the same computer. Do not bind the built-in server to a public network interface to “fix” a local address problem.

Use a different local port

php -S localhost:8001

Use the IPv4 loopback address locally

php -S 127.0.0.1:8000

How this differs from production hosting

The built-in server has a limited feature set and normally handles requests in a single process, so one blocked request can delay others. It is intended to make local iteration easy, not to provide the concurrency, security controls, HTTPS termination, deployment management or operational setup expected of a public service.

A production deployment commonly uses a web server such as Nginx or Apache with PHP-FPM or another supported PHP integration, plus deliberate document-root, TLS, logging, permissions and update configuration. PHP-FPM is a way to run PHP workers behind a web server; it is not a browser and is not the same as php -S. Hosting details vary, so follow your platform’s current deployment instructions when you move beyond local development.

Keep private configuration and credentials out of publicly served folders in any deployment. Even for local practice, use a project folder containing only files you intend the development server to serve. Never use php -S as your public production host.

Try it with a real page

Create the first-program index.php, start the server in its folder, open the local URL and then change the greeting. Watching both the browser output and the terminal request log makes the request and response cycle concrete.

Syntax

<?php

echo "Hello, Codectionary!";

Examples

Serve one local PHP page

Save this code as index.php in a project folder, run php -S localhost:8000 from that folder, then open http://localhost:8000/.

<?php

echo "Hello, Codectionary!";
  1. The browser requests / from localhost on port 8000. The built-in server finds index.php in its document root.
  2. PHP executes echo and places Hello, Codectionary! in the HTTP response body. The browser displays the result rather than running the PHP code.
  3. Change and save the quoted text, then refresh to make another request and see the new response. Stop the server with Ctrl+C when done.

Best practices

  • Run the built-in server only on localhost or a loopback address for local development and controlled tests.
  • Start it in the intended document-root folder, or specify an existing root with -t.
  • Use a production-ready server configuration and current platform guidance for any public deployment.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading