Skip to content
Mark Metcalfe edited this page Oct 4, 2021 · 14 revisions

Ngrok is a useful tool for making your local Totara site accessible publicly on the web. This is very useful when testing intergrations with Totara, such as the mobile app, or external services such as Microsoft Teams, LinkedIn Learning, or even if you simply want to make your site available for someone else to look at.

You will first need to sign up for an account, and then install and authenticate ngrok on your machine. You will also need to make sure that the ngrok command can be run normally - this may require adding your ngrok installation directory to your $PATH variable.

The easiest way to set up ngrok for use with Totara is to simply use the example config.php that is provided with this repo in your sites.

You will then need to make sure your /etc/hosts file is up to date with your site definitions. You can update your /etc/hosts file automatically by running the tool/set_hosts.sh script.

Once ngrok is set up, you can use the tngrok command to use it with Totara.


Advanced Setup

If you are not using the config.php file provided with this repo, then you will then need to add some magic to your config.php so you are able to simultaneously use ngrok as well as accessing the instance locally.

$is_multi_site = __DIR__ !== '/var/www/totara/src';
$site_name = $is_multi_site ? basename(__DIR__) : 'totara';
$has_server_dir = file_exists(__DIR__ . '/server/config.php');

$is_multi_site = __DIR__ !== '/var/www/totara/src';
$site_name = $is_multi_site ? basename(__DIR__) : 'totara';
$has_server_dir = file_exists(__DIR__  . '/server/config.php');

// This detects and sets the wwwroot dynamically so you don't have to manually change it
if (!empty($_SERVER['HTTP_X_ORIGINAL_HOST']) && strpos($_SERVER['HTTP_X_ORIGINAL_HOST'], 'ngrok.io') !== false) {
    // using ngrok
    $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_ORIGINAL_HOST'];
    $CFG->wwwroot = 'https://' . $_SERVER['HTTP_HOST'];
} else if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_SCHEME'])) {
    // accessing it locally via the web
    $hostname = $_SERVER['HTTP_HOST'];
    $hostname_parts = explode('.', $hostname);
    $CFG->wwwroot = $_SERVER['REQUEST_SCHEME'] . '://';

    if (end($hostname_parts) === 'behat') {
        // redirect if using the behat URL
        $hostname = str_replace('.behat', '', $hostname);
    }
    $CFG->wwwroot .= $hostname;

    if ($is_multi_site && strpos($hostname, $site_name) === false) {
        $CFG->wwwroot .= '/' . $site_name;
        if ($has_server_dir) {
            $CFG->wwwroot .= '/server';
        }
    }
} else {
    // accessing it via CLI
    $CFG->wwwroot = 'http://totara73';
    if ($is_multi_site) {
        $CFG->wwwroot .= '/' . $site_name;
    }
    if ($has_server_dir) {
        $CFG->wwwroot .= '/server';
    }
}

You will then need to make sure your /etc/hosts file matches up with the site directories you have if you have multiple sites set up. For example, for each Totara host such as totara73 that you wish to use, you will need to add one with your desired site as the subdomain, like yoursite.totara73.

Once ngrok is set up, you can use the tngrok command to use it with Totara.

Clone this wiki locally