Skip to content
Home » PHP Built-in web server

PHP Built-in web server

What is PHP Built-in web server:
  • As of PHP 5.4.0, PHP provides a built-in web server.
  • It allows to launch a basic web server for development purposes via the command line, test your code and then shut it down when done.
    No need of any other web server like Apache.
Why use it:
  • This will help us to get rid of test files/directories from Project directory.
  • Makes it easy to quickly try out some scripts without needing to configure Apache or really do anything much.
  • Allow you instantly running PHP code from any directory by making that directory as document root instead of setting up virtual Host in other web servers.
Where not to use:
  • It is not intended to be a full-featured web server.
  • It should not be used on a public network.
How to use it:
  • To make a directory as document root,
    Either go to that directory from command line and run the below command.
    php -S localhost:port’
    OR give the directory name directly in the command from whatever directory you are in.
    ‘php -S localhost:port -t /var/www/directory-name’
    ‘php -S localhost:port -t H:testwebserver’
  • Then open any web browser and run below url.
    http://localhost:port/

    Example:
    php -S localhost:8004 (from command line)
    http://localhost:8004/ (from any web browser)
    http://localhost:8004/test.php [add test.php file in that directory and write any php code. <?php phpinfo(); ?> for example]

    NOTE:
    – Port number can be any number that is not in use by anything else.
    – Do not close the command line or the active request till working, else web server instance will be closed.
    – If no file specified, then it returns index.php or index.html present in that directory else it will return a 404 response code.
Other Options:
  • We can make the web server accessible on a port to an interface:
    php -S 0.0.0.0:8000′ (0.0.0.0 as either IP address or server name)
  • We can make a router so that the router script will run at the start of each HTTP request.
    php -S localhost:port -t H:testwebserver H:testwebserverrouter.php’

2 thoughts on “PHP Built-in web server”

Leave a Reply

Your email address will not be published. Required fields are marked *