User Tools

Site Tools


magento:same-copy-multiple-dbs

How to use the same Magento copy with multiple DBs and configurations

Article Updates

Updated Magento patch to allow Cache Refresh — Unirgy 2010/02/08 05:14

Procedure

Prepare original Magento copy for fast deployment of new copies

  • Unpack Magento into /magento-1.3.2.4
  • Apply this patch, attached below
  • Install ALL extensions and themes that will be used in ALL copies, using PEAR or direct file copy
    • (Optional) Proceed with configuration of Magento in this copy, if required for extension installations.
  • Symlink /magento-1.3.2.4 to /magento-latest for easier future upgrades
  • Create /magento-skel with the following structure:
/magento-skel/.htaccess - copy from /magento-latest/.htaccess
/magento-skel/index.php - copy from /magento-latest/index.php and follow instructions below
/magento-skel/js - symlink to /magento-latest/js (absolute path)
/magento-skel/skin - symlink to /magento-latest/skin (absolute path)
/magento-skel/etc - contains default configuration for new copies, copy from /magento-latest/app/etc
/magento-skel/etc/modules - contains all modules' manifests, for later selective disabling for each copy
/magento-skel/media - will contain media files, specific for each new copy
/magento-skel/var - will contain temporary files, specific for each new copy

Update /magento-skel/index.php

  • Make sure that $mageFilename points to absolute path of /magento-latest/app/Mage.php
  • Replace Mage::run() with:
$dir = dirname(__FILE__);
Mage::run('', 'store', array(
    'etc_dir'     => $dir.'/etc',
    'media_dir'   => $dir.'/media',
    'upload_dir'  => $dir.'/media/upload',
    'var_dir'     => $dir.'/var',
    'tmp_dir'     => $dir.'/var/tmp',
    'cache_dir'   => $dir.'/var/cache',
    'log_dir'     => $dir.'/var/log',
    'session_dir' => $dir.'/var/session',
    'export_dir'  => $dir.'/var/export',
));

Create a new copy of skeleton template

  • Copy /magento-skel to /copy1
  • Update /copy1/etc/local.xml to reflect this copy's DB settings
    • (Optional) Create a new DB for this copy
    • If you do not have local.xml, proceed with Magento installation wizard
  • Enable/disable relevant modules in /copy1/etc/modules
  • Navigate to this copy's URL and wait for completion of DB install/upgrade

Conclusion

Now you have 1 copy of Magento core files, for unlimited number of installations and DBs, each with it's own DB, configuration, enabled extensions, sessions, temporary and media files.

Upgrade of Magento or extensions needs to be done only once.

Applications

  • Development: QA copies with different configuration/databases
  • Running multiple stores with ease of maintenance

Configurable Dirs Patch for Magento 1.3.2.4

Click on file name to download as file:

