To begin with, we will say a few words about the operation of a multisite Drupal installation. Generally, there are several types of multisite installations. You can share the code base (the core, the modules, the themes), the database, the domain and the subdomains, run it on several domains and the like.
In the following text we will be dealing with the following situation: we have a Drupal installation on a domain and we want to create subdomains for it. It can be a web site that will have smaller web sites established to support it, for example a company’s web site with product web sites serving as subdomains. We will use a single Drupal file base to avoid having too many files on the server. Besides, the update will be easier – we will have to copy the update only once for several web sites using the same modules. The main web site and the subdomain web site will both have their own databases. To ensure the common login, some of the tables in the database will however have to be shared for both web sites.
Even though subdomains are offered by many web hosting companies, it is not always possible to direct the subdomain folder into the same folder where the main web site is located. I will describe the following step by step procedure using the HostGator web hosting that offers a high-quality web hosting along with SSH, an administrative cPanel and the possibility to host several domains within a single account for a reasonable fee.
Preparation and installation of the first web site on the domain
We will begin by registering a domain by a company that offers an easy DNS records administration. Then go to www.hostgator.com and order a web hosting there. I recommend the Baby Plan or Business Plan program. You can pay using your credit card or PayPal.
HostGator offers both domain registration and hosting for an existing domain. After you pay, you will be granted access to your new HostGator account. Login to the administration interface, where you will find an Account Information box including the IP address of your hosting.
Your hosting IP address information
Login to the domain administration, switch to domain management and click the DNS setting at your domain. Edit the records and enter the IP address that you have obtained from HostGator into the respective fields.
DNS setting at Web4U
It takes a while for the changes in the DNS records to show. Usually it should not be more than 24 hours. You will be able to tell that the change is done when the HostGator home page opens instead of your domain registrant home page after you enter the address of your domain. Then you can start installing Drupal.
Click the MySQL Database Wizard button in the HostGator control panel and enter the name and access data for the new database. In the next step, check all the new database privileges for the user that you are creating and finish the database.
Setting up a database in the HostGator administration
Use FTP or WinSCP to copy the Drupal files into the public_html web site folder, enter the address of your domain into your browser and perform a common Drupal installation. Adjust the web site according to your needs, install further modules, themes, add users and insert content.
Find the Cron jobs item in the HostGator control panel. Add a record for running a Drupal cron at one-hour intervals. The command should look as follows:
curl --silent --compressed http://www.anydomain.tld/cron.php
Add a subdomain now in the HostGator administration interface using the Subdomain icon. Enter its name into the form field and retype the automatically created path in the Document Root so that only public_html remains. That will ensure that when you enter your subdomain’s address into the browser, HostGator will use data from the main folder – the same data as when accessing the main domain.
Creating a subdomain in HostGator
Setting up the databases
Now create another database and an user for the new subdomain web site in the HostGator administration, again using the MySQL Database Wizard icon. After you have finished, switch to the MySQL Databases icon. Scroll down the page to the Add User to Database form. Select the subdomain database user in the User menu and the main domain database in the Database menu. Click the Add button. This way you have allowed the subdomain database user to access the main domain database as well, which will be necessary in the next steps.
Adding another user with database access
Subdomain information for Drupal
When you enter the subdomain address into your browser at this moment, the main web site should open. If it doesn’t, check whether you have proceeded in accordance with the Subdomain creation paragraph. At this moment Drupal is not aware yet that it should react to the subdomain address in any way. We will change that soon.
Go to the sites folder and create a folder in it in the following form: subdomain.domain.com. Copy the default.settings.php file from the sites/default folder into the newly created folder for the subdomain and rename it to settings.php. Use a FTP client to enable a write permission. Mark the file for example in Total Commander, call the Files> Change attributes and set them to 777.
Now re-enter the subdomain address in your browser. A standard Drupal installer should be launched, so perform the installation the usual way. Do not forget that you have to enter the name and access data for the subdomain database when setting the database up!
After you finish the installation, you will have fully functional Drupal on your subdomain without having to copy its files and the files of the modules again. You can see it for yourself if you go to Site building > Modules. Here you will find the same modules that you use with your main domain. Of course you will be able to enable only some of them. Since the databases are independent, this setting is independent too.
Common login in the Drupal domain and subdomain
At this time you have a single Drupal file structure, two databases and two fully functional web sites. What is left to do is to connect them so that both web sites have the same user list and so that when you login to one of them, you would be automatically logged in to the other one as well. Open the file sites/default/settings.php in any editor, uncomment the line with $cookie_domain and modify it in the following way:
$cookie_domain = '.domain.com';
Save the modified file. Note that the Drupal installer has probably disabled the write permission for this file, you have to enable it first using your FTP client. Experienced users can edit the file using the command line in the vi editor that will cope with this problem.
Now open the file sites/subdomain.domain.com/settings.php in an editor and modify the record concerning the domain for cookies to our domain as well:
$cookie_domain = '.domain.com';
In this respect the web site setting for the domain and the subdomain will be the same. Let us keep editing the sites/subdomain.domain.com/settings.php file. Find the part with $db_prefix and replace it with:
$db_prefix = array(
'default' => '',
'authmap' => 'domain_database.',
'openid_associations' => ‘domain_database.',
'profile_fields' => 'domain_database.',
'profile_values' => 'domain_database.',
'role' => 'domain_database.',
'sessions' => 'domain_database.',
'users' => 'domain_database.',
Then save the file. Through this adjustment you have informed the subdomain web site that is should reach the domain main web site database for the tables for login, profiles, user roles, sessions and user lists. The apostrophes include the name of the database that you have created first, which must end with a dot.
Now log out both from the main web site and from the subdomain, empty the browser’s cache and delete the cookies. If you have done the procedure right, you will be logged in to one of the web sites automatically after you have logged in to the other one.
Tips, possible changes and topics to think about
The aforementioned procedure can be successfully applied even if the domain web site has been up and running for some time and has already its users. After you create the subdomain and set the sharing of the tables for users and sessions, the existing users will of course be able to log in to all web sites without having to re-register.
HostGator is not the only web hosting where this can be created, but I recommend it for the simplicity of its settings.
The HostGator administrative interface
If your web hosting allows you to set where the subdomain will be directed, but you have only one database, use the same database access data when installing your subdomain web site, but attach some prefixes to the table names, for example subdomain_. When adjusting the settings.php file for the subdomain you will have to enter 'subdomain_’ instead of 'domain_database.' in the $db_prefix settings as a table name. Note that there is no dot at the end, because we stay in the same database.
The aforementioned solution for common login and user sharing works only in case of a domain and its subdomain. One Drupal installation can be also used to run several different domains. However, in this case it is not possible to set a single domain for cookies. If you want to have common login for several different domains in a multisite installation, use the Single Sign On module. At this moment somebody who will be managing it in the future is however sought. Before you start using it, please consider this information.
In the settings that we have shown the user profiles have been shared as well. Of course you can leave them out, only in that case you should enable the Profile module for both web sites before you start editing the settings.php file.
When updating a multisite installation, copy the new modules or Drupal to one place, however the update.php must be performed on both web sites. Do not forget about this.
When performing a more advanced multisite installation, you can try the Domain module that allows you to publish one article in selected domains from a single administrative interface. This however exceeds the scope of this article.
If you will create a new account at HostGator, you can use maxiorelcom as the coupon code to get $9.94 off the regular price.