4

电脑技术+建站:用frp+宝塔面板访问内网详细攻略

 1 year ago
source link: https://www.taholab.com/26279
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

严格按以下步骤来,可以少走弯路。

一、搭建frp服务端(不是客户端)

服务端的安装:

上传frp到服务器,将frp文件夹内的所有文件修改为完全访问和控制。

打开frps.ini进行设置,内容如下:

[common]
bind_port = 5443
vhost_http_port = 808
vhost_https_port = 909
dashboard_port = 3333
dashboard_user = admin
dashboard_pwd = admin
token=123456

这里的 bind_port 是frp的服务端口号,用于客户端连接;

vhost_http_port 和 vhost_https_port 分别是http网站和https网站的端口号,为了避免和80与443冲突,这里分别使用了808和909 ;

dashboard_port 是frp网页后台的访问端口;

dashboard_user 和 dashboard_pwd 是frp网页后台的登录名与登录密码;

token 是服务端和客户端的握手密码

设置好后将frp注册为服务,方便使用。

通过网站访问http://IP:3333,(3333是dashboard_port所设置的),输入用户名和密码后会看到类似如下内容:

2020-04-30_225413.png

注册为系统服务的方法:

创建服务脚本并编辑

vi /etc/systemd/system/frps.service

内容如下:(注意,代码中的frps.ini是服务端配置文件,请依据自己的实际情况做相应修改)

[Unit]
Dcription=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
User=root
Restart=on-failure
ExecStart=/root/frp/frps -c /root/frp/frps.ini
ExecReload=/root/frp/frps -c /root/frp/frps.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target

加入服务列表并设置自启动

#重新加载服务列表
systemctl daemon-reload
#将frps加入开机自启动
systemctl enable frps.service
#启动服务
systemctl start frps.service
#以下是其他常用命令
#关闭服务
systemctl stop frps.service
#重启服务
systemctl restart frps.service
#显示服务的状态
systemctl status frps.service
#禁用服务开机启动
systemctl disable frps.service
#查看服务是否开机启动
systemctl is-enabled frps.service
#查看已经启动的服务列表
systemctl list-unit-files|grep enable
#查看启动失败的服务列表
systemctl --failed配置和自启动设置

注意:以上端口需要在宝塔面板中打开防火墙放行,也需要在服务器防火墙中放行。网站主要是tcp模式。

运行

systemctl start frps.service 后看到下面界面,表面启动成功,正在监听:

2023-04-22_090155.jpg

二、搭建服务端网站

服务端安装宝塔面板,建立网站,内容为空即可。这里的搭建网站方法一切如常,端口就保持80即可。

唯一一个需要修改的地方是:建立好之后,点开设置,切换到反向代理,按下图所示进行设置:

(注意,frp服务端对http使用了808端口,对https使用了909端口,这里根据网站情况自行斟酌。)

2023-04-22_083557.jpg

三、搭建客户端网站

服务端安装宝塔面板,建立网站。这里尤其要注意,客户端搭建的网站不要使用80端口,请换个端口,比如换为8003端口。否则会报“端口冲突”的错误。这是唯一一个要注意的地方,其余就正常搭建使用即可。以下图为例:

2023-04-22_084513.jpg

建议把所有网站搭建完毕后再配置frp客户端,以免后期修改引起服务重复启动而报错。

这里搭建的网站,内网访问地址是:http://192.168.5.188:8003

而客户端配置好后,可以直接用 http://localblog.taholab.com 来访问,且不需要加端口号。

四、配置frp客户端

客户端的安装与配置:

将frp安装到客户端的服务器内。将frp文件夹内的所有文件修改为完全访问和控制。

打开frpc.ini,参照下面代码进行配置:

