How to use PHP built-in web server

If you are a PHP developer, chances are you have already set up a web server (WAMP or LAMP software) on your desktop. It gives you the ability to test your PHP code locally rather than testing it on a remote server.

But what if you are working on a machine where you do not have a web server installed? Or, you may not have sufficient permissions to install the webserver on a desktop.

Luckily, PHP provides a way out of this situation. PHP has a built-in web server that can be run directly without the need for any setup or installation.

Some assumptions

For this tutorial, I am making the following assumptions-

  1. My first assumption is that you are on a Windows machine, but you can implement this on any system by downloading appropriate PHP files.
  2. My second assumption is that your website is in the following folder in your desktop- F:projectsmy-php-website. You may change it to the appropriate folder of your website.
  3. My third assumption is that there is a valid index.php file in your website root folder

How to run the PHP built-in web server

  1. Download PHP for Windows.
  2. Unzip it and save it on your desktop- F:projectsmy-php-websitephp
  3. Now press, type cmd, and press enter, to open your command prompt
  4. Now navigate to your website’s root folder using this command- cd f:projectsmy-php-website
  5. Now execute this command to run the webserver pathtophpphp.exe -S localhost:4000

It will immediately fire up the PHP web server. You can type localhost:4000 in your web browser and it will run your index file.

That’s great! Such a convenience without the hassles of setting up an entire web server. You can run any number of instances of this server. It is single-threaded and has its limitations, but it is sufficient for running the PHP website test code.

You can also make changes to the PHP configuration by editing the php.ini file. You can also add/remove PHP modules.

URL Rewriting and routing

There is one important feature missing in PHP built-in server- URL Rewriting. Suppose you have URL rewriting for pretty permalinks enabled in your website and you want to test that functionality locally.

Apache webserver provides the capability to add the .htaccess file to implement URL rewriting. In the PHP server, you can implement similar functionality by setting up a router.php file.

Add a router.php file to your website root and add the following code-

// router.php

// Get the url path and trim leading slash
$url_path = trim( $_SERVER[ 'REQUEST_URI' ], '/' );

// If url_path is empty, it is root, so call index.html
if ( ! $url_path ) {
    include( 'index.html' );
    return;
}

// If url_path has no dot, it is a post permalink, so add .html extension
if( ! preg_match( '/[.]/', $url_path ) ) {
    include( $url_path . '.html' );
    return;
}

This code takes the path from URL and rewrites requests for HTML files to call them without .html extension. You can implement any functionality you want.

Do remember, adding router.php will give the responsibility of adding the entire response on your shoulders. So, if you are including static files like style files with .css extension, you will have to handle that as well.

// Get the url path and trim leading slash
$url_path = trim( $_SERVER[ 'REQUEST_URI' ], '/' );

// In case of css files, add the appropriate header
if( preg_match( '/[.css]/', $url_path ) ) {
    header("Content-type: text/css");
    include( $url_path );
    // You can do the same for other file types as well
}

Now, go to the command line. If your PHP server is already running, press ctrl+c to stop it. Run this command-
pathtophp.exe -S localhost:4000 router.php
Now, again open the localhost:4000 in your web browser. It will now execute your router.php file and which, in turn, will include the required PHP files.

This technique will save you time if you quickly need to check your code.

Limitations of PHP built-in server

PHP built-in server is a useful tool for developers. However, it has its limitations.

  1. It is single-threaded. That means if a request is blocked, the server will stall the execution.
  2. Once you start adding more logic to your router.php file, it will soon become cluttered. It is very difficult to scale it for large projects.
  3. There is no SQL support. You can either use SQLite, which is a good option or install a separate SQL server.

Conclusion

Despite its limitations, PHP built-in web server is a useful tool for web developers. It helps in saving time in many situations and makes code testing, an easy task.

Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *