Difference between revisions of "Multi-Site Setup"

From GeeklogWiki
Jump to: navigation, search
(Clarified sitemap.xml limitations)
(siteconfig.php: Allow siteconfig to each site)
 
Line 52: Line 52:
 
=== siteconfig.php ===
 
=== siteconfig.php ===
  
The information from the <tt>siteconfig.php</tt> file will be the same for all your sites. The only setting of interest in this file is the <code>$_CONF['default_charset']</code>. 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.
+
As in the db-config.php file, the informations from the <tt>siteconfig.php</tt> file can be different for each site.
  
 +
<pre>
 +
if ($_SERVER['HTTP_HOST'] == 'example1.com') {
 +
    // To disable your site1 quickly, simply set this flag to false
 +
    $_CONF['site_enabled'] = true;
 +
    $_CONF_FCK['imagelibrary'] = '/images/library_site1';
 +
    $_CONF['default_charset'] = 'utf-8';
 +
 +
} else if ($_SERVER['HTTP_HOST'] == 'example2.com) {
 +
    // To disable your site2 quickly, simply set this flag to false
 +
    $_CONF['site_enabled'] = true;
 +
    $_CONF_FCK['imagelibrary'] = '/images/library_site2';
 +
    $_CONF['default_charset'] = 'utf-8';
 +
 +
} else if ($_SERVER['HTTP_HOST'] == 'example3.com) {
 +
    // To disable your site2 quickly, simply set this flag to false
 +
    $_CONF['site_enabled'] = false;
 +
    $_CONF_FCK['imagelibrary'] = '/images/library_site3';
 +
    $_CONF['default_charset'] = 'utf-8';
 +
 +
}
 +
</pre>
  
 
== Limitations and Workarounds ==
 
== Limitations and Workarounds ==

Latest revision as of 21:43, 8 November 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

As in the db-config.php file, the informations from the siteconfig.php file can be different for each site.

if ($_SERVER['HTTP_HOST'] == 'example1.com') {
    // To disable your site1 quickly, simply set this flag to false
    $_CONF['site_enabled'] = true;
    $_CONF_FCK['imagelibrary'] = '/images/library_site1';
    $_CONF['default_charset'] = 'utf-8';
 
} else if ($_SERVER['HTTP_HOST'] == 'example2.com) {
    // To disable your site2 quickly, simply set this flag to false
    $_CONF['site_enabled'] = true;
    $_CONF_FCK['imagelibrary'] = '/images/library_site2';
    $_CONF['default_charset'] = 'utf-8';

} else if ($_SERVER['HTTP_HOST'] == 'example3.com) {
    // To disable your site2 quickly, simply set this flag to false
    $_CONF['site_enabled'] = false;
    $_CONF_FCK['imagelibrary'] = '/images/library_site3';
    $_CONF['default_charset'] = 'utf-8';

}

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).

sitemap.xml

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. Go to the XMLSitemap section in the Configuration and make sure you use a different file name for each site.

Note that due to the fact that all your sites share a single robots.txt you can not add the sitemaps to your robots.txt file. Instead, you will have to submit them manually for each site, e.g. via the Google Webmaster Tools.

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.