Multi-sites is a Drupal feature that makes managing many websites easier.
The idea with multi-sites is that you can use a single codebase for more than one website. When you update that single codebase, you update all of your Drupal sites. Each site can still have its own theme, modules and content.
With multi-sites you can have many sites with one database, or many sites with different databases. In this tutorial we will chose to have multiple sites, each with their own database.
We’re going to set up one core installation, and share those files with two sub-sites. Each sub-site will share those files but have its own URL and its own database. In this tutorial I’m going to set up two sub-sites with different URLs:
- Master site: http://easywebupdate.com
- Sub-site 1: http://edandrea.com
- Sub-site 2: http://edsparty.com
Setting up multi-sites is not an easy task for beginners. To make it easier, we’re going to show you how to use your hosting account with cPanel to get this done more easily. If you aren’t using a commercial host with cPanel, your setup process will be slightly different.
Step 1. Choose your master domain name
Your first step is to hoose the URL that will host your main site. As we mentioned, in this example, our main URL will be http://easywebupdate.com. This doesn’t have to be an important URL. Unless you decide you want it to, this one won’t be used by anyone but you. It’s only going to hold the master copy of your Drupal installation. It is worth nothing however that this site might be visible if one of your sub-sites fails. So it would be a good idea if your master site had a page with your contact information and a message.
Step 2. Start out by creating databases for all the sub-sites
- Go to MySQL in cPanel or use the database wizard to create databases for each site you want to add.
- Be sure to add database users with full privileges to each database. If you want to maximise security, create a new database user for each database. In the example below, I named the databases the same as the URLs. This is recommended on a live site, but it does makes it easy for me to refer to them in the tutorial.
Step 3. Install Drupal in your master URL directory
- Install Drupal in your /public_html/ directory.
- Visit the URL (in this case http://easywebupdate.com) and step through the Drupal installation.
- Use the master database connection information when the install script asks for it.
Step 4. Create a sub-folder for each sub-site.
- Create a folder under public_html/sites/subsite.com. Substitute the URL you are going to use.
- Create one folder for each sub-site.
- Now copy default.settings.php to each one.
- CHMOD those files to 644 if they aren’t already.
- If you are doing multi-sites for Drupal 6, you should also create an additional folder called “files” and make sure it’s CHMOD 755.
Step 5. Upload or copy default.settings.php to the new folders
You now need to add default.settings.php to your new folders. The image below shows how file structure looks in Filezilla that makes it a little more obvious.
- If it is Drupal 6, create a files directory with the path sites/domain.com/files/. You don’t need this extra directory here for Drupal 7.
- Copy or upload default.settings.php to the folder.
- Rename it to settings.php.
We are now finished with the Drupal master site. The next part is more complicated because we have to make sure the domain names are correct. Luckily cPanel can do this easily for beginners. We’ll show you that method first. There is a manual method, but it involves making changes in Apache files and this is probably beyond beginner-intermediate skills required of this tutorial.
Step 6. Return to cPanel
- On the main cPanel dashboard, find the Domains section and click Parked Domains.
Step 7. Add the domain
- Park the domains on top of the master domain.
- When you park a domain, the domain needs to be pointed at the DNS servers for your hosting account. If you don’t know how to do this, the proper place to get information is from the domain name registrar. If you don’t know who your registrar is, you get that information your hosting company.
Step 8. Go to your browser and visit the URL
- In your browser address bar, type the url to your second or third site.
- Install Drupal by following the prompts.
Step 9. Connect to the proper database
- When prompted, enter the database information for this specific site.
Three sites, each with their own theme, each with their own modules, all on the same hosting account. To create another site, I just repeat the process. Using cPanel’s parking feature, I was able to avoid the need to make changes to Apache, and the need to make symbolic links or redirects in the .htaccess file.
There is another way to do it that is almost as simple, but if you want to try it you will learn a little more since it involves creating a symbolic link. We’ll give you the code to paste into your site and walk you through it.
Option 2: Quick ways to set up Drupal multi-sites with cPanel Sub-Domains
Go through Steps 1 through 3 of this tutorial. Everything is the same up to that point. In Step 4 you take a slight detour and so Option 2 starts from that point on.
Option 2, Step 4: Create directories for each domain with the convention subdomain.domain.com
- Create folders for each site you want to create.
- Create the folder names with this convention subdomain.domain.com.
- Upload or copy default.settings.php to each directory.
- Change the name of each copy to settings.php.
- Return to cPanel home.
Option 2, Step 5: Create subdomains
- Go to the Domains section and click Subdomains.
The correct directory structure is pictured below:
- The directories created by cPanel.
- The directories created by you.
Option 2, Step 6: Write a simple php script to create a symlink
In a script editor create a file called mysymlink.php and put the following code into it.
<?php symlink( '/home/cPanel_User_Name/public_html/', 'subdomain' );
In my case the file would look like this
Option 2, Step 7: Upload the symlink script to the main directory
To run it go to your browser address bar and type in your url and the name of the file.
If all goes well you will see a completely blank page. If you made a mistake in the file you will see error messages.
Option 2, Step 8: Add redirection to your .htaccess file
- Open your .htaccess file in a script editor
- Find the line # RewriteBase/ and remove the # sing.
- Add the following line and save the changes
RedirectMatch 301 ^/subdomain/(.*)$ http://subdomain.yoursite.com $1
Here’s what my .htaccess looked like when finished:
RewriteBase / RedirectMatch 301 ^/edandrea/(.*)$ http://edandrea.easywebupdate.com/$1 RedirectMatch 301 ^/edsparty/(.*)$http://edsparty.easywebupdate.com/$1
This redirection will make it so the site can be accessed in two ways.
http://easywebupdate.com/edsparty and http://edsparty.easywebupdate.com will both be redirected to the same site.
Option 2, Step 9: Visit your subdomain and complete the installation
If you did everything right, you can visit your subdomain and you will now be asked to install Drupal in your subdomain.
More on Drupal Multi-sites
What you’ve seen in this tutorial is just the beginning of the possibilities with Drupal Multi-Sites. There are many optional ways to arrange the code and the data:
- Multiple domains or vhosts using different databases.
- Multiple domains using the same database.
- Multi-site setup using a single Drupal instance.
- Same codebase, completely different content and users.
One security concernt to note is that Drupal’s multi-site feature is normally used in situations where the administrators for all of the sites are highly trusted. The reason is that anyone with full administrative privileges on a Drupal site can execute arbitrary PHP code on that site through various means, even without FTP access to the site. That arbitrary PHP code could be used from one site to affect another site if the two sites are in the same HTTP document root and share the same Drupal codebase. For more information: http://drupal.org/node/476544 and http://drupal.org/node/1244642.