下面给你一个在 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
假设:
/var/www/laravelexample.comsudo nano /etc/nginx/sites-available/laravel
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
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
cd /var/www/laravel
cp .env.example .env
php artisan key:generate
确保 .env 中:
APP_ENV=production
APP_DEBUG=false
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
systemctl status php8.1-fpm
publicsudo chown -R www-data:www-data /var/www/laravel
如果你愿意,我可以:
你现在是 开发环境还是生产环境?