How to Setup Drupal for Local Development with Just PHP

Advertisement

Advertisement

Introduction

With older versions of Drupal you could not run Drupal without Apache or Nginx to perform the URL rewrites needed to use the application with clean URLs.

New versions of Drupal (8+) come with a .ht.router.php file that is specifically used to run local development instances with just the built-in PHP web server. No Apache, no Wamp, no Mamp, no Xampp. Just the built-in PHP server.

In addition, you don't have to use MySQL an setup a separate database. Drupal supports SQLite3 so can completely skip any MySQL setup.

This means you can run a local Drupal development environment with nothing but PHP.

  • No Apache
  • No MySQL
  • No Wamp,Xampp,Mamp
  • Just PHP

Following the method in this guide works on Windows, Mac, and Linux equally.

Setup Drupal

There are a few easy steps for this procedure:

  • Check PHP configuration - make sure you have the right modules
  • Run the built-in PHP web server - point it at the router file
  • Configure Drupal - run through the install/setup wizard in the browser

Check PHP configuration

You will need to make sure your PHP has the SQLite PDO module. You can see what modules are enabled with php -m.

# List enabled modules
php -m

You should see these two entries somewhere in the list:

PDO
pdo_sqlite

If you don't see it in the list, you can check in your php.ini file for the pdo_sqlite extension. Make sure the line does not begin with a semicolin (;) or it means the extension is commented out and disabled. I also recommend turning on curl and openssl since they are so commonly used.

extension=pdo_sqlite

Download

Download the latest version from https://www.drupal.org/project/drupal.

You will also need to extract the .zip or .tar.gz file. For Windows, use the .zip version, or download a tool like 7-Zip or WinRAR to handle .tar.gz.

Run

Navigate in your terminal to the directory where Drupal resides. Then start the built-in PHP web server, pointing the router option to the .ht.router.php file provided by Drupal.

php -S localhost:9999 .ht.router.php

Configure

In your browser, visit http://localhost:9999/. If you have not configured Drupal yet, it should present you with the setup screen. It will let you know if you have any missing PHP modules first, then it will let you configure the database. Choose the SQLite option. Fill out the final information like the admin user account and you are all done.

Visual Studio Code Task

If you want to create a simple VSCode task to run this, you can create a tasks.json in your projects .vscode directory. Here is an example:

{
    // ${workspaceFolder}/.vscode/tasks.json

    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "run php server",
            "type": "shell",
            "command": "php -S localhost:8000 .ht.router.php",
            "options": {
                "cwd": "${workspaceFolder}/drupal"
            } 
        }
    ]
}

To run it, press CTRL-SHIFT-P to pull up the Command Palette and run >Tasks: Run Task and choose run php server.

Conclusion

After reading this guide you should understand how to setup a simple local development environment for Drupal 8+ using just PHP without MySQL or Apache. This is not a proper environment for production but it good for development.

References

Advertisement

Advertisement