Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving Drupal 7 with built-in PHP 5.4 server

I am looking to develop a Drupal 7 site using PHP's built-in server. I have successfully run Drupal without clean urls (e.g. index.php?q=/about/) but clean urls (e.g. /about/) normally rely on mod_rewrite or its equivalent. In the docs I see you can run the PHP server with a router file like so:

php -S localhost:8000 routing.php

What should I put in the routing.php to simulate mod_rewrite?

like image 968
Jason Christa Avatar asked Jul 11 '12 12:07

Jason Christa


People also ask

Is Drupal 7 still supported?

Drupal 7 community support is provided until November 2023 In light of the impact of COVID-19 on our community, Drupal 7 is supported until November 1, 2023. This means Drupal 7 will be supported throughout Drupal 9's life.

What version of PHP does Drupal use?

PHP versions supported Recommended? Drupal 10 requires at least PHP 8.1.

What web server does Drupal use?

Apache. Apache is the most commonly used web server for Drupal. Drupal will work on Apache 2. x hosted on UNIX/Linux, OS X, or Windows.

What is PHP in Drupal?

PHP: Hypertext Preprocessor (PHP) is an interpreted programming language that is widely used, and especially well suited for web development. Drupal and many other popular web applications are written in PHP. If you wish to develop Drupal modules or be able to do advanced Drupal theming, you will need to learn PHP.


2 Answers

The task is basically to encode Drupal's .htaccess in PHP for your router.php file.

Here's a start:

<?php

if (preg_match("/\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)/", $_SERVER["REQUEST_URI"])) {
  print "Error\n"; // 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.
  $_GET["q"]=$_SERVER["REQUEST_URI"];
  include("index.php");
}

This should be considered alpha quality. It represents a 3 minute walk through Drupal 7.14's .htaccess file, skipping anything that needed more than 10 seconds of thought. :)

It does, however, allow me to launch Drupal's install script, with stylesheets, JS and images loaded as expected, and hit Drupal's pages using Clean URLs. Note that to install Drupal in this environment, I needed a patch that may not become part of Drupal 7.

like image 177
ghoti Avatar answered Sep 29 '22 02:09

ghoti


You can now more easily launch a server with the command:

drush runserver

like image 35
maurits Avatar answered Sep 29 '22 03:09

maurits