分类
Laravel

Ubuntu 18.04开机启动PHP Workerman、GatewayWorker的配置方法(亲测可用)

Ubuntu 18.04不能像之前的版本那样直接通过编辑rc.local文件来设置开机启动脚本,不过,通过如下的配置,可以让rc.local重新发挥作用。

1. 建立rc-local.service文件

sudo vi /etc/systemd/system/rc-local.service

2. 将下列内容复制并粘贴进rc-local.service文件

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target

3. 创建文件 rc.local

sudo vi /etc/rc.local

4. 将下列内容复制并粘贴进rc.local 文件

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sleep 30
cd /home/vagrant/code/kttCRM/ && /usr/bin/php artisan gateway-worker:server start --daemon
exit 0

在这个文件里,把开机执行的脚本放在exit 0的上面。需要注意,sleep 30表示在执行脚本之前等待30秒钟的时间,以确保PHP启动完毕,否则会出现无法执行命令的情况,可以适当延长等待的时间。

补充:针对docker的环境,在docker-compose exec命令中加入-T选项,开启tty。

cd /home/crm/kttCRM && sudo docker-compose exec -T app /usr/local/bin/php artisan gateway-worker:server start --daemon

5. 给rc.local文件加上可执行权限

sudo chmod +x /etc/rc.local

6. 启动服务

sudo systemctl enable rc-local

7. 启动服务并检查状态

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

可以看到以下的输出说明已成功启动Workerman、GatewayWorker服务进程:

vagrant@homestead:/etc$ sudo systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (running) since Mon 2020-04-06 15:03:35 CST; 1h 41min ago
  Process: 1351 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
    Tasks: 6 (limit: 2317)
   CGroup: /system.slice/rc-local.service
           ├─3018 WorkerMan: master process  start_file=/home/vagrant/code/kttCRM/artisan
           ├─3021 WorkerMan: worker process  Gateway websocket://0.0.0.0:23460
           ├─3037 WorkerMan: worker process  BusinessWorker none
           ├─3038 WorkerMan: worker process  BusinessWorker none
           ├─3039 WorkerMan: worker process  BusinessWorker none
           └─3040 WorkerMan: worker process  Register text://0.0.0.0:12360

Apr 06 15:03:35 homestead rc.local[1351]: --------------------------------------------- WORKERMAN ------Apr 06 15:03:35 homestead rc.local[1351]: Workerman version:3.5.25          PHP version:7.4.0
Apr 06 15:03:35 homestead rc.local[1351]: ---------------------------------------------- WORKERS -------Apr 06 15:03:35 homestead rc.local[1351]: proto   user            worker            listen              
Apr 06 15:03:35 homestead systemd[1]: Started /etc/rc.local Compatibility.
Apr 06 15:03:35 homestead rc.local[1351]: tcp     root            Gateway           websocket://0.0.0.0:Apr 06 15:03:35 homestead rc.local[1351]: tcp     root            BusinessWorker    none                
Apr 06 15:03:35 homestead rc.local[1351]: tcp     root            Register          text://0.0.0.0:12360Apr 06 15:03:35 homestead rc.local[1351]: --------------------------------------------------------------Apr 06 15:03:35 homestead rc.local[1351]: Input "php gateway-worker:server stop" to stop. Start success.

至此,可以重启服务器,会看到服务已经自动启动了。

3820 ?        S      0:00 WorkerMan: worker process  Gateway websocket://0.0.0.0:23460
 3821 ?        S      0:00 WorkerMan: worker process  BusinessWorker none
 3822 ?        S      0:00 WorkerMan: worker process  BusinessWorker none
 3823 ?        S      0:00 WorkerMan: worker process  BusinessWorker none
 3824 ?        S      0:00 WorkerMan: worker process  Register text://0.0.0.0:12360

“Ubuntu 18.04开机启动PHP Workerman、GatewayWorker的配置方法(亲测可用)”上的19,098条回复