Drupal: Use PHP’s built-in Web server for development

If you have a Drupal site, and believe setting up yet another virtual host in Apache or nginx will consume too much of your time, you can use PHP’s built-in Web server available since version 5.4.0. Assuming your database and settings.php file are ready, with the help of a router file to mimic the behaviour of Apache’s .htaccess file, the built-in server running as a normal user should be enough:

  • In the terminal, get into the directory of your Drupal site:
cd /home/me/drupalsite
  • Copy the following code for the router:
if (preg_match("/.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(.php)?|xtmpl)/", $_SERVER["REQUEST_URI"])) {
  print "Errorn"; // File type is not allowed
} else
if (preg_match("/(^|/)./", $_SERVER["REQUEST_URI"])) {
  return false; // Serve the request as-is
} else
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $_SERVER["SCRIPT_NAME"])) {
  return false;
} else {
  // Feed everything else to Drupal via the "q" GET variable.

Code source

  • Save it into router.php:
cat >> router.php
  • Paste the code, then hit Ctrl+D to save.
  • Start PHP’s built-in Web server:
php -S localhost:8008 router.php
  • In a Web browser, visit the server at the following address:

This should be sufficient in most cases to run a Web server and see your local Drupal site. It is similar to running a local server using WEBrick for running a Ruby on Rails application locally. However, there are a few limitations to remember:

  • As a normal user, you can only make the server listen to ports higher than 1024, which excludes the standard port 80 for HTTP. If you want to run a server on the lower ports, call php using sudo, or go with Apache or nginx.
  • Drupal favours a LAMP environment and uses Apache’s .htaccess file typically unavailable to other Web servers for leveraging some of its functionality, like clean URLs. The router.php file is only to simulate the behaviour of a .htaccess file which does not affect PHP’s built-in Web server. If you edit your site’s .htaccess file (e.g. for adding RewriteRule directives), you will have to write PHP code in router.php to imitate those changes, or use Apache.
  • Drupal will be able to write files in your site’s directory. However, it will not be able to show you the progress of uploaded files, as doing so requires Apache with the mod_php module. The security it implements using .htaccess files in the files directory will be void.

Hope that saves time!

Read more about PHP’s built-in Web server.