在 CentOS 上部署 Laravel 时,和 Ubuntu 类似,但有一些 CentOS 特有的坑和注意事项。下面按「环境准备 → 部署 → 权限 → 优化 → 常见坑」给你总结一份实用清单。
SELinux 是 CentOS 最容易踩坑的地方
# 临时关闭
setenforce 0
# 永久关闭(重启生效)
vi /etc/selinux/config
SELINUX=disabled
如果是生产环境,建议 学习配置 SELinux 策略,而不是简单关闭。
yum install nginx php php-fpm php-mysqlnd php-mbstring php-xml php-json
启动:
systemctl start nginx
systemctl start php-fpm
CentOS 下 Apache 默认是 prefork + mod_php,性能一般。
Laravel 对 PHP 版本要求较新:
| Laravel | PHP |
|---|---|
| 8.x | ≥ 7.3 |
| 9.x | ≥ 8.0 |
| 10.x | ≥ 8.1 |
| 11.x | ≥ 8.2 |
⚠️ CentOS 自带 PHP 很旧(5.4 / 7.2),不要用 yum 默认源
推荐:
Laravel 必须可写目录:
storage
bootstrap/cache
正确做法(Nginx 用户通常是 nginx):
chown -R nginx:nginx /var/www/laravel
chmod -R 755 /var/www/laravel
chmod -R 777 storage bootstrap/cache
❌ 不要 chmod 777 整个项目
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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
部署时:
composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate
生产环境优化:
php artisan config:cache
php artisan route:cache
php artisan view:cache
修改 .env 后一定要:
php artisan config:clear
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
常见报错:
Class 'PDO' not found → 缺 php-pdoMbstring → 缺 php-mbstringdate.timezone = Asia/Shanghai
报错:
The stream or file could not be opened
git clone
composer install --no-dev
cp .env.example .env
php artisan key:generate
chown -R nginx:nginx .
php artisan migrate --force
如果你愿意,我可以:
你现在是 个人项目还是生产环境?