Login  or  Register  / Search

How to Run PHP Built-in Webserver on Windows?

Today we will discuss on how to run PHP built-in webserver on windows and let you run your PHP script without using xampp or apache, although PHP built in webserver it’s not that robust but it’s great to handle some development, so let’s get started.

The PHP built-in webserver installation instruction
Note: you may skip this part if you have installed already PHP 5.4 or (PHP 5.4.8 as what I used for this example)

Now check that we properly installed PHP on our local machine run ‘cmd’ go to the folder using windows command line ‘cd C:\PHP5.4′ (without quote) and type ‘php –version’ (without quote), If you successfully installed the PHP on your machine you’ll see version of it.
  
PHP --version command line

Create a folder inside PHP directory name ‘public_html’
  
Public HTML on PHP Directory Folder

Back to command line type ‘cd public_html’ and ‘C:\PHP5.4/php -S localhost:8080′ (Actually you can use any port you like. But for this testing purposes we will use 8080)
  
Running webserver command line on PHP

Once you go to http://localhost:8080/ you’ll end up 404 Not Found so we need to create index.php and routes.php (for routing purposes)

index.php

<?php 

    echo 'It is Working. Great!'

?>

routes.php

<?php 
    if(file_exists(__DIR__.'/'.$_SERVER['REQUEST_URI'])) {
      return false;
    } else {
      include 'index.php';
    }
?>

If the PHP webserver is running hit ‘Ctrl+C’ and type again ‘C:\PHP5.4/php -S localhost:8080 routes.php’ Just to ensure that if the requested file is not exist it will automatically redirect to index.php

AND VOILA!…
Localhost on 8080 port is working great

You just have learned how to run a PHP built-in webserver, Have fun!