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

How do I run a PHP server locally?

Patricia Arquette
Release: 2024-11-12 18:14:02
Original
379 people have browsed it

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
Copy after login

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');
}
Copy after login

Then, run the command:

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

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!

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