This post is transalation of the Hebrew guide in my blog. I'm doing it after requsests from the Joomla Conference 2009 at New York.

This guide use single installation of Joomla that produce multiple sites with one configuration file for each site with its own database prefixes. Although there are various extensions for that purpose, but I recommend to do it by this hack, because it can control the way that different sites will be run better and adjust performance by dividing the load in case of multiple servers.
Note that this installation distinguishes between a database of sites by the prefix of data in database tables. You can divide each site to different MySQL instances.

 

Step One - how sites will be identified

At this point we decide how users will be sent between sites. This can be done by domain-prefix or url querystring. The first method identified the site by the initial of the domain. The second method identified the site by using the add-to-url string (querystring). This post will demonstrate the use the first method.

Step Two - Define routing file

Routing file define how we will know which site surfer arrived. We'll placed file naming routing.php in the includes directory.

defined( '_JEXEC' ) or die( 'Restricted access' );

$baseurl = 'domain.com'; /* your domain without any prefix like www */
$default_prefix = 'www';
$config_path = 'config'.DS;
$sitename=str_replace('.'.$baseurl, ", $_SERVER['SERVER_NAME']);

/*

// for querystring manipulation
// if the structure is: http://www.domain.com/sites/sitename
// ** without sub-domain **
$ar = explode( "/", $_SERVER['REQUEST_URI']);
$sitename = $ar[2];

*/
if(!file_exists(JPATH_ROOT.DS. $config_path.$sitename.".php"))
{
if(strpos($_SERVER['SERVER_NAME'], $baseurl)&&($sitename!=$default_prefix))
// if there is a domain request and not sub domain exist redirect to default site
{
header ("Location: http://'.$default_prefix.".$baseurl);
die('no such sub domain.<br/>redirecting to default site…');
}
$sitename = $default_prefix; // if u want to premit ip domain request (or computer name; localhost also)
}
define( 'JCONFIG_SITENAME', $sitename);
define('JCONFIG_PATHNAME',$config_path.JCONFIG_SITENAME.'.php');

This file take the prefix of the domain and put it to into JCONFIG_SITENAME constant and path of configuration file under constant JCONFIG_PATHNAME (configuration file of Joomla as a default is configuration.php and is located in front of The Joomla). Configuration files will be located under the config directory under the main directory of Joomla. This library contains all the configuration files of the sites. If the configuration file of the sub-domain does not exist it will be sent to by default (www). Also, copy this file to includes directory of the administrator directory.

Step Three - The files update Joomla

At this time we will update the Joomla files to the settings we've made. First we'll update index.php files to execute the script we add.

We add the following line after line 21 in index.php in the main directory of Joomla index.php file and in the administrator directory:

require_once (JPATH_BASE. DS. 'includes'. DS. "Routing.php');

Then, we update the The Joomla core of the new path of configuration file of the specific site. We will do this using a simple sed command. Under Linux:

for i in `find /your/joomla/full_path/ -type f; do echo sed-i" s; 'Configuration.php; JCONFIG_PATHNAME; "$ i done

Under Windows:

FOR / r "your\joomla\full\path"%% a in (*. php) do (
sed-i "s; 'Configuration.php; JCONFIG_PATHNAME;" "%% a")

You must replace your \joomla\full\path to the path of your Joomla installation on your server. Windows server sed command is not built, so we'll have to install it. You can download it here.

Here actually finished the update code. Note that if your default domain is www you must create a configuration file of Joomla called www.php under config directory. Probably need to create this directory if not yet created. The configuration file of the original Joomla (configuration.php), which is the main directory of Joomla, could be used as the default domain of the central (www.php).

The final step - a database for all domain

In addition, each domain will have to create tables Joomla with his differentprefix to control. Prefix probably might be the domain name for the orderly structure of course. You must set the prefix tables in the site configuration file (again under the config directory).

To duplicate a database you'll need to export data base by the prefix of the names of the tables there so you can use one of the following scripts (Linux or Windows).
For Linux:

#!/bin/bash
DB="$1"
PREFIX=$2
EXPORT_FILE=$3
HOST=YOUR_DB_HOST
USER=YOUR_DB_USER
PASSWORD=YOUR_DB_PASSWORD

if [ -z ${EXPORT_FILE} ]; then
EXPORT_FILE="export.sql"
fi

if [ -z ${DB} ]; then
echo "YOU NEED AT LEAST 2 ARGS:"
echo "1ST ARG DB"
echo "2ND ARG PREFIX"
echo "3RD ARG EXPORT FILE NAME (DEFAULT: export.sql)"
else
if [ -z ${PREFIX} ]; then
echo "YOU DIDNT FILLED DB"
else
TEMP_FILE="/tmp/agron.mysql"
mysql -h ${HOST} -u ${USER} –password=${PASSWORD} -D ${DB} -e "SHOW TABLES FROM ${DB} LIKE '${PREFIX}%'" -N > ${TEMP_FILE}

if [ -w ${EXPORT_FILE} ]; then
rm ${EXPORT_FILE}
fi

cat ${TEMP_FILE} | while read LINE ; do
echo ${LINE}
mysqldump -h ${HOST} -u ${USER} –password=${PASSWORD} ${DB} ${LINE} >> ${EXPORT_FILE}
done

if [ -w ${TEMP_FILE} ]; then
rm ${TEMP_FILE}
fi
echo "Export done"
fi
fi
fi

For windows:

@echo off
IF dummy==dummy%1 (
GOTO NOARGS
) ELSE (
IF dummy==dummy%2 (
GOTO NOARGS
)
SET DB=%1
SET PREFIX=%2
SET HOST=YOUR_HOST
SET USER=YOUR_DB_USER
SET PASSWORD=YOUR_DB_PASSWORD
IF dummy==dummy%3 (
:: IF NOT SET PARAMETER 3
SET EXPORT_FILE=export.sql
) ELSE (
:: IF SET PARAMETER 3
SET EXPORT_FILE=%3
)
mysql -u %USER% –password=%PASSWORD% -e "SHOW TABLES FROM %DB% LIKE '%PREFIX%%%'" -N > c:\windows\temp\mysql.temp
IF EXIST %EXPORT_FILE% del %EXPORT_FILE%
FOR /F "eol=; " %%i in (c:\windows\temp\mysql.temp) do (
@echo %%i
mysqldump -u %USER% –password=%PASSWORD% %DB% %%i >> %EXPORT_FILE%
)
IF EXIST c:\windows\temp\mysql.temp del c:\windows\temp\mysql.temp
ECHO Export done
GOTO :END
:NOARGS
ECHO YOU NEED AT LEAST 2 ARGS:
ECHO 1ST ARG DB
ECHO 2ND ARG PREFIX
ECHO 3RD ARG EXPORT FILE NAME (DEFAULT: export.sql)
:END

These scripts with three arguments: database, prefix (of the tables name) and export file, when the last is not required (default: in the current directory with file named export.sql). You'll need to enter the top of the script parameters like host, database, username and password to connection to the database.

That's all for now

Hope the guide was understandable, because most of it used google translation. If not, please ask questions under the post and I'll try to answer and update the guide. Later, i'll publish scripts for easy replication of a the and its databases, and also update Joomla and its database when Joomla update release (this update is done like any normal update, but in addition, you need to running the sed command).

Site Agron BC&C LTD using this method to create multisite (see list of online libraries on the right).

Leave your comments

0
  • No comments found