温馨提示×

Debian如何管Laravel服务

小樊
50
2026-09-11 05:40:42
栏目: 智能运维

在 Debian 上“管 Laravel 服务”通常分几种场景,下面按最常见、最推荐的方式给你一套清晰做法。


一、开发环境(本地 / 测试)

1️⃣ 使用 Laravel 自带服务

cd /var/www/laravel
php artisan serve
  • 默认:http://127.0.0.1:8000
  • 仅适合开发,不适合生产

二、生产环境(推荐方式)

✅ 方案 A:Nginx + PHP-FPM(最常用)

1. 安装依赖

sudo apt update
sudo apt install nginx php-fpm php-cli php-mysql php-mbstring php-xml php-curl

2. Nginx 配置示例

server {
    listen 80;
    server_name example.com;
    root /var/www/laravel/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
}

3. 权限设置

sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 755 /var/www/laravel/storage

4. 启动服务

sudo systemctl restart nginx
sudo systemctl restart php-fpm

✅ Laravel 本身不需要“启动”,由 Web 服务器调用


三、队列 / 定时任务(Laravel 特有)

1️⃣ 队列(Queue)

php artisan queue:work

用 systemd 管理(推荐)

创建:

sudo nano /etc/systemd/system/laravel-queue.service

内容:

[Unit]
Description=Laravel Queue Worker
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/laravel
ExecStart=/usr/bin/php artisan queue:work --queue=default
Restart=always

[Install]
WantedBy=multi-user.target

启用:

sudo systemctl enable laravel-queue
sudo systemctl start laravel-queue

2️⃣ 定时任务(Cron)

crontab -e

添加:

* * * * * cd /var/www/laravel && php artisan schedule:run >> /dev/null 2>&1

四、Supervisor(队列更稳定)

sudo apt install supervisor

配置:

sudo nano /etc/supervisor/conf.d/laravel.conf
[program:laravel]
command=php /var/www/laravel/artisan queue:work
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel

五、常见管理命令总结

# 查看服务
systemctl status nginx
systemctl status php-fpm

# 重启
systemctl restart nginx

# Laravel
php artisan config:clear
php artisan cache:clear
php artisan migrate

✅ 推荐生产架构

Debian
 ├─ Nginx
 ├─ PHP-FPM
 ├─ Laravel
 ├─ Supervisor / systemd(队列)
 └─ Cron(调度)

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

  • 是 开发还是生产
  • 用 Nginx 还是 Apache
  • 是否用 Docker

我可以直接给你一套完整配置。

0 踩