Difference between revisions of "Multi-Site Setup"

From GeeklogWiki
Jump to: navigation, search
(Multi-Site Setup)
(No difference)

Revision as of 07:38, 24 May 2010

What is it?

With only a small modification, Geeklog can be used to run several websites off of one code base, i.e. you only need one physical set of files to run more than one Geeklog site.

Please note that this setup does have some drawbacks and limitations (discussed below) and is not officially supported.


Setting it up

Since (almost) all the configuration information is held in the database, we only need to make sure that Geeklog picks the correct database for the site it's supposed to display. The trick is that the global PHP variable $_SERVER['HTTP_HOST'] contains, effectively, the domain name that the visitor used to call up the site. So we modify db-config.php like so:

if (strpos($_SERVER['HTTP_HOST'], 'example.com') !== false) {
    $_DB_host = 'localhost';
    $_DB_name = 'database1';
    $_DB_user = 'dbuser1';
    $_DB_pass = 'password1';
    $_DB_table_prefix = 'ex1_';
} else {
    $_DB_host = 'localhost';
    $_DB_name = 'database2';
    $_DB_user = 'dbuser2';
    $_DB_pass = 'password2';
    $_DB_table_prefix = 'ex2_';
}
$_DB_dbms = 'mysql';

You can add additional elseif sections for every additional site that you want to support.

Tweaks

Minor tweaks:

  • If all databases are using the same host, you only need to list the $_DB_host variable once.
  • If all sites use their own database, they could also all use the same table prefix, which would also be listed only once then.
  • If you want to run sites with different DBMS (e.g. one site running on Postgres, while others are on MySQL), you would need to move the $_DB_dbms into the if/else construct.

Domain Name

Note that we're using strpos to check for the domain name. This is so that we can both accept www.example.com and example.com.

You may want to set up a .htaccess rule so that your site always uses the same form, despite what the user enters. For example, if you prefer the form www.example.com over others, you can add

RewriteEngine On

RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R]

to your .htaccess to redirect http://example.com/ to http://www.example.com/. That redirect happens before the above code in your db-config.php is executed, i.e. the code will only see the "www" form then.

siteconfig.php

The information from the siteconfig.php file will be the same for all your sites. The only setting of interest in this file is the $_CONF['default_charset']. Effectively, you will have to run all of your sites with the same character set. This should not be a problem if you use UTF-8. If you can guarantee that all your sites are using the same language, you could also use another character set here. Plan ahead, though, since changing this later will be a lot of work.


Limitations and Workarounds

The main advantage of this setup is also its main limitation: All the resources are shared between sites. This is what we want for the source code, but it also happens for other files. This can be worked around in some cases but not in all of them.

Installation and Upgrades

Please note that Geeklog's install script is not aware of this hack and will only be able to install or upgrade one site at a time. This makes upgrades especially awkward, as you have to take all the sites down at the same time and then upgrade them one after the other (using temporary copies of db-config.php, since the install script will modify it).

Plugins

All plugins will be available to all the sites. This does not mean, however, that they need to be installed for all of them. You can install, say, the Forum plugin for one site only. Just keep in mind that it will show up as an uninstalled plugin for all the other sites.

Plugins share all of their files with all of the sites. This may cause problems when the plugin stores files in fixed locations (e.g. configuration information that needs to be different for different sites).

Of the plugins that ship with Geeklog, the XMLSitemap Plugin needs a special setup: The default name for the sitemap file is sitemap.xml and would be used by all the sites which would obviously cause confusion. To the the XMLSitemap section in the Configuration and make sure you use a different file name for each site. (TBD: How do you tell the search engine which sitemap belongs to which site?)

Themes

Like any other resource, all themes are also available for each site. This may not be a problem if you're using a distinct theme for each site anyway and don't let your users change their theme. Otherwise, you may need to change the Themes Path in the Configuration of each site.

Favicon

Related to the theme: You probably want to use different Favicons for your sites. Since some browsers are looking for a file called favicon.ico by default, you should make sure not to have a file of that name. Instead, modify the header.thtml of your site's theme to point to a favicon with a unique name:

<link rel="SHORTCUT ICON" href="{site_url}/mysite.ico">

Images

Geeklog's images directory is shared. This may cause problems especially with the user photos (if you allow those). You can disable uploading of user photos and/or switch to using Gravatar.

As long as you make sure not to re-use story IDs, all article images could be left in the same directory. If you can not guarantee that, you may want to separate the images directories (again, in the Configuration). Please note that this will also affect user photos and topic icons.

robots.txt

The robots.txt has a fixed file name and will have to be shared between all sites. Keep that in mind when adding entries.

(In theory, it should be possible to set up the server to create the robots.txt dynamically. Not sure if that's worth the effort and it will most likely affect all .txt files on the server.)

.htaccess

Unless you can tweak you webserver's setup, the .htaccess will also have to be shared between sites. Keep that in mind when adding redirects or deny rules.