Laravel部署至Azure Web App遇阻:非Zip包方式部署失败
Laravel部署Azure Linux Web App的两种问题解决方案
一、解决WEBSITE_RUN_FROM_PACKAGE=1时storage目录不可写的问题
这种模式下ZIP包为只读挂载,需将Laravel的storage目录指向Azure提供的可写路径/home/site/storage:
修改Laravel的storage路径配置
在bootstrap/app.php中添加以下代码,让应用支持通过环境变量指定storage路径:$app->useStoragePath(env('APP_STORAGE', base_path('storage')));添加Azure应用环境变量
在Azure门户的Web App「配置」→「应用程序设置」中,新增环境变量:- 名称:
APP_STORAGE - 值:
/home/site/storage
- 名称:
创建可写的storage目录并设置权限
通过Azure Web App的SSH终端执行以下命令:mkdir -p /home/site/storage/{app,framework,logs} chmod -R 775 /home/site/storage chown -R www-data:www-data /home/site/storage也可将这一步整合到Github Actions部署脚本,通过Azure CLI远程执行:
- name: Create and set permissions for storage directory uses: azure/CLI@v1 with: inlineScript: | az webapp ssh --name ${{ secrets.AZURE_WEBAPP_NAME }} --resource-group ${{ secrets.AZURE_RESOURCE_GROUP }} --command "mkdir -p /home/site/storage/{app,framework,logs} && chmod -R 775 /home/site/storage && chown -R www-data:www-data /home/site/storage"
二、解决WEBSITE_RUN_FROM_PACKAGE=0时部署卡在增量同步的问题
该问题多因增量部署的文件冲突、权限问题或部署配置导致,可按以下步骤处理:
改用完全部署模式
在Github Actions的Azure部署步骤中启用clean参数,强制完全部署避免增量同步:- name: Deploy to Azure Web App uses: Azure/webapps-deploy@v2 with: app-name: ${{ secrets.AZURE_WEBAPP_NAME }} slot-name: production publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} package: . clean: true # 启用完全清理部署优化部署包内容
确保部署包只包含必要文件:构建阶段生成vendor和前端编译文件后,排除node_modules、.git等冗余目录,可通过.gitignore或部署脚本过滤实现。修复目标目录权限
部署前通过Azure CLI设置/home/site/wwwroot的权限:- name: Fix wwwroot directory permissions uses: azure/CLI@v1 with: inlineScript: | az webapp ssh --name ${{ secrets.AZURE_WEBAPP_NAME }} --resource-group ${{ secrets.AZURE_RESOURCE_GROUP }} --command "chmod -R 775 /home/site/wwwroot && chown -R www-data:www-data /home/site/wwwroot"查看详细部署日志
登录Azure门户,进入Web App的「Deployment Center」→「Logs」,查看底层部署日志,定位具体卡住的文件或操作。
内容的提问来源于stack exchange,提问作者TheEgg
相关产品推荐
相关产品推荐

