在 Debian 上“守护(守护进程化)Laravel 进程”,常见场景有两种:
下面以 最常用、最推荐的 systemd 方式 为主,并补充其他方案。
sudo nano /etc/systemd/system/laravel-queue.service
示例(根据你的路径修改):
[Unit]
Description=Laravel Queue Worker
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/laravel
ExecStart=/usr/bin/php /var/www/laravel/artisan queue:work --queue=default --tries=3
Restart=always
RestartSec=5
Environment=APP_ENV=production
[Install]
WantedBy=multi-user.target
⚠️ 确保
php路径正确(which php)
sudo systemctl daemon-reload
sudo systemctl enable laravel-queue
sudo systemctl start laravel-queue
systemctl status laravel-queue
journalctl -u laravel-queue -f
Laravel 调度本身不是常驻进程,用 cron 触发:
crontab -e
添加:
* * * * * cd /var/www/laravel && php artisan schedule:run >> /dev/null 2>&1
如果你使用 schedule:work(Laravel 8+):
php artisan schedule:work
也可以用 systemd 守护它。
[Unit]
Description=Laravel Octane
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/laravel
ExecStart=/usr/bin/php artisan octane:start --server=roadrunner
Restart=always
[Install]
Wanted=...
sudo apt install supervisor
sudo nano /etc/supervisor/conf.d/laravel-queue.conf
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel/artisan queue:work
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/laravel/storage/logs/queue.log
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-queue
storage/ 和 bootstrap/cache/ 必须可写www-data 或专门用户--max-time=3600--max-requestsqueue:work --queue=high,default
| 场景 | 推荐方式 |
|---|---|
| 队列 | systemd ✅ |
| 定时任务 | cron |
| Octane | systemd |
| 老系统 | supervisor |
如果你愿意,可以告诉我:
我可以给你一份直接可用的配置文件。