【Azure 应用服务】App Service For Linux 部署PHP Laravel 项目,如何修改首页路径为 wwwroot\public\index.php

简介: 【Azure 应用服务】App Service For Linux 部署PHP Laravel 项目,如何修改首页路径为 wwwroot\public\index.php

问题描述

参考官方文档部署 PHP Laravel 项目到App Service for Linux环境中,但是访问应用时候遇见了500 Server Error 错误。

从部署的日志中,可以明确看出部署是成功的,那么为什么启动页面不成功呢?

 

问题分析

Laravel 应用程序生命周期在 public 目录中开始,而不是在应用程序的根目录(wwwroot)中开始。在Azure App Service中,所有项目的默认根目录都是wwwroot。

但是可以使用 .htaccess 来重写所有请求,使之指向 /public 而不是根目录。 在存储库根目录中,已针对此目的添加了 .htaccess。 有了它即可部署 Laravel 应用程序。

.htaccess文件(或者"分布式配置文件"),全称是Hypertext Access(超文本入口)。提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录。

 

.htaccess 详解:https://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html

.htaccess 使用介绍:https://www.cnblogs.com/gyrgyr/p/10773118.html

问题解决

更改站点根路径,所选的 Web 框架可能使用子目录作为站点根路径。 例如Laravel 使用 public/ 子目录作为站点根路径。

应用服务的默认 PHP 映像使用 Apache,不允许为应用自定义站点根路径。 若要避开此限制,请将 .htaccess 文件添加到存储库根路径,并包含以下内容:

<IfModule mod_rewrite.c>

   RewriteEngine on

   RewriteCond %{REQUEST_URI} ^(.*)

   RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]

</IfModule>

以上为修改Laravel项目的根目录到 wwwroot/public中。如要指定到具体的启动页面,可以在public中加入页面名称,如 index.php。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)
RewriteRule ^(.*)$ public/index.php?/$1 [L]
</IfModule>

 

参考资料

App Service PHP laravel 更改站点根路径: https://docs.azure.cn/zh-cn/app-service/configure-language-php?pivots=platform-linux#change-site-root

 

在 Azure 应用服务中生成 PHP 和 MySQL 应用: https://docs.azure.cn/zh-cn/app-service/tutorial-php-mysql-app?pivots=platform-linux

 

Laravel Lifecycle Overview: https://laravel.com/docs/5.4/lifecycle

First Things

The entry point for all requests to a Laravel application is the public/index.php file. All requests are directed to this file by your web server (Apache / Nginx) configuration. The index.php file doesn't contain much code. Rather, it is simply a starting point for loading the rest of the framework.

 

Apache's mod_rewrite: http://www.datakoncepts.com/seo or https://httpd.apache.org/docs/current/mod/mod_rewrite.html

The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.

mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. Each rule can have an unlimited number of attached rule conditions, to allow you to rewrite URL based on server variables, environment variables, HTTP headers, or time stamps.

mod_rewrite operates on the full URL path, including the path-info section. A rewrite rule can be invoked in httpd.conf or in .htaccess. The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

相关文章
|
1月前
|
Java 应用服务中间件 API
【App Service】部署War包到Azure云上遇404错误
Java应用部署至Azure App Service for Windows后报404,本地运行正常。经排查,日志提示类文件版本不兼容:应用由Java 17(class file version 61.0)编译,但环境仅支持到Java 11(55.0)。错误根源为Java版本不匹配。调整App Service的Java版本至17后问题解决,成功访问接口。
134 1
|
6月前
|
人工智能 文件存储 数据中心
Ollama部署本地大模型并通过Infortress APP远程访问保姆级教程
本文介绍如何快速上手本地大模型部署工具Ollama及AI远程访问工具Infortress。通过Ollama,开发者可轻松部署如Llama、Deepseek等主流开源模型,仅需几行命令即可完成安装与运行。结合Infortress,用户能实现对本地大模型的远程访问,支持多设备无缝对接,同时提供便捷的模型切换与知识库管理功能。Infortress更兼具NAS软件特性,成为个人AI数据中心的理想选择。
|
6月前
|
Java 关系型数据库 MySQL
在Linux平台上进行JDK、Tomcat、MySQL的安装并部署后端项目
现在,你可以通过访问http://Your_IP:Tomcat_Port/Your_Project访问你的项目了。如果一切顺利,你将看到那绚烂的胜利之光照耀在你的项目之上!
386 41
|
5月前
|
域名解析 监控 NoSQL
即时通讯APP应用开发的部署策略
随着移动互联网发展,即时通讯APP成为生活和工作的必备工具。本文探讨其开发部署的关键环节,包括用户界面设计、通讯协议选择、数据库设计与服务器搭建等方面,以及部署过程中的环境准备、应用打包、服务器部署、域名解析和监控维护等步骤。通过优化每个环节,确保APP稳定高效运行,提升用户体验,在市场中保持竞争力。
|
9月前
|
应用服务中间件 Linux nginx
【Azure App Service】基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?
基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?Web App Linux 默认使用的 Nginx 版本是由平台预定义的,无法更改这个版本。
264 77
|
10月前
|
消息中间件 Linux
Linux:进程间通信(共享内存详细讲解以及小项目使用和相关指令、消息队列、信号量)
通过上述讲解和代码示例,您可以理解和实现Linux系统中的进程间通信机制,包括共享内存、消息队列和信号量。这些机制在实际开发中非常重要,能够提高系统的并发处理能力和数据通信效率。希望本文能为您的学习和开发提供实用的指导和帮助。
765 20
|
11月前
|
网络协议 容器
【Container App】部署Contianer App 遇见 Failed to deploy new revision: The Ingress's TargetPort or ExposedPort must be specified for TCP apps.
Failed to deploy new revision: The Ingress's TargetPort or ExposedPort must be specified for TCP apps.
172 27
|
开发框架 监控 .NET
【Azure App Service】部署在App Service上的.NET应用内存消耗不能超过2GB的情况分析
x64 dotnet runtime is not installed on the app service by default. Since we had the app service running in x64, it was proxying the request to a 32 bit dotnet process which was throwing an OutOfMemoryException with requests >100MB. It worked on the IaaS servers because we had the x64 runtime install
215 5