在 Debian 上跑 Laravel,和在其他 Linux 发行版上基本思路一致,但 Debian 有一些“发行版特性”和常见坑需要注意。下面按系统层面 → 运行环境 → Laravel 本身 → 安全与运维来梳理。
Laravel 对 PHP 版本要求较新:
| Laravel | PHP |
|---|---|
| 10 | ≥ 8.1 |
| 11 | ≥ 8.2 |
Debian 默认源 PHP 可能偏旧,常见做法:
sudo apt install php php-cli php-fpm php-mbstring php-xml php-curl php-mysql php-zip php-gd php-bcmath
如需要新 PHP:
sudo apt install lsb-release ca-certificates apt-transport-https
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update
Debian 上 Nginx + PHP-FPM 是最稳组合
注意:
php-fpm sock 路径fastcgi 权限client_max_body_size示例关键点:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
mod_rewritempm_event + php-fpmLaravel 需要写:
storagebootstrap/cache错误示例:
The stream or file could not be opened
正确做法(不要 777):
sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 755 /var/www/laravel
sudo chmod -R 775 storage bootstrap/cache
如果你用部署用户(如 deploy):
sudo usermod -aG www-data deploy
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
php artisan view:cache
Debian 不要用 php artisan serve 跑生产
推荐:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel/artisan queue:work
autostart=true
autorestart=true
user=www-data
* * * * * www-data php /var/www/laravel/artisan schedule:run
Debian 12 默认是 MariaDB 10.11
注意:
utf8mb4debug(APP_DEBUG=false).env 访问location ~ /\.env {
deny all;
}
apt upgrade✅ PHP 版本太低
✅ storage 没写权限
✅ 队列没常驻
✅ 用 root 跑 Laravel
✅ 没关 debug
✅ 用 artisan serve 上生产
如果你愿意,可以告诉我:
我可以直接给你一份可复制的部署方案。