The ngrok Environment
Nextjournal is a great way to experiment with building services in the cloud. Ngrok makes it easy to expose localhost servers to the internet.
Install
curl -sS https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip -o ngrok.zip
unzip /ngrok.zip
Configure
Settings for the ngrok configuration file. The following config will tunnel to localhost:5000.
region us
console_uitrue
tunnels
btf
proto http
addr5000
subdomain btf
proto: http
- the protocol. The HTTP protocol covers both HTTP and HTTPSaddr
addr: 5000
- the port of the application.addr:
https://localhost:5000
use the full URL to force HTTPS (equivalent tongrok http https://localhost:5000
on the command line)
subdomain: btf
- available with the paid version of ngrok. Otherwise the subdomain will be random public URLbind_tls
- without this setting both HTTP and HTTPS will be available from ngrokbind_tls: true
- ngrok only listens on an HTTPS tunnel endpointbind_tls: false
- ngrok only listens on an HTTP tunnel endpoint
host_header: localhost
- some APIs will want to see the host header of your app rather than 'ngrok'
Grab your authtoken from ngrok, store the token in your Nextjournal secret vault, and add it to the environment. The token will authenticate this notebook with the ngrok service.
Prepend the authtoken to the configuration file above, ngrok.yml
.
echo 'authtoken:' $ngrok | cat - ngrok.yml > /tmp/out && mv /tmp/out ngrok.yml
Run
Run ngrok
in the background using nohup
. Unfortunately, there are no plans to offer a daemon service in the basic Ngrok package.
Nohup stands for "No Hangup". The command ignores the HUP signal that terminates a process when a terminal connection is closed. A few notes on using nohup
:
nohup command &
:&
run the command in the backgroundnohup command > /path/to/file.txt
:>
redirects the output from the defaultnohup.out
Nextjournal will display any stout beneath the running cell. Even with the process running in the background, the stdout will occupy the notebooks process unless it is redirected to a file. Therefore, &>
is used below to redirect stout
output to a file.
nohup /ngrok start --all --config="/ngrok.yml" &> /tmp/ngrok.log & sleep 1
tail /tmp/ngrok.log
Place the URL into a browser and the tunnel will be visible. Anything served from port 5000 will appear here. In this case, there is nothing being served, hence the error message.
Admin
The service is up and running. To stop the ngrok tunnel, use the kill
command with the process ID: kill <PID>
or kill -9 "$(pgrep ngrok)"
.
ps aux
# kill -9 "$(pgrep ngrok)"
ps aux
Appendix
Consider reading An Ngrok Tutorial and Primer by Daniel Miessler