User:Razool/MultipleWiki

Description: Assume that you want to provide www.username.host.domain.com for the homepage of username via just DNS A records to the same machine and without any virtualhosts on this machine.

Solution: For HTTP/1.0 requests there is no solution, but for HTTP/1.1 requests which contain a Host: HTTP header we can use the following ruleset to rewrite http://www.username.host.com/anypath internally to /home/username/anypath:

    RewriteEngine on
    RewriteCond   %{HTTP_HOST}                 ^www\.[^.]+\.host\.com$
    RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
    RewriteRule   ^www\.([^.]+)\.host\.com(.*) /home/$1$2

You want to install more than one wiki on a single server, using the same source code (i.e. single set of MediaWiki files) , and using the same database?

  1. Install prerequisites.
  2. Upload MediaWiki files to web folder on the webserver.
  3. From browser, browse to the uploaded folder (for example, if your web server is running as http://localhost and MediaWiki files were uploaded to /testwiki/ folder, then the URL would be http://localhost/testwiki) which will lead to a page which gives a link to Please set up the wiki first. Click the link, fill-in the setup form, and install the first wiki (e.g., MyWiki). For details, see Manual:Config script.
  4. After successful installation, move LocalSettings.php into the root directory of your wiki and rename it in such a way to make it easy to track (e.g., myWikiLocalSettings.php)
  5. Repeat step three and four above for each wiki you wish to create, creating a new LocalSettings.php file for each wiki (e.g., anotherWikiLocalSettings.php, etc.)
  6. If two or more separately installed wikis are to be merged to operate out of files of the main wiki , then after renaming and moving each of your LocalSettings.php files to the main wiki folder, change the variable $wgScriptPath in the each of the LocalSettings.php files to point to the main wiki's folder.
  7. Create a LocalSettings.php file for your global settings, then select one from the two possibilities below:
1: If you have different domains/subdomains that link to one directory on your server, use this:
  Note:To link your subdomains to one directory on your server, you may have to edit the configuration file for your server (can not be done with a .htaccess file, try changing httpd-vhosts.txt there in Apache web server setup) or, if your site has its own IP address, modify the DNS configuration for your site.
<?php
	switch ($_SERVER["SERVER_NAME"])
	{
		case "shoopz.com":
			require_once "LocalSettings_shoopz_com_.php";
			break;

		case "help.shoopz.com":
			require_once "LocalSettings_help_shoopz_com_.php";
			break;

		case "wiki.shoopz.net":
			require_once "LocalSettings_wiki_shoopz_net.php";
			break;

		default:
			echo "This wiki is not available. Check configuration.";
			exit(0);
	}
?>


OR 2: If your wikis are in different directories (e.g. yourdomain.com/wiki1, yourdomain.com/wiki2 etc) linked to the main wiki directory on your server (say yourdomain.com/w), use this:
  Note: If the function strpos() finds the string you search for at the beginning of $callingurl, then the function returns 0 (i.e. it found the string starting at position zero) therefore, you need to change if(strpos($callingurl,'wikiN')) to if(strpos($callingurl,'wikiN') !== FALSE).
<?php

$callingurl = strtolower($_SERVER['REQUEST_URI']); //get the calling url

if ( strpos( $callingurl, 'wiki1', 0) ) {
        require_once( 'LocalSettings_wiki1.php' );
}
else if ( strpos( $callingurl, 'wiki2', 0) ) {
        require_once( 'LocalSettings_wiki2.php' );
}
.
.
.
else if ( strpos( $callingurl, 'wikiN', 0) ) {
        require_once( 'LocalSettings_wikiN.php' );
}
else {
        echo "This wiki (in ". $callingurl . ") is not available. Check configuration.";
        exit(0);
}
  Note: If you use Short URL with the second case (directory based wikis), you need to check the two directories: strpos( $callingurl, 'wiki1', 0) || strpos( $callingurl, 'w1/', 0), to symlink the sources (w1 -> w), and adapt $wgScriptPath.
  Note: You should use "srtpos() == 1" or similar instead of plain strpos() to avoid redirecting to wrong wiki when the url contains the name after the beginning

You can use a different unique MySQL database for each wiki (see $wgDBname) OR you can use a different table prefix for each wiki (for Postgres, you can achieve a similar effect by using different schemas) (see $wgDBprefix).

Separate directory wikies

edit

This works for separate directories wikies

<?php
$callingURL = strtolower($_SERVER['REQUEST_URI']); //the requesting url

if ( strpos( $callingURL, 'wikione') ) {
        require_once('Wiki_one_settings.php');    
}

else if ( strpos( $callingURL, 'wikitwo') ) {
        require_once('Wiki_two_settings.php' );    
}
?>

Share resources

edit

$wgUseSharedUploads = true;

$wgSharedUploadPath = ‘http://yourwikiname/images’;

$wgSharedUploadDirectory = ‘/(LOCALPATH)/Folder Name/images/’;

$wgHashedSharedUploadDirectory = true;

User comments

edit

Scenario 6: Multiple wikis through RewriteRules

edit

This approach is based on the work of Mizanur Rahman (see Boolean Dreams Article and it has elements in common with #Scenario 3: Quick set-up above. Follow steps 1-5 in #Scenario 3: Quick set-up to setup the individual wikis. Then modify the LocalSettings.php file for each wiki along the lines:

Wiki 1 Local Settings File:

$wgArticlePath = "/wiki1/$1"; // Gives us Wikipedia style urls for articles.
$wgScriptPath =  "/srcwiki1"; // Makes non-article urls unique for this wiki.

Similarly for Wiki 2 Local Settings File:

$wgArticlePath = "/wiki2/$1"; // Gives us Wikipedia style urls for articles.
$wgScriptPath =  "/srcwiki2"; // Makes non-article urls unique for this wiki.

You'll also need a master file, along the lines described in step 7 of #Scenario 3: Quick set-up to choose the appropriate local settings file for wiki1 or wiki2 based on a url.

Finally, in the .htaccess file you need RewriteRules to map the article urls to standard wiki urls and non-article urls to the folder containing the wiki source. Assuming the wiki source is installed in '/w/', .htaccess would be something like:

RewriteEngine On
RewriteRule ^wiki1/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki1/*$ wiki1/ [L,QSA]

RewriteRule ^wiki2/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki2/*$ wiki2/ [L,QSA]

RewriteRule ^srcwiki1/(.*)$ /w/$1 [PT,L,QSA]
RewriteRule ^srcwiki2/(.*)$ /w/$1 [PT,L,QSA]

Fix for Maintenance Scripts

edit

Maintenance scripts run directly, not via a url so the RewriteRules won't provide a suitable url for the master LocalSettings.php file to $_SERVER["SERVER_NAME"] or $_SERVER['REQUEST_URI']. To work around this, use a separate global varible to offer an alternate selection method and define the variable on the command line when calling maintenance scripts. A suitable master LocalSettings.php would be something like:

<?php
if (isset($gWikiSelect))
{
  if ($gWikiSelect == 'wiki1')
  {
    require_once('Local_Wiki1_Settings.php');
    require_once('Admin_Wiki1_Settings.php');
    
  } else if ($gWikiSelect == 'wiki2')
  {
    require_once('Local_Wiki2_Settings.php');
    require_once('Admin_Wiki2_Settings.php');
  } 

} else
{ 
  $callingUrl = strtolower($_SERVER['REQUEST_URI']);

  if (strpos($callingUrl, wiki1))
  {
    require_once('Local_Wiki1_Settings.php');
    
  } else if (strpos($callingUrl, 'wiki2'))
  {
    require_once('Local_Wiki2_Settings.php');
  } 
}
?>

Then, when running a maintenance script, such as importImages.php from the command line:

php -d auto_prepend_file=SelectWiki1.php importImages.php c:\myimages

Here the -d switch modifies the auto_prepend_file setting in php.ini to execute a file before the importImages.php script. This file sets the global variable to select the wiki:

<?php
$gWikiSelect='wiki1';
?>


To get this to work, I had to use different values of $wgScriptPath for each wiki as I think Kai was suggesting AND create rewrite rules to map these to the single wiki source.
So, in the individual LocalSettings files:

# LS1:
$wgScriptPath = ‘wiki1src’;
$wgArticlePath = ‘wiki1′;

# LS2:
$wgScriptPath = ‘wiki2src’;
$wgArticlePath = ‘wiki2′;

& in .htaccess with the wiki src in the directory /w/

RewriteRule ^wiki1src/(.*)$ /w/$1 [PT,L,QSA]
RewriteRule ^wiki2src/(.*)$ /w/$1 [PT,L,QSA]

# + the usual:
RewriteRule ^wiki1/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki1/*$ wiki1/ [L,QSA]

RewriteRule ^wiki2/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki2/*$ wiki1/ [L,QSA]

# to create pretty urls.
Setting Different Image directory:

You additionally have to set the variable $wgUploadPath to the same directory as $wgUploadDirectory. 
Otherwise the uploaded files cannot be referenced.

Discussion on MW talk page

edit

Multiple Wikis, one install/config

edit

I need to host a reasonably small number of similar wikis. I want to be able to update the source and configuration for all of them simultaneously, and have simplified maintenance. The solution I came up with is inspired by http://www.steverumberg.com/wiki/index.php?title=WikiHelp_-_Method_One and especially the reader comment about mod_rewrite. I wrote it up so I could remember, and because I thought it might be helpful. Bryan C. 68.188.91.87 05:00, 19 December 2008 (UTC)

The general idea is that we use the web server (Apache, here) to set an environment variable based on the URL. Then, in LocalSettings.php, we include a file that depends on the environment variable and contains exactly those settings needed for the selected wiki. All other settings can be shared. I have separate image directories for each wiki - I suppose this would work with a shared images directory, but I haven't tried it.

Why this is good:

  • Completely transparent to users
  • Can make a config change to all wikis at once since they all share the bulk of LocalSettings.php. For example, you can add an extension to all of them as easily as for a single wiki. You can also make settings only on some wikis, or on all but some wikis.
  • Avoids symbolic links, which will fail if the file structure of the MediaWiki install changes.
  • No need to customize maintenance scripts - just set an environment variable to choose which wiki they effect.
  • Only one directory needs to be backed up, and it's got everything.

Why you might try a different method:

  • Don't have the ability to add RewriteRules to Apache
  • Wikis will have widely differing settings/extensions.

This step-by-step shows how to set up a pair of wikis, which I'll call wiki1 and wiki2, and which are served at www.example.com/wiki1 and www.example.com/wiki2. I did this on Linux with Apache. The technique, but not the steps, should work in other settings.

Step 1: Create first wiki

edit

In fact, I did this step a long time before moving to a multi-wiki setup. Just a normal install. If you plan to run multiple wikis under one database, you should use a different database prefix for each one.

Step 2: Add structure for separating wikis

edit

Put a subdirectory structure on the images directory so that the images are stored in 'images/wiki1'. One way to do this is to rename images to wiki1, create a new directory images, copy wiki1 into it, then check that the permission settings are correct to allow write access by the webserver.

Split LocalSettings.php into two files, one still called "LocalSettings.php" and a second called "LocalSettings-wiki1.php". The -wiki1 file should contain only those settings which are specific to wiki1. For my setup, these were:

  • $wgSitename = "Wiki1"
  • $wgScriptPath = "/wiki1"
  • $wgDBname
  • $wgUploadDirectory = "{$IP}/images/wiki1";
  • $wgUploadPath = "{$wgScriptPath}/images/wiki1";
  • $wgLogo
  • $wgProxyKey

Comments: Obviosly, you want different sitenames and possibly logos. The ScriptPath is so MediaWiki can generate pages with the correct URLs. For a single database solution, the DB variable could stay in LocalSettings.php and the site-specific file could set the prefix (mysql) or schema (postgres). The wgUpload variables set up the new images directory structure. I don't understand wgProxyKey, but I think it serves to keep a user's sessions untangled when they are logged in to multiple wikis.

Now add this to LocalSettings.php:

require_once("$IP/LocalSettings-".$_SERVER["WIKI"].".php");

It includes the "LocalSettings-????.php" file depending on the state of the environment variable WIKI. I put this immediately following the require_once for DefaultSettings.

Add this line to the Apache config file:

RewriteRule ^/wiki1(.*) /your/path/to/mediawiki$1 [E=WIKI:wiki1,last]

The E part sets the environment variable "WIKI" to wiki1. The rest of the line is fairly standard and could be changed if needed.

At this point, you should test your single wiki and make sure everything still works, especially image upload/view.

Step 3: Add additional wiki(s)

edit

Create the database for the new wiki and grant appropriate permissions to the DBuser, as done in a standard install (or skip this if you're using prefixes).

Create the images/wiki2 directory and set it's permissions so the webserver can write it.

Add a RewriteRule to Apache. It's the same as above, except with wiki2 in both places instead of wiki1.

Move LocalSettings.php to a different name temporarily.

Allow write permission on the config directory.

Browse to www.example.com/wiki2. It should look just like the config for a fresh install. Fill in the install form, nothing special (except the database or prefix needs to be set appropriately for wiki2).

Put back your LocalSettings.php file that you hid temporarily.

Create LocalSettings-wiki2.php. Either hand-edit a copy of LocalSettings-wiki1.php, or cut the important stuff out of the newly created config/LocalSettings.php (which is then no longer needed).

Test wiki2. Repeat step 3 to add more wikis.

Maintenance

edit

To run a maintenance script, simply set an environment variable to pick the target wiki. For example, under Linux:

WIKI=wiki1 php maintenance/checkUsernames.php
edit

How can this be done under Windwos running

Pretty simular environment as under linux.