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 , the script below makes it easy to create sub-sites like and . This uses drupal’s multi-site feature.

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

sudo /path/to/ student1

Of course replacing ‘student1’ with whatever site name you want to use, and then you or your student/user can visit
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/ or sites/ 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.

##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/ 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


#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 ]
  echo "Usage: sudo $0 directoryname"
  exit 1

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

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

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


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

echo "Copying settings.php"
if [ ! -f $OLDSETTINGS ]
  echo "Cannot find default settings.php file: $OLDSETTINGS"
  exit 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 918 other followers
Follow EdTechDev on
%d bloggers like this: