Drupal multi-site without subdomains
Setting up a multi-tenant Drupal using subdirectories instead of subdomains.
Drupal supports multi-sites, this means you can install Drupal once and use it for multiple websites (also known as single codebase). This kind of setup will make it easier to maintain your Drupal installation or when you want to upgrade to a newer version as you are only supporting a single codebase. The most common setup is the subdomain approach: each website has its own subdomain. Suppose you want websites for different projects. The layouts and requirements may be different, but they will all run on Drupal. The URL’s will look like this:
http://project1.example.comhttp://project2.example.com
There are tons of tutorials on the Drupal site to create this setup but that’s not what we are looking for. We want a setup with multiple websites in subdirectories, not subdomains:
http://www.example.com/project1http://www.example.com/project2
It’s a little harder to find how to get this done, let’s have a look.
Install Drupal
- Start with installing Drupal as you would normally do. Download the latest version and extract it in your server’s document root (probably ‘/var/www/html’).
- Create a database and database user for this installation but call it something like ‘project1’ as this will be the database for the first project.
- Configure your installation via the web interface.
That’s it, you have your first installation up and running, nothing new here.
Add another instance
- Create a second database and call it ‘project2’. You can use the same user as for the first database but it’s obvious that this would be a bad choice for security reasons.
- Make a copy of the database for the second project (add the -u and -p parameters twice if needed).
mysqldump --opt project1 | mysql -h localhost project2 - Now create the folder structure in your <docroot>/sites directory as explained on the Drupal website. Be careful not to call the directories ‘example1.com’ and ‘example2.com’ as explained in the document but:
example.com.project1
example.com.project2 - And now the magic, add symlinks for each project in your document root:
ln -s <docroot> project1
ln -s <docroot> project2
Now point your browser to http://www.example.com/project1 and you should see the first site, http://www.example.com/project2 will get you to the second one. At this point both sites will be identical (you copied the database remember?) but you can start changing the contents independently of each other now.