Running PHP on Your Local Machine
Building a PHP website can require testing your PHP files before uploading them to your host. This article provides a guide on how to set up a local PHP server for testing purposes.
Using PHP's Built-in Web Server
PHP versions 5.4 and later include a built-in web server. To run it, follow these steps:
Type the following command:
cd path/to/your/app php -S 127.0.0.1:8000
Note: To make this work, you must ensure you have an index.php or index.html file in the root directory.
Using a Router
To enable cleaner URL routing, you can use a simple router script, such as:
// 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'); }
Run the router with the following command:
php -S 127.0.0.1:8000 router.php
Additional Resources:
The above is the detailed content of How to Set Up a Local PHP Server for Testing?. For more information, please follow other related articles on the PHP Chinese website!