Recently I needed to install Drupal 7 non-interactively on a Linux Server running a typical LAMP software stack. There is a lot of information out there already for this type of installation but nothing concise for what I needed to do.  Hopefully by adding this one to the pile it will help someone else who went through a similar exercise, if you found it useful please let me know!

What you get after installation with this custom install profile:

  • Drupal 7 core
  • Core + selected module(s) installed and enabled
  • A custom user role and user name passed in on the command-line

Server Setup:

For a testing sandbox it will be easiest to use a Linux box or VM that has the following installed:

  • Apache or lighttp w/ php and sqlite
  • php w/ sqlite
  • Drush v5.x

Use your OS’s packaging system to install the webserver with php and database support.

Make sure you drush is version 5.x, to install it using php pear:

pear channel-discover pear.drush.org
pear install drush/drush

Or see http://drupal.org/project/drush

Drush Make

The command “drush make” will read a makefile that has some directives to tell drush where to fetch the core, modules and themes. Here is the drush makefile that we will use that installs the core plus the Role Delegation module.


; Core Drupal
; -------------
core = 7.x
api = 2
projects[drupal][version] = 7

; Modules
; -------------
projects[role_delegation][subdir] = contrib

You can tell drush where to fetch the drupal core and the module but in this case it will get it from drupal.org. There are more powerful things you can do with the makefile, see the docs for more info.

To use this makefile and Drush to install Drupal into a web directory:

drush make /path/to/makefile /path/to/install/dir

This will download and explode the Drupal core and the module into a directory which will then allow you to run the install wizard by pointing a browser to it. What you most likely want however is to automatically install Drupal and get everything set up in one shot. If all you want to do is install the core and an admin user then you can stop reading here; use the drupal site-install command which has command-line arguments for the basic options.

Read on if you want to learn how to make additional customizations.

Drupal Install Profiles

The standard install profile is the default one that is used for a vanilla Drupal core installation. Using that profile as a starting point you can start making customizations like adding modules to the default list and you can add your own install steps. This example will create an additional user and permission role called “Content Creator.” This user will have the abilities to create new article content and some admin abilities like changing themes.

Copy the standard profile into a new one called my_profile

cd /path/to/install/dir
cp -r profiles/standard profiles/my_profile

In the “my_profile” directory, rename all references to the name “standard” to “my_profile.”

my_profile.info

The info file has key=value pairs that informs the profile which modules will be activated, add the “role_delegation” module to the list. This is necessary to activate the module on install.

...
dependencies[] = role_delegation

my_profile.profile

This file can be customized to add additional install steps. We are going to add a step to ask for a username and password for an additional user who will be given the custom “content creater” user role. Add the following php code to the end of my_profile.profile.

...

/**
 * Implements hook_install_tasks().
 */
function my_profile_install_tasks() {
  $tasks = array();

  // Add a page allowing the user to specify a "content creator" user
  $tasks['my_profile_cc_form'] = array(
    'display_name' => st('Content creator username'),
    'type' => 'form',
  );

  return $tasks;
}

/**
 * Task callback: returns the form allowing the user to add
 * a "content creator" user
 */
function my_profile_cc_form() {
  drupal_set_title(st('Content Creator Username'));
  $form['cc_uid'] = array(
    '#type' => 'textfield',
    '#title' => st('Username for Content Creator:'),
    '#description' => st('Enter the content creator userid'),
  );
  $form['cc_email'] = array(
    '#type' => 'textfield',
    '#title' => st('Email for Content Creator:'),
    '#description' => st('Enter the content creator email'),
  );
  $form['cc_pass'] = array(
    '#type' => 'textfield',
    '#title' => st('Password for Content Creator:'),
    '#description' => st('Enter the content creator password in both fields'),
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => st('Create content creator role and user'),
    '#weight' => 15,
  );
  return $form;
}

/**
 * Submit callback: creates the "content creator" role and user
 */
function my_profile_cc_form_submit(&$form, &$form_state) {
  $uid  = $form_state['values']['cc_uid'];
  $email  = $form_state['values']['cc_email'];
  $pass  = $form_state['values']['cc_pass'];

  // Create a role for "content managers"
  $c_role = new stdClass();
  $c_role->name = 'content manager';
  user_role_save($c_role);

  // additional permissions beyond what the authenticated
  // user receives
  user_role_grant_permissions($c_role->rid, array(
    'assign content manager role',
    'create article content',
    'edit own article content',
    'delete own article content',
    'create page content',
    'edit own page content',
    'delete own page content',
    'administer themes',
  ));
  $cc_user = array (
    'name' => $pass,
    'pass' => $pass,
    'roles' => array($c_role->rid => $c_role->rid),
    'mail' => $email,
    'status' => 1, # status: active
  );

  $user = user_save(NULL, $cc_user);
}

  • my_profile_install_tasks() – adds the additional install task
  • my_profile_cc_form() – specifies the custom form to get user input
  • my_profile_cc_form_submit() – runs when the form is submitted, creates a custom role with limited permissions and creates the user with this role assigned.

my_profile.install

This file has php code that will run on installation, it sets up the basic views, sets the theme, etc. This can be changed or extended in whatever way you want but for this example we will stick with the standard setup.

Once this profile is created it will be available as a new option on the install wizard:

And the new custom screen to create the “content creator” user:

There are many different form elements and attributes that you can read about in the Drupal form api documentation. For this example it would probably be better to use a password_confirm text input for the password but since the goal is to automate it from the commandline it doesn’t really matter.

Automating everything from the command-line

Now that the profile is customized instead of running through the wizard it would be nice to input everything from the command-line. To do this the drush site-install gives you the option of passing form parameters into the command line.

Here is what you would do to automate everything from the command-line where shell variables correspond to the “CC” (content creator) user information and admin account info.

(create the makefile)
drush make /path/to/makefile /path/to/install/dir
(create the new profile)
cd /path/to/install/dir
drush -y site-install --clean-url=0 --db-url=sqlite:sites/default/files/db.sqlite --account-name=$ADMIN_USER --account-pass=$ADMIN_PASS --account-mail=$ADMIN_MAIL --site-mail=$SITE_MAIL my_profile my_profile_cc_form.cc_uid=$CC_USER my_profile_cc_form.cc_email=$CC_MAIL my_profile_cc_form.cc_pass=$CC_PASS

“my_profile_cc_form” is the name of the form for the custom “Content Creator” install step and cc_uid, cc_email, and cc_pass are the parameters that entered. When that command completes you will have a fully functional site with a custom user and role.

If you something isn’t working for you I have put all of the above into a standalone shell script, simply install drush and and change the variable assignments at the top to suite your needs.



One Response to “Automating Drupal 7 Installs Using Drush and Install Profiles”

  1. A person essentially lend a hand to make significantly articles I would state. This is the first time I frequented your web page and to this point? I surprised with the analysis you made to create this actual post incredible. Wonderful task!