Wednesday, September 23, 2009

Installing multi-site Drupal from CVS using Drush

These are my prettified notes on installing multi-site Drupal setup, using swiss army knife for all things Drupal - drush (DRUpal SHell) on Ubuntu 9.04 (Jaunty). This post will cover quick LAMP, CVS and phpMyAdmin installation and then show how easy it is to get latest stable Drupal, plus over a dozen of modules, up and running with drush. If you care, just follow along.

First we need to set up our Drupal environment. We'll need LAMP (Linux, Apache, MySQL, PHP) and CVS to checkout Drupal core and contributed modules.

LAMP and CVS installation

Install Apache:

# apt-get install apache2 apache2-mpm-prefork

apache2-mpm-prefork package is required by PHP.

Install MySQL:

# apt-get install mysql-server

Decide on and enter your MySQL administrator password when prompted.

Install PHP:

# apt-get install php5 php5-cli php5-dev php-pear

Since I'm planning to use drush, I'll need the php5-cli package. Packages php5-dev and php-pear are optional, but are likely to be required if you need to install PHP extensions and classes that are not already prepackaged for Ubuntu.

Install CVS:

# apt-get install cvs

Install phpMyAdmin (optional):

# apt-get install phpmyadmin

During installation select apache2, allow installer configure database for phpmyadmin with dbconfig, enter MySQL admin password and leave field blank for phpmyadmin password.

Drush installation

Check out drush from CVS:

# cd
# cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal-contrib \
    checkout -r DRUPAL-6--2-0 \
    -d drush contributions/modules/drush

This is the only time I had to manually use CVS, all other checkouts / updates will be done by drush.

I moved drush directory to more central location in /usr/lib/drush and created a symlink so it is in path:

# mv drush /usr/lib/
# ln -s /usr/lib/drush/drush /usr/bin/drush

Create drushrc.php file to avoid typing a long list of arguments for every command:

# cd /usr/lib/drush
# cp example.drushrc.php drushrc.php

Then uncomment line containing: $options['package-handler'] = 'cvs'; in drushrc.php to tell drush to use CVS.

Install Drupal

Setup a workspace and install Drupal in home directory:

$ cd
$ mkdir workspace
$ cd workspace
$ drush dl drupal

Now that we have Drupal downloaded in ~/workspace/drupal, create first site in our multi-site setup called my-site-name.lan:

$ cd ~/workspace/drupal/sites
$ mkdir my-site-name.lan
$ cp default/default.settings.php \
    my-site-name.lan/settings.php

We need to manually create database for our Drupal site either with phpMyAdmin or by hand. Here's how to do it by hand:

$ mysql -u root -p
Enter password: your_mysql_admin_password
mysql> create database dbname;
Query OK, 1 row affected (0.00 sec)

mysql> grant usage on dbname.* to dbuser@localhost identified by 'dbpassword';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on dbname.* to dbuser@localhost ;
Query OK, 0 rows affected (0.00 sec)

mysql> quit

Make sure to write down / memorize your dbname, dbuser and dbpassword, you will need them later during Drupal web install.

Now add the virtual host configuration file for Apache /etc/apache2/sites-available/my-site-name.lan and insert the following:

<virtualhost *:80="">
    ServerAdmin root@localhost
    DocumentRoot /home/username/workspace/drupal/
    ServerName www.my-site-name.lan
    ServerAlias my-site-name.lan *.my-site-name.lan
    RewriteEngine On
    RewriteOptions inherit
    CustomLog /var/log/apache2/my-site-name.lan.log combined
</virtualhost>

Don't forget to replace "username" and "my-site-name.lan" with actual values. By the way, I'm using .lan because that's what I chose for my local domain, .local is another common option.

Also, to make Drupal web installation work we need to allow Apache to create some default directories and write to Drupal's config file. So I changed the group permissions on my sites directory like so:

# cd ~/workspace/drupal
# chgrp -R www-data sites
# chmod -R g+w sites

Now to enable this virtual host and reload Apache:

# a2ensite my-site-name.lan
# /etc/init.d/apache2 reload

We should be able to access our Drupal installation at http://my-site-name.lan, that is if "my-site-name.lan" resolves to your LAMP server, of course.

Follow the web installation and don't forget to remove write permissions from drupal/sites/my-site-name.lan and drupal/sites/my-site-name.lan/settings.php after it's complete:

# cd ~/workspace/drupal
# chmod g-w sites/my-site-name.lan/settings.php
# chmod g-w sites/my-site-name.lan

Install common modules

Since I find myself installing the same set of modules on all my Drupal setups, I chose to put them into drupal/sites/all/modules. To do that with drush I just need to be in drupal root directory:

$ cd ~/workspace/drupal
$ drush dl cck views email phone link token pathauto date \
    mollom globalredirect transliteration wysiwyg spamspan \
    admin_menu cvs_deploy menu_breadcrumb jquery_ui imageapi \
    filefield imagefield imagecache backup_migrate \
    auto_nodetitle

Although you *could* also enable modules with drush, because of the huge number of submodules, I find it is faster and easier to do via web interface.

With all these modules Drupal will be using quite a bit of memory. It is best to up PHP's memory_limit value in /etc/php5/apache2/php.ini to at least 32M.