在 CentOS 系统上部署 Laravel 项目时,处理静态资源(如 CSS、JavaScript、图片等)通常涉及以下几个步骤:
安装 Nginx 或 Apache:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
配置 Nginx:
/etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf
。server {
listen 80;
server_name your_domain.com; # 替换为你的域名或 IP 地址
root /path/to/your/laravel/project; # 替换为你的 Laravel 项目路径
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # 或者使用 127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
sudo systemctl reload nginx
处理静态资源:
npm install
npm run production
public
目录下。设置文件权限:
nginx
或 www-data
)有权限访问你的 Laravel 项目目录和静态资源目录。你可以使用以下命令来设置权限:sudo chown -R nginx:nginx /path/to/your/laravel/project
sudo chmod -R 755 /path/to/your/laravel/project
清除缓存:
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
通过以上步骤,你应该能够在 CentOS 系统上成功部署 Laravel 项目并处理静态资源。如果有任何问题,请检查 Nginx 和 PHP-FPM 的日志文件以获取更多信息。