Bootstrap

centos设置开机自启的几种方案(frp为例)

下面几种方式任选其一即可

创建自定义systemd服务实现

  • 以frps为例
  1. 创建服务文件
    vim /etc/systemd/system/frps.service
    如果没有vim命令可以使用vi 也可以执行yum install vim 安装一下
  2. 将该配置粘贴到frps.service中并根据实际情况修改保存
[Unit]
Description=FRP Server Service
After=network.target network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.toml
Restart=on-failure
StandardOutput=file:/usr/local/frp/frps.log
StandardError=file:/usr/local/frp/frps.log

[Install]
WantedBy=multi-user.target
  1. 重新加载 systemd 配置
sudo systemctl daemon-reload
  1. 启用服务并设置开机自启
systemctl start frps.service
systemctl enable frps.service
  1. 查看服务状态
systemctl status frps.service
  1. 正常输出参考
sudo systemctl status frps.service
● frps.service - FRP Server Service
   Loaded: loaded (/etc/systemd/system/frps.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2025-01-20 10:39:48 CST; 771ms ago
 Main PID: 11018 (frps)
    Tasks: 5
   Memory: 3.4M
   CGroup: /system.slice/frps.service
           └─11018 /usr/local/frp/frps -c /usr/local/frp/frps.toml

Jan 20 10:39:48 vm-16-3-centos systemd[1]: Started FRP Server Service.
Jan 20 10:39:48 vm-16-3-centos frps[11018]: 2025-01-20 10:39:48.196 [I] [frps/root....ml
Jan 20 10:39:48 vm-16-3-centos frps[11018]: 2025-01-20 10:39:48.327 [I] [server/ser...00
Jan 20 10:39:48 vm-16-3-centos frps[11018]: 2025-01-20 10:39:48.327 [I] [server/ser...01
Jan 20 10:39:48 vm-16-3-centos frps[11018]: 2025-01-20 10:39:48.327 [I] [frps/root....ly
Jan 20 10:39:48 vm-16-3-centos frps[11018]: 2025-01-20 10:39:48.327 [I] [server/ser...02
Hint: Some lines were ellipsized, use -l to show in full.

修改/etc/rc.d/rc.local实现

  1. 编辑 rc.local 文件
#确保 rc.local 文件具有可执行权限,并编辑rc.local添加启动 frps 的命令
sudo chmod +x /etc/rc.d/rc.local
sudo vim /etc/rc.d/rc.local
  1. 在rc.local文件内部添加下面的内容 然后wq保存
    为了确保它在后台运行并且忽略挂起信号,使用 nohup 和 & 符号
# Start FRPS
nohup /usr/local/frp/frps -c /usr/local/frp/frps.toml > /usr/local/frp/frps.log 2>&1 &
  1. 确保 rc-local.service 已启用
sudo systemctl start rc-local
sudo systemctl enable rc-local
  1. 验证配置
sudo reboot
# 或者
sudo systemctl restart rc-local
  1. 检查frps服务状态
#查看日志文件
cat /usr/local/frp/frps.log

docker安装frp实现(埋坑 有时间再写 推荐上面两种更简单)

;