Skip to content

PHP’s built-in Web Server

Last updated on February 2, 2021

PHP’s built-in Web Server designed specifically for development and testing. Since PHP5.4, CLI SAPI comes with a built-in web server. A Built-in web server makes it easy to quickly try out some scripts without needing to configure Apache, Nginx, or other web servers.

Built-in sever strongly recommended for development, testing, and demo purpose. Don’t use as production sever and in public networks.

How to use Built-in Server

You will need to have PHP 5.4 or greater installed to use the built-in server. To verify your version of PHP, issue command php -v in a terminal.

php -v output the version of your current installed PHP.

You can then determine if the server is available in your build by running php -h and looking for the -S and -t options which are specific to the server.

 PHP’s built-in Web Server

Start built-in Server

The server runs when you pass the -S switch to PHP on the command line. You then supply the server name and port number:

php -S localhost:8080

This gives initial output and then provides information about each request
How to use Built-in Server

Changing the host name

You can easily change the hostname from localhost to desired name or project name. To use a custom hostname, you just need to add the new server name into /etc/hosts (or the equivalent for your system) and then start the server with the new name as shown below.

php -S arjunphp:8080

Changing the document root directory

By default when you issue sever start command php -S localhost:8080 it will make the current directory as server root. You can change this behavior by specifying the directory path with the -t option while starting to sever.

php -S localhost:8080 -t /var/www/awesomecode

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments