宝塔:在CentOS系统中搭建frp服务,使得可以用外网访问内网网站

1 安装配置

(1) 服务器端配置:

1) 首先,下载frp压缩包,下载地址为:https://github.com/fatedier/frp/releases,要下载与服务器liunx相对应的版本



2) 接着,解压压缩包,然后把解压后的整个文件夹frp放到【/root/frp 】目录下。


注意:记得给这些文件777权限。


3) 接着,修改【/root/frp/frps.ini】文件,内容如下:

[common]
bind_port = 5443
vhost_http_port = 808
vhost_https_port = 909
dashboard_user = admin
dashboard_pwd = admin
token=yourpassword

注意:

- 5443是frp服务端口,808是http服务端口,909是https服务端口,之所以这样做,是因为我的服务器本身用了宝塔面板搭建了网站,因此为了错开80端口和443端口进行了这样的设置,以上端口需要在宝塔面板中打开防火墙放行,也需要在阿里云管理面板中放行。
- 然后做域名解析,将需要的域名A记录解析到阿里云服务器上,比如把nas.abc.com解析到了服务器上。
- 在宝塔面板中新建网站,输入刚才解析的域名nas.abc.com,建立的时候只需要修改一个地方,即PHP版本处,修改为“静态”。然后在“反向代理”处进行修改,对于访问http的话,修改为 “http://127.0.0.1:808”,访问https的话修改为“https://127.0.0.1:909”。


4) 接着,启动frp服务:

cd /root/frp
./frps -c frps.ini


5) 到此,服务器端配置完成。


(2) 客户端配置:

1) 接着,同理,把整个文件夹frp放到客户端宝塔的【/root/frp 】目录下。


注意:记得给这些文件777权限。


3) 接着,修改【/root/frp/frpc.ini】文件,内容如下:

[common]
server_addr = 替换为你的服务器IP地址
server_port = 5443
tls_enable = true
token=yourpassword

[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

注意:

- 以上的内网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 。


4) 接着,启动客户端,与服务端建立连接,命令如下:

cd /root/frp
./frpc -c frpc.ini


5) 到处,如果在浏览器访问 http://nas.abc.com:808,则会打开内网的http://192.168.5.189:5000。



6) 到处,客户端配置完成。



2 设置自启动

(1) 服务端配置和自动设置:

1) 简介:在CentOS上将frp内网穿透服务注册为服务并开启自启动且失效自启动。

2) 首先,创建服务脚本并编辑 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


3)接着,加入服务列表并设置自启动:

#重新加载服务列表
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配置和自启动设置


(2) 客户端配置和自动设置:

1)客户端与服务端配置方法完全一致。

2)首先,创建服务脚本并编辑 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

3)接着,加入服务列表并设置自启动:

#重新加载服务列表
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



相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容<?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' => [ '__PUBLIC__' => '/', '__STATIC__' => '/static', '__CONSOLE__' => '/static/console', '__CONSOLE_CSS__' => '/static/console/css', '__CONSOLE_IMAGES__' => '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this->x = $x; $this->y = $y; }}$point = new Point(3.5, 2.8);echo $point->x; // 输出: 3.5echo $point->y; // 输出: 2.8// 下面的尝