SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

Fun with PHP built-in web server

If you haven't heard yet, you should probably know starting from PHP 5.4, PHP has a built-in web server. In this tutorial, we will teach you how to use PHP built-in web server. And show you how we can actually take advantage of this simple web server to do some fun stuff.

Introduction

The first question comes to mind when PHP 5.4 introduced built-in web server is, why do we need a PHP built-in server when we already have Apache/Nginx? And we also have some nicely packaged tools such as WAMP and XAMPP. The answer is as stated in PHP official page. This web server was designed to aid application development. It is not a full-featured web server, so we should not use it for production.

However, as it comes with PHP, it is super handy when we need to spin up a web server to do some testing or development. It saves us from installing any real server or other third party tools. In section 3 of this tutorial, we take advantage of PHP built-in web server to run examples of pChart. It is easy and fast. You can apply the same techniques when you ever want to test out something.

Usage

In this section, we will go through a couple of simple commands for PHP web server.

Starting a server

php -S localhost:8000

It will treat current directory as the document root directory and if a request does not specify a file, then either index.php or index.html in the given directory are served. You will be able to visit the site via URL localhost:8000 as the URL from any browser.

Specifying a document root directory

php -S localhost:8000 -t foo/

The -t option allows us to specify a document root directory. In the example above, request will be served from foo directory.

Using router file

php -S localhost:8000 router.php

In some situation, you might want to use some third party router to serve the request. You can do so easily by supplying the router file additionally.

As you can see, PHP built-in web server is an easy to use web server, which does not come in with complicated configurations. It is perfect for development and testing purpose.

Running pChart in PHP built-in web server

To demonstrate how we can have fun with PHP built-in web server. We will show you how easy and fast to setup pChart examples on our local machine. Brief information about pChart, it is a server-side charting library. Which means it requires a server to run.

Let's get started.

Step 1: Download pChart

curl -LOk http://www.pchart.net/release/pChart2.1.4.tar

We are using a Mac and comfortable with doing things with Terminal. Feel free to download it directly.

Step 2: Extract zip/tar file

tar -xzf pChart2.1.4.tar

Step 3: Start the server

cd pChart2.1.4/examples/
php -S localhost:8888

Step 4: It is done

Now it is done. Open a browser and type in http://localhost:8888/. You can now check out pChart's awesome demo.

img

Are you ready to make use of PHP built-in web server? How about using it to run our resumble file uploader? Have fun!

The end

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.