在CentOS系统上部署ngrokd实现内网穿透
环境准备与依赖安装
确保系统为CentOS 7+版本并执行系统更新:
yum update -y
yum install -y git gcc golang make openssl-devel
生成SSL加密证书
创建专用证书目录并生成密钥文件:
mkdir /usr/local/ngrok-certs
openssl genrsa -out /usr/local/ngrok-certs/rootCA.key 2048
openssl req -x509 -new -nodes -key /usr/local/ngrok-certs/rootCA.key -days 5000 -out /usr/local/ngrok-certs/rootCA.pem -subj "/CN=ngrok.yourdomain.com"
源码编译与安装
克隆ngrok源码仓库并进行交叉编译:
git clone https://github.com/inconshreveable/ngrok.git
cd ngrok
make release-server release-client
服务端配置与启动
创建systemd服务配置文件/etc/systemd/system/ngrokd.service
:
[Unit]
Description=ngrokd service
After=network.target
[Service]
ExecStart=/opt/ngrok/bin/ngrokd -tlsKey=/usr/local/ngrok-certs/rootCA.key -tlsCrt=/usr/local/ngrok-certs/rootCA.pem -domain=ngrok.yourdomain.com -httpAddr=:8080 -httpsAddr=:8081
Restart=always
[Install]
WantedBy=multi-user.target
启用并启动服务:
systemctl daemon-reload
systemctl enable --now ngrokd
客户端连接验证
在本地设备创建ngrok.cfg
配置文件:
server_addr: "ngrok.yourdomain.com:4443"
trust_host_root_certs: true
执行穿透测试命令:
./ngrok -config=ngrok.cfg -subdomain=test 80
关键配置说明
- 域名解析:需将ngrok.yourdomain.com的A记录指向服务器IP
- 防火墙设置:开放4443(TCP)、8080(HTTP)、8081(HTTPS)端口
- 日志查看:
journalctl -u ngrokd -f
实时监控服务状态