- Published on
[TIL] Keep Ngrok Running on Mac OS: A Cost-Free Solution
If you want to expose local servers to the internet securely and cost-effectively on Mac OS, Ngrok is a great tool. This guide shows you how to set up Ngrok, manage it with a home server, and automate Slack notifications—all without spending any money.
Ngrok is a tunneling service that creates secure tunnels to localhost. This allows developers to share their local applications with others using a temporary URL. It’s ideal for remote collaboration on web projects or databases.
Hosting a home server on your Mac gives you control over your web applications and files. With Ngrok, you can expose these services to the internet from home, all at no cost.
Follow Ngrok document for installation and authentication https://ngrok.com
To keep Ngrok running continuously, you’ll use launchd on Mac OS. This system utility manages background services, keeping them active even between sessions.
Create a .plist File: Run:Replace username with your actual username.
shellvim ~/Library/LaunchAgents/com.username.ngrok.plist
Edit .plist File: Add the following XML configuration:
xml<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>Label</key><string>com.yourusername.ngrok</string><key>ProgramArguments</key><array><string>/bin/bash</string><string>/Users/username/path/ngrok_with_slack.sh</string></array><key>RunAtLoad</key><true/><key>KeepAlive</key><true/></dict></plist>
Load the Job: Activate it with:
shelllaunchctl load ~/Library/LaunchAgents/com.username.ngrok.plist
To get notifications in Slack when Ngrok is running, create a bash script (ngrok_with_slack.sh):
shell#!/bin/bash# start ngrok/usr/local/bin/ngrok http 80 --log ~/ngrok.log &PID=$!# wait for ngrok readysleep 10# get urlngrok_url=$(curl -s http://127.0.0.1:4040/api/tunnels | grep -o '"public_url":"[^"]*"' | cut -d '"' -f 4)# send to slack channelwebhook_url="https://hooks.slack.com/services/your/webhook/path"payload="{\"text\": \"ngrok is running at: $ngrok_url\"}"curl -X POST -H 'Content-type: application/json' --data "$payload" $webhook_urlwait $PID
You can get the Ngrok domain using the local API at http://127.0.0.1:4040.
To test, load a service on localhost and check if it’s accessible through the Ngrok URL. Ensure Slack receives the notification!
Combining Mac OS, Ngrok, Slack, and a home server offers a powerful, cost-effective solution for managing and sharing local applications. Follow these steps to set up your environment and enjoy a seamless, efficient workflow.
With this setup, you can leverage technology to enhance your workflow without any financial burden. Enjoy the flexibility and control it provides!
- Published on