配置树莓派的frp反向代理与supervisor进程守护与开机自启脚本

配置树莓派的frp反向代理与supervisor进程守护与开机自启脚本

1. frp下载与安装

  • 在github上直接搜索frp,选择release最新版本,由于树莓派是32bit系统,因此需要下载arm版本的frp
  • 直接在Download文件夹解压安装,然后编辑frpc.ini文件
[common]
server_addr = 47.115.79.156
server_port = 7000

[ssh2] //与frps相对应
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6002 //与frps相对应,未被占用的端口

[vnc] //与frps相对应
type = tcp
local_ip = 127.0.0.1
local_port = 5900
remote_port = 5902 //无需在frps中配置
  • frpc启动命令 ./frpc -c ./frpc.ini

2. 使用supervisor对frpc进程化

  • supervisor3.2.0版本只能在python2.4+中运行
  • 要在python3中运行,需要python版本默认在python3中,执行pip install supervisor
  • 安装成功后,目录/etc/中可能无法找到supervisord.conf
  • 自行在/etc文件夹中创建supervisor文件夹,然后创建配置文件sudo su - root -c "echo_supervisord_conf > /etc/supervisor/supervisord.conf"
  • 在supervisord.conf文件中配置[include] files = /etc/supervisor/supervisord.d/*.ini
  • 接着在conf.d文件夹中新建frp.conf,添加的内容如下:
[program:frp]	//	frp用于执行supervisor进程
command = /home/pi/Downloads/frp_0.33.0_linux_arm/frpc -c home/pi/Downloads/frp_0.33.0_linux_arm/frpc.ini
autostart = true

3. 启动supervisor服务

  • sudo supervisord -c /etc/supervisor/supervisord.conf
  • 查看进程:sudo supervisorctl status
  • 重启进程:sudo supervisorctl restart frp
  • 停止进程:sudo supervisorctl stop frp
  • 启动进程:sudo supervisorctl start frp
  • 重载进程:sudo supervisorctl reload

4. 开机自启supervisor

在默认情况下,supervisor没有被安装成服务,其本身也是进程,可以通过如下方式进行supervisor开机自启:

4.1 systemctl
  • 启动服务:systemctl start xxx.service
  • 关闭服务:systemctl stop xxx.service
  • 重启服务:systemctl restart xxx.service
  • 显示服务的状态:systemctl status xxx.service
  • 在开机时启用服务:systemctl enable xxx.service
  • 在开机时禁用服务:systemctl disable xxx.service
  • 查看服务是否开机启动:systemctl is-enabled xxx.service
  • 查看已启动的服务列表:systemctl list-unit-files|grep enabled
  • 查看启动失败的服务列表:systemctl --failed
4.2 利用systemctl开机启动supervisor(无法生效)
vi /usr/lib/systemd/system/supervisord.service
写入以下内容:
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf  #根据实际路径填写
ExecStop=/usr/local/bin/supervisorctl shutdown
ExecReload=/usr/local/cdbin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
4.3 简单方法 利用开机执行/etc/rc.loacl内的脚本,实现服务自启
  • 在执行 rc.local 脚本时,PATH 环境变量未全部初始化,命令需使用绝对路径
  • Debian系统: /usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf
  • 查找相关命令的绝对路径:sudo find / -name supervisord

注意:在设置时先直接执行上述命令,判断命令是否正确

原文地址:https://www.cnblogs.com/litchi99/p/13504305.html