Ngrok 安装

150 阅读3分钟

1 参考链接:

www.bilibili.com/video/BV1MW… blog.csdn.net/daiqi552715…

2 域名解析

image.png

3 go 环境搭建

3.1 参考地址:

docs.studygolang.com/doc/install

  • 下载并解压 go 文件
sudo -i

cd /usr/local/
 
wget https://studygolang.com/dl/golang/go1.16.4.src.tar.gz
 
tar -xf go1.16.4.src.tar.gz
  
ln -s /usr/local/go/bin/* /usr/bin/

  • 设置环境变量, 编辑 /etc/profile 文件
export GOROOT=/usr/local/go

export PATH=$PATH:$GOROOT/bin

  • 执行如下命令,使环境变量生效
source /etc/profile
  • 安装完之后,执行如下指令,进行检测
go version

go env

4 安装 Ngrok

  • 通过 git 下载 ngrok 源码包
cd /usr/local

git clone https://github.com/inconshreveable/ngrok.git

  • 配置 ngrok 的环境变量
export GOPATH=/usr/local/ngrok/

export NGROK_DOMAIN="ngrok.xxxx.com"

  • 生成 ngrok 证书
cd /usr/local/ngrok

openssl genrsa -out rootCA.key 2048 

openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem  

openssl genrsa -out server.key 2048 

openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr

openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000

  • 覆盖 ngrok 源码安装包中原有的证书
cp rootCA.pem assets/client/tls/ngrokroot.crt

cp server.crt assets/server/tls/snakeoil.crt

cp server.key assets/server/tls/snakeoil.key

5 生成服务端

cd /usr/local/ngrok

GOOS=linux GOARCH=amd64 make release-server

执行之后,在 /usr/local/ngrok/bin 下面会有 ngrokd 文件

image.png

5.1 系统的类型不同,生成服务端软件时,执行的指令也不一样

系统类型位数执行指令
Linux32GOOS=linux GOARCH=386 make release-server
Linux64GOOS=linux GOARCH=amd64 make release-server
Windows32GOOS=windows GOARCH=386 make release-server
Windows64GOOS=windows GOARCH=amd64 make release-server
Mac32GOOS=darwin GOARCH=386 make release-server
Mac64GOOS=darwin GOARCH=amd64 make release-server
ARM不限GOOS=linux GOARCH=arm make release-server

6 生成客户端

GOOS=linux GOARCH=amd64 make release-client

执行之后,在 /usr/local/ngrok/bin 下面会有 ngrok 文件

image.png

6.1 系统类型不同,生成客户端软件时,执行的指令也不一样

系统类型位数执行指令
Linux32GOOS=linux GOARCH=386 make release-client
Linux64GOOS=linux GOARCH=amd64 make release-client
Windows32GOOS=windows GOARCH=386 make release-client
Windows64GOOS=windows GOARCH=amd64 make release-client
Mac32GOOS=darwin GOARCH=386 make release-client
Mac64GOOS=darwin GOARCH=amd64 make release-client
ARM不限GOOS=linux GOARCH=arm make release-client

7 服务端启动

cd /usr/local/ngrok

# 临时启动, 由于 80 和 443 端口被 nginx 占用,所有 httpAddr 选择为8080, httpsAddr 选为 4433

./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="ngrok.xx.com" -httpAddr=":8080" -httpsAddr=":4433" -tunnelAddr=":4443"

# nohup 模式启动
nohup ./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="ngrok.xx.com" -httpAddr=":8080" -httpsAddr=":4433" -tunnelAddr=":4443" &

7.1 设置为开机自动启动

vim /etc/rc.d/init.d/ngrok
  • 输入如下内容:
#!/bin/bash
 
#chkconfig: - 99 01
 
#description:ngrok  
 
 
case "$1" in
	start)
	echo "start ngrok service.."
	cd /usr/local/ngrok/
	setsid ./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="ngrok.xxxx.com" -httpAddr=":8080" -httpsAddr=":4433" -tunnelAddr=":4443" 
	;;
        *)
        exit 1
        ;;
esac
 
###参数说明:
### cd /usr/local/ngrok/:进入ngrok目录
### setsid:开始没有这个 结果关闭窗口 发现服务停掉了 加上目前好用
### setsid ./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="abc.club" -httpAddr=":80" -httpsAddr=":443" -tunnelAddr=":4443" :启动ngrok服务端命令
  • 给该配置文件权限
chmod 755 ngrok
  • 注册为系统服务
chkconfig --add ngrok
  • 启动
service ngrok start
  • 加入开机自启动项
systemctl enable ngrok.service //加入开机启动

systemctl daemon-reload  //重新加载配置文件

8 linux 环境下,客户端布置

  • root 用户身份登录, 创建 ngrok 文件夹,然后通过ftp 传入文件
cd /root

mkdir ngrok

文件位于百度网盘中:

链接:https://pan.baidu.com/s/1NS74SZGnX7EXFb49KglTSg

提取码:an98