Installing Drupal 7 non-interactively

I'm currently chasing a wild goose of an idea in Drupal 7, and found myself in a situation where it would be really convenient to run the Drupal install script through a debugger. Normally, this won't work because the installer runs through many stages of redirect plus batch api. However, I knew I had read that there were changes in Drupal 7 to make this possible, and catch pointed out in IRC that Drush can install Drupal now. So I went over to see how Drush does it and half an hour later I had the following script. Drop it into a standalone PHP file and you're set to go.

The key to this is the function install_drupal() and the array of settings which mimcs the values of the forms you would normally submit by hand. When you pass that array to install_drupal(), then Drupal goes into non-interactive mode and installs in one page request.

So here's the code. Make a PHP file in your Drupal root directory (note it must be in a Drupal root directory or else you have to edit the DRUPAL_ROOT define), add this code, edit the details (all are pretty self-explanatory), hit it with something, and you're off to the races. Yes you can use Drush and 99% of the time that will be fine, but maybe someone will find this useful too.

EDIT: It was pointed out to me that the update_status_module settings are not particularly self-explanatory. It is a two-element array. If element 1 is TRUE, then update status is enabled. If 1 and 2 are both TRUE then update status is enabled and email notifications are turned on.

  /**
   * Global flag to indicate that site is in installation mode.
   */
  define('MAINTENANCE_MODE', 'install');

  /**
   * Root directory of Drupal installation.
   */
  define('DRUPAL_ROOT', getcwd());

  require_once DRUPAL_ROOT . '/includes/install.core.inc';

  $settings = array(
    'parameters' => array(
      'profile' => 'standard',
      'locale' => 'en',
    ),
    'forms' => array(
      'install_settings_form' => array(
        'driver' => 'mysql',
        'username' => 'myuser',
        'host' => 'myhost',
        'port' => '',
        'password' => 'mypass',
        'database' => 'mydbname',
      ),
      'install_configure_form' => array(
        'site_name' => 'my site',
        'site_mail' => 'me@me.com',
        'account' => array(
          'name' => 'admin',
          'mail' => 'me@me.com',
          'pass' => array(
            'pass1' => 'adminpass',
            'pass2' => 'adminpass',
          ),
        ),
        'update_status_module' => array(
          1 => TRUE,
          2 => TRUE,
        ),
        'clean_url' => TRUE,
      ),
    ),
  );

  install_drupal($settings);

Add new comment

I wrote two chapters of this book - Drupal 7 Module Development and I co-wrote it with Matt Butcher, Larry Garfield, Matt Farina, Ken Rickard, and John Wilkins. Go buy a copy!
I am the owner of the configuration management initiative for Drupal 8. You can follow this work at the dashboard on groups.drupal.org.

I used to work at NodeOne in Stockholm, Sweden. NodeOne is the largest pure Drupal consultancy in Europe. They have built websites for clients like IKEA, SFBio, and Möbler. If you need some work done get in touch!