Built-in Web Server in PHP

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

    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

    PHP is one of the most valued and accessible server-side language, mostly used for creating websites. With a faster turnaround time and enhanced security features, PHP has become one of the most preferred choice for website development by website developers.

    Lots of developers have been a fan of the short arrow function usage since their addition to programming languages. In JavaScript, it has helped make coding functions/closures more concise.

    Imagine you have a scenario where you need to add multiple actions to your Laravel Notification where you can add as many as button components with different URLs.

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.