如何将Laravel项目部署到子域名(无数据库)?部署后仅显示文件不执行如何解决?
解决Laravel项目显示文件列表无法运行的问题
1. 调整服务器根目录指向
Laravel的入口文件在public目录下,必须让Web服务器将子域名的根目录指向项目的public文件夹,而非项目根目录:
- Apache:修改虚拟主机配置,将
DocumentRoot设置为/path/to/your/laravel-project/public - Nginx:修改server块中的
root配置为/path/to/your/laravel-project/public;,同时确保index项包含index.php
2. 修复目录权限
确保Laravel的storage和bootstrap/cache目录具备写入权限,执行以下命令:
chmod -R 755 storage bootstrap/cache chown -R www-data:www-data storage bootstrap/cache # 适配Apache/Nginx的运行用户
3. 配置Apache的.htaccess文件
若public目录下无.htaccess文件,添加以下内容:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # 处理授权头 RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # 移除末尾斜杠 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # 转发请求到入口文件 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
同时确认Apache已开启mod_rewrite模块。
4. 配置Nginx的URL重写
在Nginx的server块中添加重写规则:
location / { try_files $uri $uri/ /index.php?$query_string; }
5. 清除Laravel缓存
完成上述配置后,执行缓存清除命令:
php artisan cache:clear php artisan config:clear php artisan route:clear
内容的提问来源于stack exchange,提问作者Bayaz
相关产品推荐
相关产品推荐

