在 CentOS 上部署/安装 Laravel,比起 Ubuntu 有一些坑和注意点,下面按「系统环境 → PHP → 依赖 → 权限 → 常见坑」给你梳理一遍。
CentOS 8 Stream / Rocky Linux 9
Laravel 对 PHP 版本要求严格:
| Laravel | PHP |
|---|---|
| 9.x | ≥ 8.0 |
| 10.x | ≥ 8.1 |
| 11.x | ≥ 8.2 |
✅ 不要用系统自带 PHP
yum install -y epel-release
yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
yum module reset php
yum module enable php:remi-8.2
yum install php php-cli php-fpm php-mysqlnd php-mbstring php-xml php-curl php-zip php-gd php-bcmath
检查:
php -v
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer -v
⚠️ 注意:
composer config -g repos.packagist composer https://mirrors.aliyun.com/composer/
php-mbstring
php-xml
php-curl
php-zip
php-gd
php-bcmath
php-openssl(默认有)
少一个都可能报:
Class ‘XXX’ not found
Laravel 需要写:
storage
bootstrap/cache
✅ 正确做法(不要用 777):
chown -R nginx:nginx /var/www/laravel
chmod -R 755 /var/www/laravel
chmod -R 775 storage bootstrap/cache
如果你用 php artisan serve 测试,也要确保当前用户有权限。
yum install nginx
systemctl enable --now nginx php-fpm
server {
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
⚠️ 注意:
publicsetsebool -P httpd_read_user_content 1
报错常见于:
查看:
getenforce
临时关闭(不推荐长期):
setenforce 0
正确做法:
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/laravel/storage(/.*)?"
restorecon -Rv /var/www/laravel
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
cp .env.example .env
php artisan key:generate
⚠️ 生产环境:
| 现象 | 原因 |
|---|---|
| 500 空白 | storage 权限 |
| 502 | php-fpm 没起 / sock 路径错 |
| 404 | Nginx root 没指 public |
| composer 慢 | 没换镜像 |
| class not found | 扩展缺失 |
CentOS 装 Laravel = 新 PHP + Composer + 正确权限 + Nginx + SELinux
如果你愿意,我可以: