Skip to content

Instantly share code, notes, and snippets.


andrewnicols/ Secret

Last active Apr 2, 2020
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.

// You can set your 'preferred' ngrok hostname too to force ngrok usage for CLI scripts.
$ngrokhostname = "";
$preferngrok = false;
* Configuration for use of ngrok with a Moodle installation.
* @copyright Andrew Nicols <>
if (isset($CFG) && isset($_SERVER)) {
// Store the current wwwroot - we'll need this later.
$originalroot = $CFG->wwwroot;
if (isset($_SERVER['HTTP_HOST'])) {
$protocol = 'https';
if (strpos($_SERVER['HTTP_HOST'], '') !== false) {
// NGrok is a special case because we proxy for it, and always redirect to the SSL variant.
if (!array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)) {
// Just redirect to the HTTPS Version.
header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
// 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 {
$protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
// Add the requested HTTP Host name.
$CFG->wwwroot = "{$protocol}://{$_SERVER['HTTP_HOST']}";
// Add everything from the first slash onwards.
// i.e. http://example.local/stable_master
// ^^^^^^^^^^^^^^
$CFG->wwwroot .= substr($originalroot, strpos($originalroot, '/', 9));
if (!empty($CFG->behat_wwwroot) && $CFG->wwwroot === $CFG->behat_wwwroot) {
$CFG->wwwroot = $originalroot;
} else if (!empty($preferngrok) && !empty($ngrokhostname)) {
$originalroot = $CFG->wwwroot;
$CFG->wwwroot = "https://{$ngrokhostname}" . substr($originalroot, strpos($originalroot, '/', 9));
grep ngrok config.php > /dev/null
if [ $? -ne 0 ]
echo Adding ngrok
sed -i '/^require_once.*lib.*setup.php.*$/i 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.

This comment has been minimized.

Copy link

@rezaies rezaies commented Feb 19, 2020


This comment has been minimized.

Copy link
Owner Author

@andrewnicols andrewnicols commented Feb 19, 2020

Thanks @rezaies, fixed.


This comment has been minimized.

Copy link

@emosturi emosturi commented Apr 2, 2020

Hi everyone!
I am totally new to moodle, and I think I succesfully installed it on my localhost (Linux Mint 19). I was trying out moodle for a local educational project in my town. and I wanted to show some people how it works, so we can get the VPS and deploy it. so I though ngrok was a good idea, and then I realised it doesn't work that easily with moodle. so I want to know if this code would help me accomplish that. And I think I need a little help starting with this I cant find most of the things mentioned above, like:

  • Where should I run this lines "mdk config show" in the terminal? will it work on linux, do I have to change some code?
  • Where should I create this directoy ---> "~/.moodle-sdk/scripts "
  • Where do I place all those files: ngrok.php,, ngrokrunner
  • And where is $HOME/.config/ngrokrunner.

Please help me, if you do you will be opening the eyes of a young man into the world of PHP and moodle. I am currently on the edge of a cliff with a paraglide on and no idea how it works!!


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment