You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Laravel项目从本地部署到远程服务器遇问题求助

Laravel 7.x 部署到Aruba共享服务器问题解决指南

1. 修复.env文件暴露风险

Aruba共享服务器默认允许直接访问.env这类敏感文件,必须先添加限制规则:
在项目根目录(laravel-project)的.htaccess中,将以下规则放在Rewrite规则之前:

# 禁止访问敏感配置文件
<FilesMatch "\.(env|env\.local|env\.example|env\.testing)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

2. 实现URL不带public并修复403/跳转404问题

当前根目录的Rewrite规则不适配子目录部署场景,替换为以下内容:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 适配子路径的重写规则
    RewriteBase /laravel/laravel-project/
    RewriteRule ^$ public/ [L]
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

同时确认public目录下的.htaccess保持Laravel 7.x默认配置:

<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>

3. .env文件关键配置调整

  • APP_URL必须设置为项目的访问根路径:
APP_URL=https://www.mydomain.it/laravel/laravel-project
  • 确保APP_KEY与本地项目一致(直接复制本地.env中的对应值即可)
  • 根据环境设置APP_ENV:测试环境可设为testing,生产环境设为production

4. 服务器权限调整

Aruba共享服务器对权限有严格要求:

  • 将storage和bootstrap/cache目录权限设置为755
  • 所有文件权限设置为644(避免不必要的执行权限)

5. 修复跳转后404问题

由于项目部署在子目录,需确保路由能正确识别路径:

  • 无需修改Route::redirect('/', '/posts');这条路由,只要APP_URL配置正确,Laravel会自动处理子路径路由
  • 清除路由缓存:若有SSH权限,执行命令:
php artisan route:clear

若无SSH权限,直接删除bootstrap/cache/routes.php文件即可


内容的提问来源于stack exchange,提问作者Stefano Pagani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 06:59:53