Tuesday, 3 October 2017

How to create built in web server in PHP?

How to create built in web server in PHP

Question: In which PHP Version Built-in web server come?
PHP 5.4


Question: Can we run php without apache?
Yes, We can run the php independent.


Question: Can we test our application with Built-in web server feature?
Yes, We can.


Question: Can we test our application with Built-in web server feature?
Yes, We can use this feature to test our module.


Question: Can we go LIVE with Built-in web server ?
Not, Its recommended We should not go LIVE with this Built-In Web Server.


Question: How to check If built-in Web server is available in your PHP Build?
Way 1
print the phpinfo() in php. and Search for Built-in HTTP server , If found means It is available.

Way 2
in php command line, execute following command

php -h

Search -S (Run with built-in Web server), If found means It is available.


Question: How to run PHP Script with built-in Web Server?
Go to file Location and execute following command from command line.
php -S 0.0.0.0:8080 myfile.php



Question: What is use of -S?
It is used to specify the Ip Address with port with which bind.


Question: What is use of -t?

It is used to specify the target folder.


Question: How to detect the request by built-in Web Server?
if(php_sapi_name()=='cli-server'){
    echo "this is request by built-in Web Server";
}



Question: Give full working example of built-in Web Server?
  1. Create a file hello.php
  2. Login to command line
  3. Go to that particular folder in command line
  4. Execute following command.
    php -S 0.0.0.0:8080 hello.php