samecopy.patch
diff -rupbB magento/app/code/core/Mage/Core/Model/Config/Options.php magento-1.3.2.4/app/code/core/Mage/Core/Model/Config/Options.php
--- magento/app/code/core/Mage/Core/Model/Config/Options.php	2009-09-23 14:52:55.000000000 -0400
+++ magento-1.3.2.4/app/code/core/Mage/Core/Model/Config/Options.php	2010-01-18 19:55:02.000000000 -0500
@@ -68,7 +68,7 @@ class Mage_Core_Model_Config_Options ext
 
     public function getDir($type)
     {
-        $this->_construct();
+        #$this->_construct();
         $method = 'get'.ucwords($type).'Dir';
         $dir = $this->$method();
         if (!$dir) {
diff -rupbB magento/app/code/core/Mage/Core/Model/Config.php magento-1.3.2.4/app/code/core/Mage/Core/Model/Config.php
--- magento/app/code/core/Mage/Core/Model/Config.php	2009-09-23 14:52:57.000000000 -0400
+++ magento-1.3.2.4/app/code/core/Mage/Core/Model/Config.php	2009-11-15 22:54:56.000000000 -0500
@@ -213,7 +213,7 @@ class Mage_Core_Model_Config extends Mag
 
         $localConfigLoaded  = $this->loadFile($etcDir.DS.'local.xml');
 
-        if (Mage::isInstalled()) {
+        if (Mage::isInstalled($options)) {
             if ($this->_canUseCacheForInit()) {
                 Varien_Profiler::start('mage::app::init::config::load_cache');
                 $loaded = $this->loadCache();
@@ -286,6 +286,9 @@ class Mage_Core_Model_Config extends Mag
     {
         $this->_allowCacheForInit = false;
         $this->_useCache = false;
+        if (!$options) {
+            $options = Mage::registry('user_options');
+        }
         return $this->init($options);
     }
 
diff -rupbB magento/app/code/core/Mage/Install/etc/install.xml magento-1.3.2.4/app/code/core/Mage/Install/etc/install.xml
--- magento/app/code/core/Mage/Install/etc/install.xml	2009-09-23 14:51:54.000000000 -0400
+++ magento-1.3.2.4/app/code/core/Mage/Install/etc/install.xml	2009-11-15 22:31:06.000000000 -0500
@@ -62,16 +62,19 @@
                     <path>/app/etc</path>
                     <existence>1</existence>
                     <recursive>0</recursive>
+                    <option_dir>etc</option_dir>
                 </app_etc>
                 <var>
                     <path>/var</path>
                     <existence>1</existence>
                     <recursive>1</recursive>
+                    <option_dir>var</option_dir>
                 </var>
                 <media>
                     <path>/media</path>
                     <existence>1</existence>
                     <recursive>1</recursive>
+                    <option_dir>media</option_dir>
                 </media>
             </writeable>
         </filesystem>
diff -rupbB magento/app/code/core/Mage/Install/Model/Installer/Filesystem.php magento-1.3.2.4/app/code/core/Mage/Install/Model/Installer/Filesystem.php
--- magento/app/code/core/Mage/Install/Model/Installer/Filesystem.php	2009-09-23 14:51:54.000000000 -0400
+++ magento-1.3.2.4/app/code/core/Mage/Install/Model/Installer/Filesystem.php	2009-11-15 22:51:44.000000000 -0500
@@ -66,7 +66,8 @@ class Mage_Install_Model_Installer_Files
             foreach ($config['writeable'] as $item) {
                 $recursive = isset($item['recursive']) ? $item['recursive'] : false;
                 $existence = isset($item['existence']) ? $item['existence'] : false;
-                $checkRes = $this->_checkPath($item['path'], $recursive, $existence, 'write');
+                $path = isset($item['option_dir']) ? Mage::getConfig()->getOptions()->getDir($item['option_dir']) : $item['path'];
+                $checkRes = $this->_checkPath($path, $recursive, $existence, 'write');
                 $res = $res && $checkRes;
             }
         }
@@ -85,7 +86,11 @@ class Mage_Install_Model_Installer_Files
     protected function _checkPath($path, $recursive, $existence, $mode)
     {
         $res = true;
+        if ($path[0]=='/' || $path[0]=='\\' || $path[1]==':') {
+            $fullPath = $path;
+        } else {
         $fullPath = dirname(Mage::getRoot()).$path;
+        }
         if ($mode == self::MODE_WRITE) {
             $setError = false;
             if ($existence) {
diff -rupbB magento/app/Mage.php magento-1.3.2.4/app/Mage.php
--- magento/app/Mage.php	2009-09-23 14:53:39.000000000 -0400
+++ magento-1.3.2.4/app/Mage.php	2010-01-18 19:55:28.000000000 -0500
@@ -452,6 +452,8 @@ final class Mage {
             Varien_Profiler::start('mage');
 
             Varien_Profiler::start('mage::app');
+
+            self::register('user_options', is_string($options) ? array('etc_dir'=>$options) : (array)$options);
             self::app($code, $type, $options);
             Varien_Profiler::stop('mage::app');
 
@@ -518,10 +518,13 @@ final class Mage {
             if (!empty($options['etc_dir'])) {
                 $etcDir = $options['etc_dir'];
             }
+            if ($etcDir[0]=='/' || $etcDir[0]=='\\' || $etcDir[1]==':') {
+                $localConfigFile = $etcDir . DS . 'local.xml';
+            } else {
             $localConfigFile = self::getRoot() . DS . $etcDir . DS . 'local.xml';
+            }
 
             $isInstalled = false;
-
             if (is_readable($localConfigFile)) {
                 $localConfig = simplexml_load_file($localConfigFile);
                 date_default_timezone_set('UTC');
magento/same-copy-multiple-dbs.txt · by unirgy