Home  >  Article  >  Backend Development  >  How do I run a PHP server locally?

How do I run a PHP server locally?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-12 18:14:02327browse

How do I run a PHP server locally?

How to Run a PHP Server on Your Local Machine

Setting up a PHP server on your local machine allows you to test PHP files before uploading them to a web host.

Solution:

With PHP 5.4 and later, you can use the built-in web server:

  1. Navigate to the directory containing your PHP app using the terminal.
  2. Run the following command:
php -S 127.0.0.1:8000

Visit http://127.0.0.1:8000 in your browser to verify that your server is running.

Additional Router:

To create a more robust router, use the following code:

// 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');
}

Then, run the command:

php -S 127.0.0.1:8000 router.php

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 do I run a PHP server locally?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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