Skip to content

Instantly share code, notes, and snippets.

@andrewnicols andrewnicols/ Secret
Created Jul 2, 2018

What would you like to do?
NGrok Moodle config

Moodle Configuration for Dynamic ngrok

Instructions for installation

The following directories will be required:

  • MDK Storage Directory - you can check where this is with mdk config show
  • MDK Local Scripts Directory - this is in ~/.moodle-sdk/scripts (you may need to create this directory if it does not exist)
  1. Download the included ngrok.php file and place it into the MDK Storage Directory
  2. Place the included script and place it int the MDK Local Scripts Directory
  3. Create an ngrok Authentication according to the ngrok Documentation
  4. Place the included ngrokrunner script somewhere within your path and make it executable
  5. Create a new config file in ~/.config/ngrokrunner with contents:

Documentation for use of subdomains is available in the ngrok Documentation Documentation for the available regions is available in the ngrok Global Infrastructure docs


Moodle configuration

You can configure an MDK instance of Moodle to use ngrok by running:

mdk run ngrok

This will add a line to the config.php to load a custom ngrok configuration. This only needs to be done once, but does not harm if run multiple times.

Running ngrok

Once configured you can simply run:


This will start ngrok with your custom configuration of port, subdomain, and region.

* Configuration for use of ngrok with a Moodle installation.
* @copyright Andrew Nicols <>
if (isset($CFG) && isset($_SERVER)) {
if (isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], '') !== false) {
// Store the current wwwroot - we'll need this later.
$originalroot = $CFG->wwwroot;
if (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)) {
// Proxied protocol is set.
// The requested protocol is in HTTP_X_FORWARDED_PROTO.
// Use that to form the first part of the new wwwroot.
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
// This should always be an https URL, but best to check.
// Add the SSL Proxy configuration.
$CFG->sslproxy = true;
} else {
// Just redirect to the HTTPS Version.
header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
// Add the requested HTTP Host name.
$CFG->wwwroot .= "://{$_SERVER['HTTP_HOST']}";
// Add everything from the first slash onwards.
// i.e. http://example.local/stable_master
// ^^^^^^^^^^^^^^
$CFG->wwwroot .= substr($originalroot, strpos($originalroot, '/', 9));
grep ngrok config.php > /dev/null
if [ $? -ne 0 ]
echo Adding ngrok
gsed -i '/^\$CFG->wwwroot.*$/ a require_once(dirname(dirname(__DIR__)) . "\/ngrok.php");' config.php
echo ngrok already there
set -e
# Load configuration from ~/.config/ngrokrunner
if [ -f "${CONFIGFILE}" ]
echo "Loading configuration from ${CONFIGFILE}"
CMD=`which ngrok`
ARGS="http ${PORT}"
if [ -n "${SUBDOMAIN}" ]
ARGS="${ARGS} -subdomain=${SUBDOMAIN}"
if [ -n "${REGION}" ]
ARGS="${ARGS} -region=${REGION}"
echo "Starting ngrok with ${ARGS}"
"${CMD}" ${ARGS}
# Sample configuration for ngrokrunner.
# This file can be placed in $HOME/.config/ngrokrunner
# All values are optional.
## The subdomain to use when running ngrok.
# Documentation for this setting is available at
# This value is optional and the default is empty.
# ngrok will generate a unique name each time if you do not specify a value here.
## The region to use for ngrok.
# Documentation for this setting is available at
# This value is optional and the default is empty.
# ngrok will use the US region (Ohio) if you do not specify a region here.
## The port on which your http server runs locally.
# This value is optional and the default is 80.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.