在 CentOS 上配置 Laravel 队列,一般包含以下几个步骤:环境准备、队列配置、启动队列进程、设置开机自启(Supervisor 或 systemd)。下面按常见做法一步步说明。
确保已安装 PHP(建议 8.0+)以及队列相关扩展:
yum install -y php php-cli php-mbstring php-xml php-curl php-zip php-bcmath php-pdo php-mysqlnd
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer create-project laravel/laravel example-app
cd example-app
.envQUEUE_CONNECTION=redis
常用连接方式:
databaseredis(推荐)sync(同步,仅测试)如果使用 Redis:
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
php artisan queue:table
php artisan migrate
php artisan make:job SendEmail
在 app/Jobs/SendEmail.php 中写逻辑。
php artisan queue:work
或(处理完就退出):
php artisan queue:listen
⚠️ 手动方式不适合生产环境。
yum install -y epel-release
yum install -y supervisor
systemctl enable supervisord
systemctl start supervisord
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根据你的项目调整
supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker:*
查看状态:
supervisorctl status
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
chown -R nginx:nginx storage bootstrap/cache
storage/logs/laravel.logphp artisan queue:failedphp artisan queue:restart
如果你愿意,可以告诉我:
我可以给你更精确的配置示例。