FRP區域網穿透+linux自動監控服務執行
需求:需要將demo 在外網上進行展示.有兩臺伺服器:伺服器A(外網伺服器,有公網IP,有域名),伺服器B(內網伺服器,無公網IP,通過wifi連結路由器上網,無域名).其中伺服器A效能差(qiong),伺服器B效能還行(E3, 24G,GTX1070),希望能夠從外網訪問家裡的伺服器,包括http訪問和ssh訪問.
難點:小區寬頻,外網IP不能直接訪問,不能設定DMZ主機
解決方案:用FRP來配置伺服器A和伺服器B,實現用域名訪問兩個伺服器,搭建複雜一點的Demo.
關鍵技術:1.FRP安裝配置,2. linux下配置FRP服務,3.linux下用shell指令碼實時監控FRP服務執行,重啟後也能自動連結上.
FRP安裝配置
安裝
去github上面下載最新的release, 本文用的是 v0.23.1 .
wget https://github.com/fatedier/frp/releases/download/v0.23.1/frp_0.23.1_linux_amd64.tar.gz
然後解壓
tar zxvf frp_0.23.1_linux_amd64.tar.gz
得到一個資料夾

image
frps 是伺服器端程式 frps.ini是伺服器端配置 (放到有外網的伺服器上)
frpc 是客戶端程式 frpc.ini是客戶端配置(放到內網伺服器上)
配置
frps.ini
[common] bind_port = 7000
frpc.ini
配置ssh登入以及兩個網站
[common] server_addr = xxx.xxx.xxx.xx server_port = 7000 [ssh] type = tcp#tcp型別連結 local_ip = 127.0.0.1#本地ip local_port = 22#本地埠 remote_port = xxx#外網訪問埠 [web1] type = tcp local_port = xxxx remote_port = xxxx custom_domains = localhost [web2] type = tcp local_port = xxx remote_port = xxxx custom_domains = localhost
後臺執行
推薦使用systemctl來管理,比較方便
伺服器端配置如下
sudo vim /lib/systemd/system/frps.service
在frps.service裡寫入以下內容
[Unit] Description=fraps service After=network.target syslog.target Wants=network.target [Service] Type=simple #啟動服務的命令(此處寫你的frps的實際安裝目錄) ExecStart=/your/path/frps -c /your/path/frps.ini [Install] WantedBy=multi-user.target
客戶端配置也很類似,到這裡,可以通過 systemctl start frps[frpc]
啟動服務了
通過 ssh 外網IP@remote_port可以ssh登入內網伺服器了
同理, 對應的外網IP@remote_port 可以訪問web1 和web2
但是,如果機器重啟了,還需要手動執行這些服務,比較麻煩,而且如果網路中斷,可能服務就掛了,需要手動重啟服務.如果能夠實時監控服務執行狀況,而且服務掛了自動執行,就完美了.
監控服務
伺服器端監控frps
#!/bin/sh ps -fe|grep frps |grep -v grep if [ $? -ne 0 ] then echo "start process....." systemctl start frps else echo "runing....." fi
frps 表示程序特徵字串,能夠查詢到唯一程序的特徵字串(客戶端用frpc代替就可以了)
$?表示上一個指令碼執行的返回值
-ne 0 不等於0 表示沒有查詢到結果
-eq 0 等於0 表示查詢到結果
定時監控
每一分鐘執行指令碼
sudo crontab -e
做如下修改
*/1 * * * * sh /your/path/filename.sh
客戶端也要做相應處理,到此,完美解決了這個問題.再也不用擔心我的內網穿透問題了.
參考文獻:
frp-github: https://github.com/fatedier/frp
frp 後臺執行: https://blog.csdn.net/x7418520/article/details/81077652
監控指令碼: https://www.cnblogs.com/anitinaj/p/7457000.html
shell中$?含義: https://www.cnblogs.com/chjbbs/p/6393935.html
linux 定時任務: https://blog.csdn.net/gb4215287/article/details/79496390