Search code examples
gitdeploymentdevelopment-environmentexpressionengineproduction-environment

Git: Deploying to Environments with Different Web Root Directory Names


I have an ExpressionEngine site set up with Git in multiple environments: Local, Development, and Production.

I have a couple of directories that are above web root, so the web root directory itself is inside the git repo, like this:

  • .git
  • system
  • third_party
  • templates
  • public_html (web root)
    • assets
      • css
      • js
      • img
    • themes
    • index.php

Now, my development and production environments are with 2 separate hosting providers, and their web roots have different names from each other. Development, for example, is named public_html, but Production is named content.

How do I deploy to both of these environments when the web root directories have different names?


Solution

  • Using symbolic links on the server to point the web root to the appropriate directory is a time honored technique. Let's assume you gave your Git Repo an obvious name: clientsite.com, so inside that folder you have:

    • .git
    • system
    • third_party
    • templates
    • web_root
      • assets
        • css
        • js
        • img
      • themes
      • index.php
      • admin.php

    That folder gets uploaded to your staging/production servers. On the staging server, you would then create a symbolic link to web_root named public_html:

    ln -s clientsite.com/web_root public_html

    And then on the production server, you would make a symbolic link to web_root called content:

    ln -s clientsite.com/web_root content


    Now, what's brilliant about this is that if you are very clever and are using MSM, you can create config.php and index.php files that allow you to use web_root for ALL your domains in that EE installation and just create symbolic links to it for each site. For example:

    ln -s clientsite.com/web_root siteone_html
    
    ln -s clientsite.com/web_root sitetwo_html
    

    Then in index.php, you look at the HTTP_HOST server config to set the site_name:

    switch ( $_SERVER['HTTP_HOST'] ) {
    
        case 'siteone.com' :
        case 'dev.siteone.com' :
            $assign_to_config['site_name']  = 'siteone';
        break;
        case 'sitetwo.com' :
        case 'dev.sitetwo.com' :
            $assign_to_config['site_name']  = 'site two';
        break;
    }
    

    Finally, your config.php can do something very similar:

    $config['site_index']   = "";
    $config['site_url']     = "http://".$_SERVER['HTTP_HOST'];
    $config['server_path']  = $_SERVER['DOCUMENT_ROOT'];
    $config['cp_url']       = $config['site_url']."/".$config['system_folder'].'/index.php';
    
    ....stuff here...
    
    switch ( $_SERVER['HTTP_HOST'] ) {
    
        // production
        case 'siteone.com' :
            $config['cookie_domain'] = ".siteone.com";
            $db['expressionengine']['hostname'] = "111.222.111.22";
            $db['expressionengine']['username'] = "siteone";
            $db['expressionengine']['password'] = "passone";
            $db['expressionengine']['database'] = "database_live";
            $db['expressionengine']['db_debug'] = FALSE; 
        break;
    
        // staging
        case 'dev.siteone.com' :
            $config['cookie_domain'] = "dev.siteone.com";
            $db['expressionengine']['hostname'] = "333.444.555.22";
            $db['expressionengine']['username'] = "siteone";
            $db['expressionengine']['password'] = "passone";
            $db['expressionengine']['database'] = "database_dev";
            $db['expressionengine']['db_debug'] = FALSE; 
        break;
    }
    

    In this way, you can have global config options and then site specific options too.