[common]
server_addr = 替换为你的服务器IP地址
server_port = 5443
tls_enable = true
token=123456
[EXSI]
type = https
local_ip = 192.168.5.200
local_port = 80
remote_port = 909
custom_domains = exsi.abc.com
[NAS]
type = http
local_ip = 192.168.5.189
local_port = 5000
remote_port = 808
custom_domains = nas.abc.com
[localblog]
type = http
local_ip = 192.168.5.188
local_port = 8888
remote_port = 808
custom_domains = localblog.taholab.com
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
[http_proxyx]
type = tcp              #通信数据类型
remote_port = 1080      #在frp服务端开设代理服务的端口
plugin = socks5         #通过插件开设socks5代理服务
#plugin_user = abc123   #通过插件设置连接代理的用户名              
#plugin_passwd = abc123 #通过插件设置连接代理的密码
#user_compression=true

以上的内网IP地址、端口和域名相应的替换为你的即可。

[common] 部分为客户端的初始化板块,包括了服务端的ip地址,服务端口,tls是否启动,token密码等内容,一次设置,全局有效。

除了[common]部分外,以上配置文件中,每一个 [ ] 板块下的一段,对应着一个你需要内网穿透的应用。如果你只有一个网站需要内网穿透,则只保留一个板块就可以。里面的 [ ] 是服务标识符,其下对应的代码含义如下:

  • type 表示服务类型,包括了http、https、tcp、udp等服务,比如搭建网站的话就用http或https服务。
  • local_ip 表示你的内网网站的ip地址,比如我的测试博客 localblog.taholab.com 在内网访问就可以输入 http://192.168.5.188:端口号 来访问。端口号就是你的内网网站设置的端口号,默认是80。
  • local_port 表示你的内网网站的网站端口,默认是80,你也可以改成你要的端口号,比如上述的 localblog.taholab.com 如果把网站端口设置为了8888,那么内网网站访问地址就是:http://192.168.5.189:8888
  • remote_port 表示frp服务中http网站所用的端口,本配置文件用的808,服务端也是808,如果是https网站的话,那么就得用909(因为服务端配置的是909)。
  • custom_domains 是访问该服务的域名,只对网站有用,tcp服务不需要这一项。以 localblog.taholab.com 为例,这里就输入 localblog.taholab.com 。

现在只是配置完成,还没有启动服务端。先不着急,先注册为服务后,用服务的方式来启动比较好。

注册为系统服务的方法:

将客户端frp注册为服务并开机自启动,与服务端配置方法完全一致,代码如下:

创建服务脚本并编辑

vi /etc/systemd/system/frpc.service

内容如下:(注意,代码中的frpc.ini是服务端配置文件,请依据自己的实际情况做相应修改)

[Unit]
Dcription=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
User=root
Restart=on-failure
ExecStart=/root/frp/frpc -c /root/frp/frpc.ini
ExecReload=/root/frp/frpc -c /root/frp/frpc.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target

加入服务列表并设置自启动

#重新加载服务列表
systemctl daemon-reload
#将frpc加入开机自启动
systemctl enable frpc.service
#启动服务
systemctl start frpc.service
#以下是其他常用命令
#关闭服务
systemctl stop frpc.service
#重启服务
systemctl restart frpc.service
#显示服务的状态
systemctl status frpc.service
#禁用服务开机启动
systemctl disable frpc.service
#查看服务是否开机启动
systemctl is-enabled frpc.service
#查看已经启动的服务列表
systemctl list-unit-files|grep enable
#查看启动失败的服务列表
systemctl --failed

配置成功的话,如果在浏览器访问 http://localblog.taholab.com,则会打开内网的 http://192.168.5.188:8003,表明访问成功。

至此,所有步骤均已完成,尽情享用吧!


附录:常见报错原因

错误1(frps):Create vhost http listener error, listen tcp 0.0.0.0:80: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
解决方法:端口被其他应用程序占用,关闭其他应用程序或改用其他端口。
错误2(frpc):[rdp] start error: port already used
解决方法:远程端口已被使用,改用其他端口。
错误3(frpc):[web01] start error: proxy name [web01] is already in use
解决方法:代理名称[web01]已被使用,检查并更换一个代理名称。
错误4(frpc):[web01] start error: router config conflict
解决方法:[web01]指定的域名(custom_domains)已被使用,检查并更换其他域名。
错误5(frpc):work connection closed, EOF
解决方法:连不上服务器端,检查服务器端运行是否正常,服务器端恢复后客户端会自动恢复连接。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK