本文以workerman为例,介绍linux环境下配置开启自启动.
1.创建workerman.service服务文件(权限755)
# 进入system目录
cd /lib/systemd/system/
#创建workerman.service
touch workerman.service
#授权755
chmod 755 workerman.service
2.编辑workerman.service文件
编辑文件,一定要重启服务器,才会生效。
查看php环境变量,执行 whereis php 命令,如下可见php环境变量位于/usr/local/php/bin下,所以workerman.service文件中使用/usr/local/php/bin/php命令。
[root@VM-16-15-centos ~]# whereis php
php: /usr/local/php /usr/local/php/bin/php
[Unit]
Description=workerman
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/bin/php /data/wwwroot/GatewayWorker/start.php start -d
ExecReload=/usr/local/php/bin/php /data/wwwroot/GatewayWorker/start.php restart
ExecStop=/usr/local/php/bin/php /data/wwwroot/GatewayWorker/start.php stop
PrivateTmp=true
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
/usr/local/php/bin/php /data/wwwroot/GatewayWorker/start.php 要替换成实际内容
3.启动服务
systemctl start workerman.service
4.查看服务状态
执行此命令会显示详细信息(可以排查错误)
systemctl status workerman.service
5.设置开机自启动
systemctl enable workerman.service
返回信息如下
#创建了软连接
Created symlink from /etc/systemd/system/multi-user.target.wants/workerman.service to /usr/lib/systemd/system/workerman.service.
6.重启服务器查看结果
- 进入项目目录 cd /data/wwwroot/GatewayWorker/
- 查看状态 php start.php status
[root@VM-16-15-centos ~]# cd /data/wwwroot/GatewayWorker/
[root@VM-16-15-centos GatewayWorker]# php start.php status
Workerman[start.php] status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:4.0.26 PHP version:8.1.12
start time:2022-12-09 11:00:56 run 0 days 0 hours
load average: 0.07, 0.02, 0.01 event-loop:\Workerman\Events\Select
3 workers 9 processes
worker_name exit_status exit_count
JYXYChatAppBusinessWorker 0 0
JYXYChatAppGateway 0 0
Register 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 36M - - 52 0 4 32 0 [Summary]
上图显示配置成功,即重启服务器后开启了workerman服务。