Bash Script to Quickly Create Drupal Sub-sites

I haven’t tried this for real yet, only tested it with a local xampp-based drupal site, but here’s a shell script that quickly creates drupal sub-sites, say, for students. The sub-sites use the same database as your main drupal site, but each has its own table prefix.

So basically if you already have drupal installed, say at http://your.drupal.host/ , the script below makes it easy to create sub-sites like http://your.drupal.host/student1 and http://your.drupal.host/student2 . This uses drupal’s multi-site feature.

You just copy the below script to a file and make it executable (chmod 700 makesite.sh), edit some of the settings in it (like change “nobody” to “www-data” if using debian/ubuntu, and “localhost” to “your.drupal.host” or whatever),
and then on the command line change (cd) to your drupal root folder and run:

sudo /path/to/makesite.sh student1

Of course replacing ‘student1’ with whatever site name you want to use, and then you or your student/user can visit http://your.drupal.host/student1/install.php
to finish the drupal installation.

Of course, backup your main site’s database before using this (see the backup and migrate module).

For other related options see aegir, densite (both of which use separate databases per site), and domain (in which multi-sites not only share the same database but the same tables as well).

This script doesn’t handle sub-sites with their own domain names (see densite or aegir instead), and it doesn’t create cron jobs for each drupal site (see densite or just enable poormanscron on each site). Of course any modules or themes installed under sites/all/modules or sites/all/themes can be shared by all the sub-sites, so you only need to install them once. If you want a module or theme just for one particular subsite, install it under sites/your.drupal.host.student1/modules or sites/your.drupal.host.student1/themes or whatever the names are. Those sub-folders and the files folder are already created for you with the right permissions by the script.

I’ll also probably be using modules like theme_editor and zenophile so that students won’t need command line or ftp access to edit the look of their sites.

#makesite.sh

##About this script:
##This script will create a drupal sub-site using table prefixes.
##It assumes you already have a main drupal site running,
##and can be used to create sub-sites, such as:
##   http://yoursite/subsite
##The database tables will have prefix: subsite__
##The script also assumes that your main drupal site has
##no prefix ''.  If that's not the case, edit the sed line below

##To run it:
## -First change the options below (apache user, site host)
## -cd into your root drupal folder (cd /var/www, etc)
## -run: sudo /path/to/makesite.sh subsitename
##    'subsitename' should have only have letters/numbers
##    also it should not duplicate an existing sub-folder
## -then visit http://yoursite/subsitename/install.php
##    to finish the drupal install

##### CHANGE THESE 2 or 3 TO MATCH YOUR CONFIG: #####

#what user does apache2/httpd run under:
APACHEUSER="nobody"  #www-data on debian/ubuntu

#what is the root hostname/url of your main drupal site:
SITEHOST="localhost"  #root hostname for main drupal site

#where to find the settings.php we'll base sub-sites on:
DEFAULTFOLDER="default" #main sites/default/settings.php file

#########Stuff below shouldn't need editing:########

if [ $# -ne 1 ]
then
  echo "Usage: sudo $0 directoryname"
  exit 1
fi

if [ $(whoami) != "root" ]; then
  echo "You need to run this script as root."
  echo "Use sudo $0 directoryname"
  exit 1
fi

if [ -d $1 ]
then
  echo "$1 already exists, please use a different name"
  exit 1
fi

echo "Creating symlink from $1 to ."
ln -s . $1

NEWSITEDIR="sites/$SITEHOST.$1"

echo "Creating sub-site folders at $NEWSITEDIR"
mkdir $NEWSITEDIR
mkdir $NEWSITEDIR/files
mkdir $NEWSITEDIR/themes
mkdir $NEWSITEDIR/modules
chown -R $APACHEUSER $NEWSITEDIR

echo "Copying settings.php"
OLDSETTINGS="sites/$DEFAULTFOLDER/settings.php";
if [ ! -f $OLDSETTINGS ]
then
  echo "Cannot find default settings.php file: $OLDSETTINGS"
  exit 1
fi

NEWSETTINGS="$NEWSITEDIR/settings.php"
cp $OLDSETTINGS $NEWSETTINGS
chown $APACHEUSER $NEWSETTINGS

PREFIX="$1__"
echo "Using table prefix: $PREFIX"
#drupal 7:
sed -i "s/'prefix' => '',/'prefix' => '$PREFIX',/" $NEWSETTINGS
#drupal 6
## sed -i "s/\$db_prefix = '';/\$db_prefix = '$PREFIX';/" $NEWSETTINGS

echo "FINISHED: Visit http://$SITEHOST/$1/install.php to finish"

Faculty developer. Interests: developing educational technology; faculty & student development; learning sciences & psychology.

Posted in drupal
Doug Holton

Doug Holton

Faculty developer. Interests: developing educational technology; faculty & student development; learning sciences & psychology.

View Full Profile →

Join 5,342 other subscribers
Follow EdTechDev – Doug Holton on WordPress.com