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
Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first Fifteen years a number of leadership positions at AmarBebsha Ltd as a CTO, Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now I’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.