ElearningWorld.org

For the online learning world

MoodleTechnical

Share Local Moodle Docker Using Ngrok

In the past, as a developer, I’ve used vagrant + vagrant share to get a public URL for my local Moodle which can then be accessed via the internet.

Why would I want to do this? Well, there are a few reasons I can think of off the top of my head and probably a dozen more — e.g:

Access my local Moodle via my phone / other devicesSend a link to a QA person for testingSend a link to a fellow developer to assist in debugging a problem

The problem is that I haven’t used vagrant in a long time. Now I use Docker instead of Vagrant, but today found myself needing to share my local Moodle. However, Docker doesn’t have a comparable “vagrant share” feature so what’s the solution?

This is where ngrok comes in. What is ngrok?

ngrok is the programmable network edge that adds connectivity,
security, and observability to your apps with no code changes

OK, so how does it work? Basically, it forwards traffic to and from your localhost Moodle instance via a generated public URL. This URL can be accessed over the internet thus allowing you to test on different devices, etc…

Getting Started | ngrok Documentation

Once you’ve got ngrok setup, you can then connect a public URL to your localhost Moodle.

E.g:

If your Moodle is hosted on localhost with a port of 80:

ngrok http localhost

If your Moodle is hosted on localhost with a different port:

ngrok http localhost:8080

If your Moodle is hosted locally but with a custom host name:

ngrok http mymoodle.test:8080

Once you’ve run this you should see something like this:

Web Interface http://127.0.0.1:4041
Forwarding http://xxxx.ngrok.io -> http://mymoodle.test:8080
Forwarding https://xxxx.ngrok.io -> http://mymoodle.test:8080

If I take the ngrok public url from the right hand column (http://xxxx.ngrok.io) and paste it into my browser URL bar, it will now forward to my local Moodle. Hurray! Actually, we can’t quite celebrate yet. Moodle generally wants you to have a fixed wwwroot and doesn’t play nicely with multiple URLs accessing it. However, you can make Moodle accept any URL by adding the following code to your Moodle config.php file:

if (stripos($CFG->wwwroot, $_SERVER[‘SERVER_NAME’]) === false) {
$urlinfo = parse_url($CFG->wwwroot);
$srch = (int) $urlinfo[‘port’] !== 80 ? $urlinfo[‘host’].’:’.$urlinfo[‘port’] : $urlinfo[‘host’];
$repl = $_SERVER[‘HTTP_HOST’];
$CFG->wwwroot = str_replace($srch, $repl, $CFG->wwwroot);
}

This code will basically change the $CFG->wwwroot on the fly if Moodle is accessed via an alternative URL (e.g. vagrant share / ngrok).

Now I can log in to my development Moodle from my phone, tablet, Chromebook, or any other device!

And I can share this URL with colleagues!

Source: https://brudinie.medium.com/feed

blank
Follow me

blank

ElearningWorld Admin

Site administrator

Add a reply or comment...