温馨提示×

laravel在debian怎么配nginx

小樊
42
2026-10-16 22:24:58
栏目: 智能运维

下面给你一个在 Debian 上用 Nginx 运行 Laravel 的完整配置示例(以 Debian 11/12、PHP 8.1+ 为例)。


一、安装必要软件

sudo apt update
sudo apt install nginx php-fpm php-cli php-mysql php-mbstring php-xml php-curl php-zip unzip git -y

查看 PHP 版本和 fpm socket:

php -v
ls /run/php/
# 通常是 /run/php/php8.1-fpm.sock

二、配置 Nginx 站点

假设:

  • 项目路径:/var/www/laravel
  • 域名:example.com

1. 创建 Nginx 配置

sudo nano /etc/nginx/sites-available/laravel

2. 写入配置

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/laravel/public;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

⚠️ 注意:php8.1-fpm.sock 改成你实际的 PHP 版本


三、启用站点

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

四、Laravel 项目权限

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

五、Laravel 环境配置

cd /var/www/laravel
cp .env.example .env
php artisan key:generate

确保 .env 中:

APP_ENV=production
APP_DEBUG=false

六、可选:HTTPS(推荐)

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

七、常见问题排查

1. 502 Bad Gateway

  • PHP-FPM 没启动
systemctl status php8.1-fpm

2. 403 Forbidden

  • root 路径写错
  • 没指向 public

3. 权限问题

sudo chown -R www-data:www-data /var/www/laravel

如果你愿意,我可以:

  • 给你 Laravel + Nginx + Queue + Supervisor 配置
  • 或 Docker / Debian 最小化生产配置
  • 或针对 Laravel 10 / 11 的优化

你现在是 开发环境还是生产环境?

0 踩