PHP Built In Web Server [Developer Tips]
If you’re a web developer and deal with any type of server side platform like PHP, you know the amount of overhead that goes into just getting your code to run, let alone develop. In the past with PHP, you’d have to install a web server, set up some sort of virtual hosts, add a new DNS entry for each project and make sure the web server had some way to transfer and receive data from the PHP interpreter. Only then would you be able to actually run your code in a browser. Well, those days are over.
PHP 5.4 introduced a HTTP web server! Seriously. If you have PHP 5.4 or greater installed, you can start serving your content over the web with just one command.
php -S localhost:8000 -t foo/
Really, that’s it. Just run that and you’ll instantly have foo/ being served over port 8000. No giant configuration files, no virtualhosts, no CGI and no permission issues.
The beauty of this is that you can easily work on multiple projects and switch back and forth with ease. If you want to stop working on foo/ from the above example and now want to work on bar/, just end that server and start it up again on bar/.
php -S localhost:8000 -t bar/
That’s it! Now, you’re serving bar/! Or, if you want to serve them both at the same time, just fire up another terminal or use screen and use a different port.
php -S localhost:8000 -t foo/ php -S localhost:8001 -t bar/
Now you can have foot/ on port 8000 and bar/ on port 8001. The added benefit here is that each of your projects are identified and served over different ports, rather than different hostnames and virtualhosts. This completely eliminates the need to deal with DNS in any way, which is great for development.
Just set up a single development server for yourself accessible via an easy to remember hostname like dev.mydomain.com. Then, each of your projects will simply be on different ports as you fire them up with a single command line. It’s an absolute dream come true.
dev.mydomain.com:8000 would serve foo/. While dev.mydomain.com:8001 would serve bar/.
While this has been available for about a year now, I’ve yet to see anybody use the built in PHP HTTP server for production. However, I don’t see any real drawbacks to using it on projects and applications that don’t require a full fledged HTTP server. The biggest examples that come to mind are API gateways, which don’t need fancy features like rewrites.