How to Run a PHP Server Locally Using the Built-in Web Server?

Patricia Arquette
Release: 2024-11-11 18:09:03
Original
203 people have browsed it

How to Run a PHP Server Locally Using the Built-in Web Server?

Running PHP Server on Local Machine

With the advent of PHP 5.4, developers have access to a built-in web server for conveniently testing PHP files locally. The following steps guide you through the process:

  1. Open a terminal window and navigate to your application directory using the cd command.
  2. Execute the following command:

    php -S 127.0.0.1:8000
    Copy after login

This command starts the PHP server on your local machine, listening on port 8000.

  1. Ensure that you have either an index.php or index.html file in your project directory.
  2. Open your browser and navigate to http://127.0.0.1:8000. Your PHP application should now be accessible and ready for testing.

To enhance the functionality of your local server, consider implementing a simple router. Here's an example:

// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    require_once('resolver.php');
}
Copy after login

After saving the router file, run the following command:

php -S 127.0.0.1:8000 router.php
Copy after login

This enhanced server configuration allows for more flexible routing and resource handling.

References:

  • [PHP Built-in Web Server](https://www.php.net/manual/en/features.commandline.webserver.php)
  • [PHP Command Line Options](https://www.php.net/manual/en/features.commandline.options.php)

The above is the detailed content of How to Run a PHP Server Locally Using the Built-in Web Server?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template