There are many reasons why a systems administrator or developer may choose to host multiple sites on the same server in a multisite configuration. For example, such a configuration could:
- Enable you to build an efficient development environment
- Allow you to stage and test a site before it goes public
- Let you run different software on the same site
- Enable sharing of a single code base across several sites
- Leverage shared hosting
- Allow you to run similar secure (HTTP over Secure Sockets Layer [HTTPS]) and standard (HTTP) sites on the same server
- Ease server administration
Not all of these reasons, however, are best addressed with Drupal's multisite feature. This articles focuses on just one strategy: how to use Drupal's built-in multisite support to create and serve several sites from one instance of Drupal.
Using the Drupal content management system (CMS) to create a multisite configuration has several distinct advantages, one of which is that it is probably the easiest configuration to set up and maintain. Another is that data duplication is minimized because Drupal is installed in only one place on your server. Finally, Drupal has a wealth of contributed modules that can be used to augment multisite handling, offering advanced capabilities to all the sites in the arrangement. The combination of added features and simplified administration would be a boon for anyone wanting to set up and administer a multisite arrangement. Nevertheless, there are a few drawbacks to using such a configuration, and two in particular deserve clear mention: security and sharing.
The sites in a multisite installation share the same code and thus the same files. As a result, anyone who gains access to the server's file system for one site also has access to all the files used for all of the sites on that multisite installation. Therefore, in cases where different people are managing different sites within the arrangement, it may not be quite secure. If security among sites is a primary issue, or if different people manage the various sites, Drupal's built-in multisite functionality may not be the best solution. Instead, consider giving each site its own distinct Drupal instance. Although the advantages of easier administration that the multisite arrangement affords will be lost, your sites will be better protected from mishaps.
The second concern — sharing — centers around database access. Even though Drupal's multisites share the same source code, they do not share the same database data. Thus, nodes, users, and comments in one site are not accessible by default from other sites. This behavior can actually be regarded as a good thing, but in situations where the administrator may require that all the sites share information, it may be a limitation for the design of Drupal's multisite feature. Nevertheless, the sweet spot for Drupal's built-in multisite support is that modules, themes, and, even to some extent, uploaded files, can be shared across all the sites. In addition, the same or custom files can be retained in different folders corresponding to the different sites in the configuration. If it's important that all content be shared, consider using a multidomain feature, which is easily provided by a module of the same name.
Installing Drupal for multisite use
After downloading the Drupal archive (see Resources for a link), extract the file and copy the resulting
folder to the document root of your web server. This task isn't difficult
with a suitable FTP client; with Secure Shell (SSH) access to your server,
the task is even easier. Simply run a
command to download the archive file from the repository, and extract it
to produce the Drupal installation folder. (If your development
environment is local, stand by for more options.) Name the installation
Three different sites will be created for this exercise; therefore, three different databases are needed — one for each site. Using phpMyadmin from the Cpanel of your server, this task is easily accomplished. Next, install the framework. Two tasks must be performed, however, before using your web browser to run the installer:
- Provide Drupal with the information it needs about the sites you intend to run.
- Pass the site-specific configuration data from Drupal to all of the sites.
To achieve the first task, create a special directory for each site. For the second, locate a settings file in each directory. The objective is to set up one master installation with two sub-sites. Each sub-site shares the same files as the core, but each will have its own separate URL and database. The URLs of the sites to be created are:
- Master site: http://goodsite.com
- Sub-site 1: http://bettersite.com
- Sub-site 2: http://bestsite.com
Installing the first host
After placing the Drupal source code in the document root and having created the MySQL databases, begin installing the first site and the master domain.
The master domain
The master domain name is the URL from which the main site will be accessed (in this article, http://goodsite.com). The primary purpose of the master domain is to host the base Drupal installation. Regardless of whether this domain is used as one of your sites, if any sub-sites fails, their URLs will default to the master domain; therefore, it is a good idea that this site contain information relevant to the sub-sites, such as contact information.
As mentioned, a subfolder must be created within the main Drupal folder for each sub-site. What many would imagine the most difficult task in Drupal's multisite installation is simply routine: Let Drupal know what sites you want to host, and simply create subfolders for those sites in the /sites folder.
In the Drupal main root folder is a directory called /sites. If you've administered Drupal before, you know all non-core code ought to reside within this folder. In the default Drupal installation, the /sites folder contains two directories. The first, called all/, is where you should put downloaded themes and modules. The all/ directory is the location for information shared across all sites. The second folder, named default/, is where configuration files should be kept, as well as non-core and custom themes and modules. Drupal typically searches the default/ directory for site configuration information unless it is instructed otherwise. Note, however, that these are assumptions for a generic, single-host site — not necessarily for a multisite installation.
Creating the subfolders and files
A big tip for building multisite Drupal is that apart from the all/ and default/ folders, other folders can be created in /sites, and Drupal will assign them special meaning. More specifically, Drupal assumes that all other directories found in the /sites folder represent a hostname or a hostname pattern.
You create a subfolder in the pattern
/goodsite/sites/<sub-site> under the
/sites folder for all sub-sites, substituting your intended URL for the
name of each sub-site folder. In this example, you have the following
sub-sites, as shown in Figure 1.
- Sub-site1: /goodsite/sites/bettersite
- Sub-site2: /goodsite/sites/bestsite
Figure 1. The /sites folder hierarchy
For each sub-site, complete the following steps:
- Copy the file default.settings.php from sites/default to each sub-site
folder, and rename it
CHMODto set the permission on settings.php to 644, if it is not the current permission.
- For a Drupal, Version 6 installation, create a folder named
filesunder each sub-site, and use
CHMODto change the permission to 755.
By default, Drupal looks for a subdirectory named files/ within the directory of every site. In a default Drupal installation, this folder would be in sites/default/files/.
- Create a folder called
modulesfor each sub-site.
This is the folder in which any modules specific to that sub-site reside. Any modules that all sites will use should reside in the sites/all/modules folder.
- Create a folder called
themesfor each sub-site.
This is where any themes unique to that sub-site are placed. Any theme that all sites will use should reside in sites/all/themes.
Now, point the sub-site URLs to your web server, if they are not administered by the same hosting company. If you use different hosts for the various sites, you will need to change the Domain Name System (DNS) information (both the registrars and the hosting companies should be able to tell you how to do so). Thereafter, you will have to "park" that domain on top of your primary domain — in this case, goodsite.com. From this point, you have a few alternatives, the more difficult of which involves using what's called a symlink. Parking is much simpler, however; so in your Cpanel, click on Parked Domains. Add bettersite.com and bestsite.com in New Domain Name, and then click on Add Domain.
Creating the multisites
There are several ways to go from here, but again, let's take the easiest route: directly loading the database for each sub-site with the tables created in the master site installation. To do so, you must create a dump of the database tables from the master site installation and save it locally. Then, either upload this file with phpMyadmin or open it using a code editor. To use the second option, perform these tasks within phpMyadmin:
- Go to the database created for the sub-site.
- Go to SQL, and then paste all of the code from the Structured Query Language (SQL) dump there.
- Click on Go.
This procedure reproduces the master site database tables in the sub-site database.
Next, open the settings.php file for each sub-site, and look for the following line:
$db_url = 'mysql://username:password@localhost/databasename';
Change this database connecting string to reflect the parameters for the sub-site database (username, password, hostname, database name). Now if you go to bettersite.com, you should get the standard "Welcome to your new Drupal website" page, as shown in Figure 2. Repeat these steps for all additional sites.
Figure 2. Your new website in Drupal
Creating multisites from sub-domains
There is another way to quickly create multisites by first detailing them as sub-domains from the Cpanel. In this method, however, you must initially create folders for each site under /goodsite/sites, this time using the following convention (see Figure 3):
- Sub-site1: /www/goodsite/sites/ bettersite.com
- Sub-site2: /www/goodsite/sites/ bestsite.com
Figure 3. Creating multisites from sub-domains
Note that the sub-domain folders must have the same names as the
sub-domains themselves. As before, put a copy of default.settings.php in
each sub-domain folder, change the file name to
settings.php, and issue
CHMOD to change the permission to 644.
Now, return to Cpanel and create two sub-domains:
Create a symlink
A symlink is a file that contains a reference to another file or
folder in the form of an absolute or relative path and is created to
affect path name resolution. Use any code editor to create a file called
symlink.php, and then add code similar to the following:
<?php symlink( '/home/Cpanel_User_Name/public_html/', 'sub-domain' ); ?>
In this case, the file would look something like this:
<?php symlink('/home/myuserdirectory/goodsite/','bettersite'); symlink('/home/myuserdirectory/goodsite/','bestsite'); ?>
Put this symlink in the main directory of the main site, and run it from the URL http://goodsite.com/symlink.php. If the code syntax is good, you should see a blank page; otherwise, you will see error messages. Tweak the code in this file until it comes out correctly.
Now open the .htaccess file in any code editor. Look for the following line:
Remove the hash sign (
#), and add the following
line before saving the changes:
RedirectMatch 301 ^/sub-domain/(.*)$ http://sub-domain.yoursite.com $1
Here's what .htaccess should look like when finished:
RewriteBase / RedirectMatch 301 ^/bettersite/(.*)$ http://bettersite.goodsite.com/$1 RedirectMatch 301 ^/bestsite/(.*)$http://bestsite.goodsite.com/$1
This redirection makes it possible for both http://goodsite.com/bettersite and http://bettersite.goodsite.com to be redirected to the same site. If everything has been done correctly, you will be directed to the installation page when you access the sub-site URLs http://bettersite.goodsite.com and http://bestsite.goodsite.com.
With all the sites installed, it's easy from the Cpanel to associate the site http://bettersite.goodsite.com using a redirect to the URL http://bettersite.com.
Multisite on a local host running Ubuntu
Running a multisite installation on Ubuntu is a bit more complicated than what you have done so far for remote hosting. A multisite installation can be easily configured using the method outlined in the following sections.
Create a new hosts folder
Begin by creating a folder called goodsite in your home folder using the following code:
>> filesystem/home/<your ubuntu login name>/
The result should be
-> /home/<your ubuntu login name>/goodsite.
Now copy all your Drupal files into this folder.
Edit the hosts file
Edit the hosts file by opening a terminal window with the following command:
sudo gedit /etc/hosts'
You can then add any site name in the format
127.0.0.1 mysite.com. For this example, add the
127.0.0.1 goodsite.com 127.0.0.1 bettersite.com 127.0.0.1 bestsite.com
Edit the sites available
Now go to the sites-available folder to create a file called goodsite.com using the following commands:
cd /etc/apache2/sites-available sudo cp default goodsite.com sudo gedit goodsite.com
The first command goes to the sites-available folder; the second command copies the default file and renames it goodsite.com. The third command opens the file goodsite.com for editing. Add the code shown in Listing 1 to the file.
Listing 1. Addition to the sites-available folder file for the new site
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName goodsite.com DocumentRoot /home/<your ubuntu login name>/goodsite <Directory /> Options FollowSymLinks AllowOverride All </Directory> </VirtualHost>
Save goodsite.com, enable it, and then reload Apache using the following commands:
sudo a2ensite mysite.com sudo /etc/init.d/apache2 reload
Create sub-site folders
Create your sub-site in the /sites folder /home/[your ubuntu login name]/goodsite/sites/bettersite.com, and then add settings.php to the new folder. If you have already navigated to the sites/ directory, copy the settings.php file with the following command:
$ cp default/default.settings.php bettersite.com/settings.php $ chmod 777 bettersite.com/settings.php
Run sub-site installation
Open your browser, then type
http://bettersite.com. You should see the
installation setup page. Make the settings.php file read-only again to
avoid security messages, then run the sub-site installation.
Next, create a site folder and run the installation for bestsite.com as well as any other sub-sites mentioned in your hosts configuration.
Modules are used to extend the functionality of a basic Drupal installation and come in various categories, from administrative to actual performance-enhancement functionalities. Several modules can assist in creating a multisite or similar configuration. For details on how to use these modules, consult the installation documentation for each module.
The Domain Access modules
The Domain Access modules (see Resources for a link) enable the creation of a multisite using a single code base, a shared user database, and shared sign-on but with different content databases. Such a configuration is different from what we have done in this article. Domain Access is actually described as a set of modules that facilitate running a group of sites by using one basic Drupal code installed inside a single shared database. These modules enable all of the sites to share users, content, and settings if they have been preconfigured to operate in such a way. The Domain Access modules employ Drupal's Node Access System to establish which content should be made available on each site in the multisite arrangement. The modules, unlike other multidomain modules, also control user access on the basis of the active domain the user is viewing, not on the basis of the group or site to which the user belongs.
The Multisite Manager module
The Multisite Manager (see Resources for a link) is a contributed module that allows creation of new Drupal sites from a base Drupal site without the need to access the installation database. By default, the new site is installed in the same database as the base installation, but with a different prefix. Otherwise, if the current user has access permission to create a database, the installation could possibly be done in that database.
This article explored how to create a multisites configuration using Drupal as the base CMS. You installed the master site and located the sub-sites within its directory structure. You learned how to run multisite installations on a remote server by using the administrative facilities your hosting company provides. You also learned how to create a multisite installation on a local machine — specifically, on a computer running Ubuntu Linux®. This article also introduced two Drupal modules that can assist you in the creation of both multisite and multidomain configurations.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
- developerWorks on Twitter: Join today to follow developerWorks tweets.
- developerWorks podcasts: Listen to interesting interviews and discussions for software developers.
- developerWorks on-demand demos: Watch demos ranging from product installation and setup for beginners to advanced functionality for experienced developers.
Get products and technologies
- Download the latest version of Drupal.
- Learn more about the Domain Access modules.
- Learn more about the Multisite Manager module.
- IBM product evaluation versions: Download or explore the online trials in the IBM SOA Sandbox and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
- The developerWorks community: Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups, and wikis.
Dig deeper into Web development on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Keep up with the best and latest technical info to help you tackle your development challenges.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.