温馨提示×

centos laravel队列怎么设置

小樊
51
2026-09-21 02:58:36
栏目: 智能运维

在 CentOS 上配置 Laravel 队列,一般包含以下几个步骤:环境准备、队列配置、启动队列进程、设置开机自启(Supervisor 或 systemd)。下面按常见做法一步步说明。


一、基础环境准备

1. 安装 PHP 及必要扩展

确保已安装 PHP(建议 8.0+)以及队列相关扩展:

yum install -y php php-cli php-mbstring php-xml php-curl php-zip php-bcmath php-pdo php-mysqlnd

2. 安装 Composer

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

3. 安装 Laravel(已有项目可跳过)

composer create-project laravel/laravel example-app
cd example-app

二、Laravel 队列配置

1. 修改 .env

QUEUE_CONNECTION=redis

常用连接方式:

  • database
  • redis(推荐)
  • sync(同步,仅测试)

如果使用 Redis:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

2. 生成队列表(database 驱动才需要)

php artisan queue:table
php artisan migrate

3. 创建队列任务

php artisan make:job SendEmail

在 app/Jobs/SendEmail.php 中写逻辑。


三、手动启动队列(测试用)

php artisan queue:work

或(处理完就退出):

php artisan queue:listen

⚠️ 手动方式不适合生产环境。


四、CentOS 生产环境推荐方案(Supervisor)

1. 安装 Supervisor

yum install -y epel-release
yum install -y supervisor
systemctl enable supervisord
systemctl start supervisord

2. 创建配置文件

vi /etc/supervisord.d/laravel-worker.ini

内容示例:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/example-app/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=nginx
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/example-app/storage/logs/worker.log

路径和 user 根据你的项目调整

3. 重新加载 Supervisor

supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker:*

查看状态:

supervisorctl status

五、使用 systemd(可选)

vi /etc/systemd/system/laravel-queue.service
[Unit]
Description=Laravel Queue Worker
After=network.target

[Service]
User=nginx
WorkingDirectory=/var/www/example-app
ExecStart=/usr/bin/php artisan queue:work
Restart=always

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable laravel-queue
systemctl start laravel-queue

六、常见问题

1. 权限问题

chown -R nginx:nginx storage bootstrap/cache

2. 队列不执行

  • 检查 Redis 是否启动
  • 查看 storage/logs/laravel.log
  • 执行 php artisan queue:failed

3. 代码更新后

php artisan queue:restart

如果你愿意,可以告诉我:

  • 用的是 CentOS 几
  • Laravel 版本
  • 队列用的是 Redis / database / 其他

我可以给你更精确的配置示例。

0 踩