Multiple Sites Using the Same Drupal Install
In a perfect world, you want all your websites to rely on the same code base. That way a fix for one site is reflected in all the sites you manage. This holds doubly true when you use any sort of framework software, such as Drupal, as there's nothing worse than going through a dozen different installs and rolling out the same patch over and over again. Fortunately it is very easy to configure Drupal to run many websites from the same install.
This list of instructions is for a standard LAMP (Linux, Apache, MySQL, PHP) configuration which is the most common (and cheapest) hosting solution out there. You will need shell access to your account to follow these instructions. If your hosting company does not allow shell access there may be ways around restriction with FTP tools, but you should seriously consider another hosting option. There will undoubtedly be other customizations you would like to make (eg: changes to .htaccess) that they most likely will not allow.
For this example, we will be serving two pretend websites (thing1.com and thing2.com) through the same Drupal install.
- Create a MySQL database using your web host's control panel.
- Also in your web host's control panel, add the two domains to your account. This is usually done as an "add-on" domain and many hosting companies will allow you a limited number of additional domains for free. Check your hosting company's help pages for information on how to do this. (If you haven't already, you will need to point the name servers to the right DNS's -- check with your domain registrar on how to do that.)
- Access your account via the shell. Personally, I use WinSCP (which also handles FTP very nicely) and PuTTY (Windows installer is here) but there are loads of ways to do this. Once you have a shell open you are basically working in Linux-land and will be using Linux commands.
- Type
pwdto find out where you are. Most web hosts set up your accounts so that your home directory is not public (thus, not accessible from the web) and websites are stored in a subdirectory which is public. For example/home/mike/would be my account's home directory but thing1.com would point to/home/mike/www/thing1.com/. Also,~can be used as a shortcut to your home directory so~/www/thing1.comis the same as/home/mike/www/thing1.com. From this point on, I assume that~/www/is the root of you public directory (the files you would see if you were to go tohttp://www.YourDomain.com). - You want your Drupal install to be in a protected (non-public) directory, so from your home directory (eg:
/home/mike/), create a directory to hold the Drupal source code:mkdir Drupal-6.2(or whatever the current release of Drupal is). Why include the release number? We'll get to that later on. - Now copy the Drupal sources into that directory. You can do this by FTP'ing the files from your computer or do it directly from the shell.
- Change to the thing1.com directory:
cd ~/www/thing1.com - Create a symbolic link between this directory and your Drupal install:
ln -s ~/Drupal-6.2/* .(Don't forget to include the '.' at the end!). Then once more:ln -s ~/Drupal-6.2/.* .to get any system files such as the.htaccessfile. - Repeat the previous two steps for each website you wish to host with this install
- Back in the Drupal install directory navigate to the
sitesdirectory:cd ~/Drupal-6.2/sites - Create directories here for each sites you'll host with this install:
mkdir thing1.com; mkdir thing2.com. Note: we're leaving off the 'www.' part. More on that later as well... - Copy the default
settings.phpfile into each new directory:cp default/settings.php thing1.com/.; cp default/settings.php thing2.com/.(Again, don't forget the trailing '.'). - Edit each of these new
settings.phpfiles to use the currect MySQL database, user, password and prefix- Change the permissions on the file:
chmod +w thing1.com/settings.php - Change the
$db_url:$db_url = 'mysql://user:password@localhost/DBname';(replacinguser,passwordandDBnameas appropriate) - Add a database prefix if multiple sites will share the same MySQL database:
$db_prefix = 'thing1_'; - Remove write permissions on the file:
chmod -w thing1.com/settings.php
- Change the permissions on the file:
- Point your browser at
http://thing1.com/install.phpand follow the instructions.
That should do it!
Why leave the 'www.' off of the directory names?
If you want people to access your site by either http://www.thing1.com or http://thing1.com, then you need to leave the www. off off the directory name in the sites/ directory. Also this lets your setup a test.thing1.com subdomain where you can play with changes to your site using all the same server configurations without affecting the live site. Having a test site is crucial when rolling out updates to the core source, new modules or even changes to your site's layout. To create a test subdomain, just add a sites/test.thing1.com subdirectory to your Drupal core install, copy the settings.php file from sites/thing1.com to sites/test.thing1.com. Keep in mind that any database changes in the test site will be reflected in the live site so be careful when changing things like content, users, etc.
Why add the Drupal version number to the directory name?
One of the scariest things to do it rollout changes to your site while it's live. Obviously you test all changes as best you can before dumping them on the public! The first place to do that is in your own development sandbox. The next place to do it is on the same server as your live site using a subdomain such as test.thing1.com. By keeping different versions of Drupal in separate directories (eg: Drupal-6.1, Drupal-6.2, Drupal-7.0) you can easily point test.thing1.com at the beta of Drupal 7.0 while the live site at www.thing1.com is still using Drupal-6.2:
- Create a subdomain for thing1.com using your web host's control panel. Let's assume the directory for this subdomain is
~/www/test.thing1.comwhile the live site is at~/www/thing1.com. - Change to the test subdomain's directory:
cd ~/www/test.thing1.com - If needed, remove all files from this directory:
rm -rf * - Create a symbolic link between this directory and the code to be tested:
ln -s ~/Drupal-7.0/* .(Don't forget the '.' at the end) - Switch back to your Drupal sites directory:
cd ~/Drupal-7.0/sites - Add a directory for the test subdomain:
mkdir test.thing1.com - Copy the original settings to the test subdomain:
cp ~/Drupal-6.2/sites/thing1.com/settings.php ~Drupal-7.0/sites/test.thing1.com/.(Again, mind that trailing '.') - Depending on what you're testing, you may wish to create a copy of your database and run your tests against that. If so, then adjust the
$db_urlvariable insettings.phpto point to your test database. - Point your browser to
http://test.thing1.comso see you site running on the latest code.
In general, whenever you are testing changes to the core code, you should create a copy of your live database to test against. There is no guarantee that a minor version number change (eg: 6.2 to 6.3) will not change that database and that change may not be compatible with the version of Drupal running on your live site.
Post new comment