Multi-site Drupal Installations

Hey, first real post! It's about time! For our first post, I'll be talking about one of my favorite platforms for web development: drupal.

Here at FC, drupal is our CMS of choice. One great feature of drupal is its ability to host multiple sites from a single installation.

If you need multiple drupal sites, having them run off of a single drupal installation will seriously cut down on maintenance time. Drupal is updated frequently, and most of the changes are security-related, so running an old version of drupal can have serious security ramifications. But, updating drupal can be kind of a pain - you have to back up your databases, backup the sites folder, take your site offline, etc. If you have even just three or four sites running on separate drupal installations, you're looking at a significant time investment that you may well have to repeat in only a few weeks. But, if you're running those three or four or ten or however many sites you want on a single installation, you can have ALL of your sites updated in the time it takes to update one.

Another advantage to running a multi-site drupal installation is that you can share modules/themes/etc between sites from a single location. On a single site installation, any extra modules or themes go into the sites/all folder, on a multisite installation, modules and themes in this folder are available to all of your sites. This ties back in to the maintenance issue - having a single shared copy of a module/theme will make installation, back-ups, and updating a lot faster and easier.

I'm not going to go into a lot of detail about how to set up a multisite installation, as it is covered in great detail elsewhere (a good place to start: http://drupal.org/getting-started/6/install/multi-site) but the basic steps start out the same as for a single-site drupal installation:

1. Download latest drupal version and unpack it somewhere on your webserver.
2. Set up a database

Now, it diverges a bit (note - these next steps have to be repeated for each new site):

3. In the sites folder, create a new folder with the name of your site (ex. sites/example.com, see link above for naming information)
4. Copy sites/default/default.settings.php to the new folder
5. Rename sites/example.com/default.settings.php to sites/example.com/settings.php and make it writable by php (777 is generally fine as long as it is temporary. In other words, don't forget to change it after installation to 444 [read-only]).

Nothing too difficult so far, right? Well, the next step is where multi-site installation trips most people up.

6. If you want to use a subdirectory as a separate drupal site, you have to create a symbolic link pointing to the drupal installation folder.
7. Visit the new site location and continue normal drupal installation process.

In step number six, for example, if you have a drupal installation in example.com/drupal, and you want a different subdirectory to have a separate drupal site such as example.com/shop, you have to create a symbolic link named shop/ that points to drupal/.

Confused yet? Well, unless you have experience working with Unix or Unix-like operating systems (linux, BSD, Solaris) it's very unlikely you know what a symbolic link is in the first place, so let's start there. A symbolic link is a special kind of file that points to a different file or folder. Symbolic links are good for reducing repetition of files. So, if you have a file that you want to be in two different places, instead of copying the file to the second place, you can instead create a symbolic link pointing to the first file. This is exactly like having one file (or folder) in two places! (Read more about symbolic links.)

So, when you create a symbolic link that points to the drupal installation, it's the equivalent of having the exact same drupal installation in two different places!

Creating a symbolic link is easy if you have shell access on your server, but most shared hosting doesn't allow shell access for security reasons. So what if you have shared hosting and can't afford to run your own web server or upgrade to an expensive dedicated server? PHP to the rescue! PHP has a built-in function that will create a symlink for you - aren't you lucky? Yes, you are! And, if you aren't comfortable with PHP, you're still lucky! The nice people here at FC have made an easy to use script that will handle symlink creation for you without you having to get into any code whatsoever!

All you have to do is upload the script to the public html directory on your web server, rename it to create_symlink.php (no txt), then visit it with the browser of your choice. It will work on Linux hosts running PHP 4 and higher and now it will work on Windows Vista hosts running PHP 5.3 or higher. CAUTION: this script will make changes to your server filesystem and is accessible from the interwebs. Make sure it is in a secure area or set file permissions to 000 (no permission) after you're done using it.

By the way, this script is open-source, GPL! So have at it - make it better, translate it, expand it, incorporate it in something else, have fun! If you do, it would be really really really really (did I mention, really) nice for you to post a comment and let us know!

AttachmentSize
create_symlink.php.txt6.26 KB

Useful info and written in a playful style.

First, thanx for the script.
Second, do you know of any better method for updating Drupal than reinstalling? While it isn't difficult for one site, backing up multiple db can be a pain. Have you heard any rumblings about dynamic updates that don't require the site going down for long periods?

Thanks again for the script!

Dean

RE: Useful info and written in a playful style.

Hey, thanks for the comment!

Unfortunately, the nature of the web server/client environment doesn't really lend itself to dynamic updates. I would be extremely leery of making major code base changes to a live site. If something goes wrong, everyone will know, and if you don't have a backup of your db and files, you're looking at rebuilding a site from scratch which could be devastating.

Actually, updating a drupal site doesn't really require the site to go down for a long time. You only need to take your site down while you copy over the new files and run the update.php script. Assuming nothing goes wrong, it shouldn't be down for more than ten minutes.

Also, if you have a lot of databases (or even just one) you should really be backing them up regularly anyway. I would suggest at least a nightly cron job to back up your databases. If you have an active site with a lot of critical data, hourly might be better - just make sure your server can handle it.

Hope this helps!
Emily