参考文章:
http://blog.csdn.net/sdfgsdfg1444/article/details/72793313
http://www.jianshu.com/p/b254547b9fe5
http://blog.csdn.net/u013216667/article/details/50782084
步骤:
1.安装依赖环境
yum -y install zlib-devel openssl-devel perl hg cpio expat-devel gettext-devel curl curl-devel perl-ExtUtils-MakeMaker hg wget gcc gcc-c++ unzip
2.安装git
先下载:https://www.kernel.org/pub/software/scm/git/git-2.6.0.tar.gz
cd /usr/
wget https://www.kernel.org/pub/software/scm/git/git-2.6.0.tar.gz
tar zxvf git-2.6.0.tar.gz
配置git环境
cd /usr/git-2.6.0
make prefix=/usr/local/git all
make prefix=/usr/local/git install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc
git version
3.go下载:https://pan.baidu.com/s/1hs1VzjY ; 放到/usr/local
解压 tar zxvf go1.4.2.linux-amd64.tar.gz
环境go配置 添加环境变量,
编辑
vim /etc/profile
在最后添加
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
保存后,使环境变量生效
source /etc/profile
或者创建软连接:
ln -s /usr/local/go/bin/* /usr/bin/
检查版本
go version
4.ngrok下载:https://pan.baidu.com/s/1i5ckSdR ; zip解压放到/usr/local/ngrok下
配置ngrok
cd /usr/local/ngrok
export GOPATH=/usr/local/ngrok/
export NGROK_DOMAIN="xxx.xin"
cd /usr/local/ngrok
5.签名:
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
6.复制签名:
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key
7.编译服务端
cd /usr/local/go/src
GOOS=linux GOARCH=amd64 ./make.bash
cd /usr/local/ngrok/
GOOS=linux GOARCH=amd64 make release-server
8.编译客户端
cd /usr/local/go/src
GOOS=windows GOARCH=amd64 ./make.bash
cd /usr/local/ngrok/
GOOS=windows GOARCH=amd64 make release-client
9.启动服务端
cd 进入根目录直接输入
/usr/local/ngrok/bin/ngrokd -domain="xxx.xin" -httpAddr=":80" -tunnelAddr=":4443"
//服务端后台永久运行
nohup /usr/local/ngrok/bin/ngrokd -domain="xxx.xin" -httpAddr=":80" -tunnelAddr=":4443"
10.客户端配置新建一个 ngrok.cfg 文件
server_addr: "xxx.xin:4443"
trust_host_root_certs: false
11.客户端启动新建一个xxx.bat文件
ngrok -config=ngrok.cfg -subdomain xxx 8080
注意:
1.如果客户端启动显示reconnection,请检查防火墙端口是否开放80和4443端口
vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4443 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
编辑防火墙配置文件 按 i 编辑、按esc退出编辑、输入 :wq
//重启防火墙
service iptables restart
2.如果可以连接,但是不能访问,请检查域名是否开启CNAME记录
路由上虚拟机服务器是否设置端口80/4443/443
192.168.5.110(ngrok所在Linux的ip)、4443是http访问的端口、443是https访问的端口
常用服务器 外部端口 内部端口 IP地址 协议类型
443 443 192.168.5.110 ALL
80 80 192.168.5.110 ALL
4443 4443 192.168.5.110 ALL
--window服务端启动
ngrokd -domain="xxx.xin" -tunnelAddr=":4443" -httpAddr=":80"
ngrokd -tlsKey="server.key" -tlsCrt="server.crt" -domain="xxx.xin" -tunnelAddr=":4443" -httpsAddr=":443" -httpAddr=":80"
编译32位用go32位,可能会出现以下错误
bash: /usr/local/bin/rar: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
是因为64位系统中安装了32位程序
解决方法:
yum install glibc.i686
重新安装以后还有如下类系错误 再继续安装包
error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
yum install libstdc++.so.6
window服务端启动,新建bat命令文件,添加以下内容:
ngrokd -tlsKey="server.key" -tlsCrt="server.crt" -domain="xxx.xin" -tunnelAddr=":4443" -httpsAddr=":443" -httpAddr=":80"
说明:
-tlsKey="server.key" -tlsCrt="server.crt"
是对应自己申请的SSL证书,Apache,找到server.key server.crt 这两个证书文件
注意:如何你只是申请了顶级域名的证书、二级域名使用顶级域名的证书是无效的。除非你申请的是泛型*.顶级域名的证书 例如:*.xxx.xin证书
客户端启动:要用证书时设置下面为:trust_host_root_certs: true,没用证书trust_host_root_certs: false
客户端配置新建一个 ngrok.cfg 文件
server_addr: "xxx.xin:4443"
trust_host_root_certs: false
11.客户端启动新建一个xxx.bat文件(顶级域名:-hostname、二级域名:-subdomain)
-- 顶级域名
ngrok -config=ngrok.cfg -hostname xxx.xin192.168.0.106:8080
-- 二级域名
ngrok -config=ngrok.cfg -subdomain weixin.xxx.xin192.168.0.106:8080