Built-in Web Server in PHP

Last Updated at : July 25, 2017 By - Usama Muneer | 1477

    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.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Inspired from Laravel's router, lets build our very own PHP's router for managing our endpoints well for our next PHP project.

    Another #LaraconUS in the books & It was the 6th instalment of #LaraconUS happened at Chicago with full swing & we got a lot to learn this time with exceptional line of speakers and never-ending community support by fellow Artisans

    Laravel is one of a famous and widely used PHP framework which is growing with immense pace since its version 4.2. Learning Laravel & Setting up your application locally is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle.

    There are several PHP frameworks for a web development project. Among them Laravel is regarded as the best open-source PHP web framework.