Built-in Web Server in PHP

As many of you might not know that with PHP version 5.4 or newer, you can run your application using the built-in Web server without having XAMP or WAMP installed.

As many of you might not know that with PHP version 5.4 or newer, you can run your application using the built-in Web server without having XAMP or WAMP installed.

Which means you can start learning PHP straight out without installing and configuring a full-fledged web server.

How to Start the server?

To start the server, run the following command from your terminal in your project’s root directory.

$ php -S localhost:8888

Now, if you go to your browser, you’ll see your project running on localhost port 8888. Simple enough?

Simplifying using an Alias

In my case, I’ve further simplified this process by adding an alias named “serve” in my bash file.

Now I can simply run the following in my terminal to start my server.

$ serve

Another Trick

Besides from creating an Alias for the serve command. We can also create a new bash function inside our bash file and making the assignment of system’s port variable.

function serve()
{ 
    php -S localhost:$*
}

Now from your terminal, you can rather mention your desirable port number.

$ serve 8080

Your application is now running on localhost:8080

Just to make it more Fancy

You can also specify a default port in your bash function in order to avoid specifying port every single time.

function serve()
{ 
    php -S localhost:${*-8080}
}

If you have any questions regarding this article, you can comment below or leave us a tweet. You can also follow us on Twitter.

Note: This web server was designed to aid application development and is only useful for testing purposes only. It is not intended to be a full-featured web server & it is not recommended to be used on  a public network.