Skip to content

Instantly share code, notes, and snippets.

@andrewnicols andrewnicols/README.md Secret
Last active Apr 2, 2020

Embed
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 dirs.storage
  • 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 ngrok.sh 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:
SUBDOMAIN=yoursubdomain
REGION=yourpreferredregion

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

Usage

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:

ngrokrunner

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

<?php
// You can set your 'preferred' ngrok hostname too to force ngrok usage for CLI scripts.
$ngrokhostname = "dobedobedoh.au";
$preferngrok = false;
/**
* Configuration for use of ngrok with a Moodle installation.
*
* @copyright Andrew Nicols <andrew@nicols.co.uk>
*/
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'], 'ngrok.io') !== 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.
$protocol = $_SERVER['HTTP_X_FORWARDED_PROTO'];
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}.ngrok.io" . substr($originalroot, strpos($originalroot, '/', 9));
}
}
#!/bin/bash
grep ngrok config.php > /dev/null
if [ $? -ne 0 ]
then
echo Adding ngrok
sed -i '/^require_once.*lib.*setup.php.*$/i require_once(dirname(dirname(__DIR__)) . "\/ngrok.php");' config.php
else
echo ngrok already there
fi
#!/bin/bash
set -e
# Load configuration from ~/.config/ngrokrunner
CONFIGFILE="${HOME}/.config/ngrokrunner"
if [ -f "${CONFIGFILE}" ]
then
echo "Loading configuration from ${CONFIGFILE}"
. "${CONFIGFILE}"
fi
SUBDOMAIN="${SUBDOMAIN:-}"
REGION="${REGION:-}"
PORT="${PORT:-80}"
CMD=`which ngrok`
ARGS="http ${PORT}"
if [ -n "${SUBDOMAIN}" ]
then
ARGS="${ARGS} -subdomain=${SUBDOMAIN}"
fi
if [ -n "${REGION}" ]
then
ARGS="${ARGS} -region=${REGION}"
fi
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 https://ngrok.com/docs#subdomain
# 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.
#SUBDOMAIN=
## The region to use for ngrok.
# Documentation for this setting is available at https://ngrok.com/docs#global-locations
# This value is optional and the default is empty.
# ngrok will use the US region (Ohio) if you do not specify a region here.
#REGION=au
## The port on which your http server runs locally.
# This value is optional and the default is 80.
#PORT=80
@rezaies

This comment has been minimized.

Copy link

rezaies commented Feb 19, 2020

@andrewnicols

This comment has been minimized.

Copy link
Owner Author

andrewnicols commented Feb 19, 2020

Thanks @rezaies, fixed.

@emosturi

This comment has been minimized.

Copy link

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 dirs.storage" 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, ngrok.sh, 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!!

Thanks

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.