Hey everybody you may probably know starting from PHP 5.4, PHP has a built-in web server. In this tutorial, we will learn how to use PHP built-in web server. And we’ll see how we can actually take advantage of this simple web server to do some fun stuff.
The first you can ask why do we need a PHP built-in server when we already have Apache/Nginx? And we also have some nicely packaged tools such as WAMP and XAMPP. The answer is as stated in PHP official page. This web server was designed to aid application development. It is not a full-featured web server, so we should not use it for production.
However, as it comes with PHP, it is super handy when we need to spin up a web server to do some testing or development. It saves us from installing any real server or other third party tools.
2. Download PHP
First we need to download php from http://windows.php.net/download/ for windows user
And if you use Linux, you need to download from http://php.net/downloads.php
First extract your php in your any drive and run below command in your terminal or cmd .
php -S localhost:8000
It will treat current directory as the document root directory and if a request does not specify a file, then either index.php or index.html in the given directory are served. You will be able to visit the site via URL localhost:8000 as the URL from any browser.
2. Specifying a document root directory
php -S localhost:8000 -t f:\php\foo
The -t option allows us to specify a document root directory. In the example above, request will be served from foo directory.
Now you can create and run any .php or .html file from foo directory
You can also Write just in your index.php then you will see below output