Built-in Web Server in PHP

June 20, 2017 By - Usama Muneer | 934

    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

    If you're on MacOSX & use Laravel valet for managing your development environment. Then this issue might arouse when you try to upgrade your PHP version or even Laravel Valet. But there's an easy fix for that.

    Maintaining Database Relationships in Laravel is quite a task but when it comes to nested relationships, things get a bit tangled up to handle.

    When it comes to validation in Laravel. Developers often refer to using separate Requests to achieve it. Now for creating and updating, rules might get a little bit tricky. But we've got you covered.

    While Developing applications in Laravel, there comes situations where you have date in simple string format. For instance getting raw response from an API. But you want to convert that string into Carbon instance and use the Carbon helpers on it. Well there's a simple solution to it.