如何实现ngrok开机启动
时间: 2024-07-27 22:01:00 浏览: 139
Linux搭建Ngrok服务器实现内网的计算机实现远程桌面.pdf
在Linux或macOS系统中,让Ngrok在每次系统启动时自动运行,可以利用系统级别的服务管理器。以下是两种常见的方法:
**在Ubuntu和Debian系列(Systemd)**:
1. **安装ngrok**: 使用apt-get安装:
```
sudo apt-get install ngrok
```
2. **创建systemd服务**: 编辑`/etc/systemd/system/ngrok.service`文件,添加以下内容:
```ini
[Unit]
Description=Ngrok HTTP/TLS Tunnel
After=network.target
[Service]
ExecStart=/usr/local/bin/ngrok authtoken [YOUR_NGROK_API_TOKEN] http [YOUR_PORT]
Restart=always
User=<your-user>
WorkingDirectory=<path-to-your-project>
[Install]
WantedBy=multi-user.target
```
将`[YOUR_NGROK_API_TOKEN]`和`[YOUR_PORT]`替换为你实际的Ngrok API token和需要绑定的端口号。
3. **启用并启动服务**:
```sh
sudo systemctl daemon-reload
sudo systemctl enable ngrok
sudo systemctl start ngrok
```
**在macOS(Homebrew)**:
1. **安装ngrok**: 执行:
```sh
brew install ngrok
```
2. **创建launchd守护进程**: 创建`~/Library/LaunchAgents/homebrew.ngrok.plist`文件,加入内容:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.ngrok</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/ngrok</string>
<string>authtoken</string>
<string>[YOUR_NGROK_API_TOKEN]</string>
<string>http</string>
<string>[YOUR_PORT]</string>
</array>
<!-- 其他选项如User等可选 -->
</dict>
</plist>
```
3. **加载和启动守护进程**:
```sh
launchctl load ~/Library/LaunchAgents/homebrew.ngrok.plist
```
请注意替换`[YOUR_NGROK_API_TOKEN]`和`[YOUR_PORT]`为你的Ngrok API密钥和应用绑定的端口。
阅读全文