問題描述

參考官方文件部署 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.