Sometimes you want to get a server up and running as quickly as humanly possible. Whether it's trying out a js library where the demos only work with a server, developing a static front-end (stand-alone or for integration with an API) or getting a PHP app up and running without setting up apache or nginx - being able to quickly start a local server is a nice convenience. Here's one of my goto approaches for getting up and running quickly.

PHP Built-in Server

Since the release and general use/availability of PHP 5.4, there's been a built-in PHP server available from the PHP CLI. To run it, from the terminal/command line all you need to type is

php -S localhost:8000

and boom, you're done! You can now access anything in that directory or subdirectories as you would from a normal web server. Naturally, getting to this point has two main pre-requisites - your PHP version must be 5.4+ (php -v), and PHP must be on your path.

Command Explained

Breaking it down, this is taking advantage of the (very not-production ready) built-in server that PHP 5.4+ offers. If you were to type

php -h

in the terminal, one of the instructions provided would be

php [options] -S <addr>:<port> [-t docroot]

This specifies the structure of starting the built-in PHP web server. In reference to our example: the -S specifies that you're trying to start the built-in server, localhost is the address and 8000 is the port. We're not using the -t docroot option here, but when specified you could use it to create a routing script (for more information on that, see the PHP docs here).

Accessing the server remotely

If you want to access the web server from a remote machine (useful for trying it on your phone, or letting other people on your network access it using your IP), you can specify the address as instead of localhost. For example

php -S

More Examples

For more one-line server starters, there's a helpful Github gist here. It doesn't provide any specifics or explanations of the examples, but it gives examples in multiple languages including node.js, ruby, and python.