Friday, 24 May 2013

Set-up Multisite Drupal 7

In order to develop my website using Drupal I wanted a local version to work on off-line and a live version to periodically update with releases.  This post continues to tell how I achieved that by describing how to create a multi-site drupal installation having completed setting up Drupal 7 and AMP on my Mac OSX Lion.
Part 1: Local Drupal7 on Mac OSX Lion
Part 2: Set-up Multisite Drupal 7
Part 3: Using Drush to sync live and dev Drupal websites
Live website: www.chanzachanzo.com/ points to my drupal installation on a cloud server having set it up as described in my previous post.

Drupal was installed on Mac OSX Lion so that:
  • http://localhost/ pointed to Apache "It Works!" page at /Library/WebServer/Documents/index.html
  • http://localhost/~mattmapleston points to a Mac default user page (image below) at /Users/mattmapleston/Sites/index.html
  • http://localhost/~mattmapleston/drupal/ points to default drupal page (second image below)


I want to set up another local drupal website arbitrarily called c2.dev to mirror the live www.chanzachanzo.com.  The purpose of this second site was to allow me to updated it offline and play around with settings and code without stuffing up the live site.  Having two sites meant that I would always have the clean default site to fall back on if anything went wrong.

Step 1: Create new mysql database

Similarly to setting up a live drupal site (but perhaps in a more logical order) create a new mysql database for c2.dev called c2devdb and install my d7user with full access
mysqladmin -u root -p create c2devdb
mysql
mysql> GRANT ALL PRIVILEGES ON c2devdb.* TO d7user@localhost IDENTIFIED BY '<pwd>';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> EXIT
Bye

Prepare settings.php

Make a copy of the default folder called after the new web page i.e. c2.dev then refresh the settings.php file
cd ~/Sites/drupal/sites/
mkdir c2.dev
cp default -a c2.dev
cd c2.dev
rm settings.php
cp default.settings.php settings.php
chmod 777 settings.php

Add the following to the new c2dev/settings.php to link it to the database just created.
$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'c2devdb',
  'username' => 'd7user',
  'password' => 'password',
  'host' => 'localhost',
  'prefix' => '',
);

chmod 655 settings.php

Alter sites.php to include the new site:
cd drupal/sites/
If it doesn't already exist
cp example.sites.php sites.php
and at the end add
$sites['c2.dev'] = 'c2.dev';

Next bit is to redirect a URL to this folder.  I found 3 ways to do this and after some experimentation went for the third way of using httpd-vhosts.config.  I recorded the three ways as it is useful information.

Experiment 1: Redirecting localhost
cd /etc/apache2
sudo nano httpd.conf
change
DocumentRoot "/Library/WebServer/Documents"
to
#DocumentRoot "/Library/WebServer/Documents"
DocumentRoot "/Users/username/Sites/"

and change
<Directory "/Library/WebServer/Documents">
to
#<Directory "/Library/WebServer/Documents">
<Directory "/Users/username/Sites">

ctrl-o and enter to save, ctrl-x to exit
sudo apachectl restart
In a browser localhost will now point to your user site or whatever you set DocumentRoot and Directory to.  I didn't think this would work with a drupal mutli-site installation so I moved on.

Experiment 2:
Alias /c2dev /drupal/chanzachanzo.com/ somewhere in httpd.conf
localhost/c2.dev would then post to localhost/drupal/c2.dev
Useful documentation from apache.org: Mapping URLs to Filesystem Locations
I haven't got round to trying this out because it's not quite what I wanted.

Experiment 3:
Adding a virtual host from a superuser.com answer
I want the URL http://c2.dev to point to go to the c2.dev website i.e. the local version of the live website.  To make this so:

edit /etc/apache2/httpd.conf to uncomment Include /private/etc/apache2/extra/httpd-vhosts.conf
edit /etc/hosts to add 127.0.0.1 c2.dev
edit /etc/apache2/extra/httpd-vhosts.conf to add:
<VirtualHost *:80>
  DocumentRoot "/Users/username/Sites/drupal"
  ServerName c2.dev
</VirtualHost>
then finish off with
sudo apachectl restart

Now the URL c2.dev goes anywhere I set it, in this case the top of the drupal folder.  I assume that from there some magic looks at sites.php and forwards me onto the right folder.

The documentation from drupal on setting up a multi-site installation was pretty useful once I'd read it a few times.

I now have set up two drupal websites on my laptop: Default drupal at URL localhost/~mattmapleston/drupal and c2.dev website at URL c2.dev. Both are identical at this point.

The next post will describe how to copy the live website to the local drupal installation using Drush.