How to make any directory into a simple server using php built-in web server?

| | 2 min read

This article explains a relatively new feature in php; The Built-in web server. Other languages like python and ruby already had their own simple HTTP servers where you can convert you local folders/directories into a simple HTTP server (for python: python -m SimpleHTTPServer) which serves scripts and HTML files and basically does almost everything apache does. Almost!

To use this functionality in PHP, you need to have a version 5.4 or above. Using terminal, navigate to the root of the folder where the index.php or index.html and run the following command.

php -S localhost:8000
// php -S <host>:<port>

You can use localhost or 127.0.0.1. Look here for more details. Now, in your browser, enter the address'http://localhost:8000' and your index.php file will be displayed here. If you want to simultaneously run another PHP built-in web server, repeat the same steps but give a different port. Run 'php -S localhost:8080' and go to 'http://localhost:8080' on your browser.

This is sufficient to run a simple or even a complex application in no time. Setting this up takes a matter of seconds. But there are limitations to this. Since there is no apache involved in this, the .htaccess and .htpasswd files will be of no effect. Security is very little to none. So definitely test it on an apache local server as well before you deploy it. But for UI development and other aspects not involving apache this is sufficient.

You can also run PHPMyAdmin as a PHP built-in web server. Learn how here. Applications involving MySQL database connection can also be served in this server. You can test this by navigating to your drupal installation root folder and run 'php -S localhost:8000' in terminal. Then go to 'http://localhost:8000' and see the magic happen!

PS: You can also replace "localhost" with an IP address so that other computers connected via LAN or to the same wifi network can access this. Eg 'php -S 192.168.10.25:8000'. Now every other computer connected to the same LAN can access this from http://192.168.10.25:8000