logo
August 17, 2024

[TIL] Keep Ngrok Running on Mac OS: A Cost-Free Solution

Learn how to use Mac OS, Ngrok, Slack, and a home server for free to keep your Ngrok process running smoothly and efficiently.
Introduction

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.

Setting Up Ngrok on Mac OS

Follow Ngrok document for installation and authentication https://ngrok.com

Run Ngrok on Startup

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 Launch Agent

Create a .plist File: Run:Replace username with your actual username.

shell
vim ~/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:

shell
launchctl load ~/Library/LaunchAgents/com.username.ngrok.plist
Automate Slack Notifications

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 ready
sleep 10
# get url
ngrok_url=$(curl -s http://127.0.0.1:4040/api/tunnels | grep -o '"public_url":"[^"]*"' | cut -d '"' -f 4)
# send to slack channel
webhook_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_url
wait $PID
Retrieve Ngrok Domain Programmatically

You can get the Ngrok domain using the local API at http://127.0.0.1:4040.

Testing Your Setup

To test, load a service on localhost and check if it’s accessible through the Ngrok URL. Ensure Slack receives the notification!

Benefits
  • Cost-Free: No expenses involved.
  • Flexible: Easy to configure for different needs.
  • Collaboration: Share projects effortlessly.
  • Persistence: launchd keeps Ngrok running continuously.
  • Notifications: Stay updated with Slack alerts.
  • Control: Host and manage your projects from home.
  • Conclusion

    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.

    Key Takeaways
  • Integrate Ngrok and Slack to streamline development.
  • Automate processes on Mac OS for efficiency.
  • Save money with free tools and a home server.
  • Stay informed with real-time notifications.
  • Use Ngrok for temporary, remote access projects.
  • Ensure continuous operation with launchd.
  • With this setup, you can leverage technology to enhance your workflow without any financial burden. Enjoy the flexibility and control it provides!