Built-in Web Server in PHP

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

    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

    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.

    Checkout how to install xDebug with pecl on your MacOSX

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.