設定適用于 Azure App Service 的 PHP 應用程式Configure a PHP app for Azure App Service

本指南說明如何在 Azure App Service 中設定您的 PHP web 應用程式、行動後端和 API 應用程式。This guide shows you how to configure your PHP web apps, mobile back ends, and API apps in Azure App Service.

本指南針對將應用程式部署至 App Service 的 PHP 開發人員提供重要概念和指示。This guide provides key concepts and instructions for PHP developers who deploy apps to App Service. 如果您從未使用過 Azure App Service,請先遵循 PHP 快速入門搭配使用 PHP 和 MySQL 教學課程If you've never used Azure App Service, follow the PHP quickstart and PHP with MySQL tutorial first.

顯示 PHP 版本Show PHP version

若要顯示目前的 PHP 版本,請在 Cloud Shell 中執行下列命令:To show the current PHP version, run the following command in the Cloud Shell:

az webapp config show --resource-group <resource-group-name> --name <app-name> --query phpVersion

若要顯示所有支援的 PHP 版本,請在 Cloud Shell 中執行下列命令:To show all supported PHP versions, run the following command in the Cloud Shell:

az webapp list-runtimes | grep php

若要顯示目前的 PHP 版本,請在 Cloud Shell 中執行下列命令:To show the current PHP version, run the following command in the Cloud Shell:

az webapp config show --resource-group <resource-group-name> --name <app-name> --query linuxFxVersion

若要顯示所有支援的 PHP 版本,請在 Cloud Shell 中執行下列命令:To show all supported PHP versions, run the following command in the Cloud Shell:

az webapp list-runtimes --linux | grep PHP

設定 PHP 版本Set PHP version

Cloud Shell 中執行下列命令,以將 PHP 版本設定為7.4:Run the following command in the Cloud Shell to set the PHP version to 7.4:

az webapp config set --name <app-name> --resource-group <resource-group-name> --php-version 7.4

Cloud Shell 中執行下列命令,以將 PHP 版本設定為 7.2:Run the following command in the Cloud Shell to set the PHP version to 7.2:

az webapp config set --name <app-name> --resource-group <resource-group-name> --linux-fx-version "PHP|7.2"

執行編輯器Run Composer

如果您想要 App Service 在部署階段執行 編輯器 ,最簡單的方式就是在您的存放庫中包含編輯器。If you want App Service to run Composer at deployment time, the easiest way is to include the Composer in your repository.

從本機終端機視窗中,將目錄變更為您的存放庫根目錄,然後依照 下載編輯器 中的指示,將 >composer.phar 下載至目錄根目錄。From a local terminal window, change directory to your repository root, and follow the instructions at download Composer to download composer.phar to the directory root.

(您需要安裝 npm) ,請執行下列命令:Run the following commands (you need npm installed):

npm install kuduscript -g
kuduscript --node --scriptType bash --suppressPrompt

您的存放庫根目錄現在有兩個額外的檔案: . deploymentdeploy.shYour repository root now has two additional files: .deployment and deploy.sh.

開啟 deploy.sh 並尋找 Deployment 區段,如下所示:Open deploy.sh and find the Deployment section, which looks like this:

##################################################################################################################################
# Deployment
# ----------

在區段 結尾 新增執行必要工具所需的程式碼區段 DeploymentAdd the code section you need to run the required tool at the end of the Deployment section:

# 4. Use composer
echo "$DEPLOYMENT_TARGET"
if [ -e "$DEPLOYMENT_TARGET/composer.json" ]; then
  echo "Found composer.json"
  pushd "$DEPLOYMENT_TARGET"
  php composer.phar install $COMPOSER_ARGS
  exitWithMessageOnError "Composer install failed"
  popd
fi

認可所有變更,並使用 Git 部署您的程式碼,或使用啟用組建自動化的 Zip 部署。Commit all your changes and deploy your code using Git, or Zip deploy with build automation enabled. 編輯器現在應該在部署自動化中執行。Composer should now be running as part of deployment automation.

執行 Grunt/Bower/GulpRun Grunt/Bower/Gulp

如果您想要 App Service 在部署期間執行熱門的自動化工具,例如 Grunt、Bower 或 Gulp,您需要提供 自訂部署腳本If you want App Service to run popular automation tools at deployment time, such as Grunt, Bower, or Gulp, you need to supply a custom deployment script. 當您使用 Git 進行部署,或使用啟用組建自動化的 Zip 部署 時,App Service 執行此腳本。App Service runs this script when you deploy with Git, or with Zip deployment with build automation enabled.

若要讓您的存放庫能夠執行這些工具,您必須將它們新增至package.js的相依性。To enable your repository to run these tools, you need to add them to the dependencies in package.json. 例如:For example:

"dependencies": {
  "bower": "^1.7.9",
  "grunt": "^1.0.1",
  "gulp": "^3.9.1",
  ...
}

從本機終端機視窗中,將目錄變更為您的存放庫根目錄,然後執行下列命令 (您需要安裝 npm) :From a local terminal window, change directory to your repository root and run the following commands (you need npm installed):

npm install kuduscript -g
kuduscript --node --scriptType bash --suppressPrompt

您的存放庫根目錄現在有兩個額外的檔案: . deploymentdeploy.shYour repository root now has two additional files: .deployment and deploy.sh.

開啟 deploy.sh 並尋找 Deployment 區段,如下所示:Open deploy.sh and find the Deployment section, which looks like this:

##################################################################################################################################
# Deployment
# ----------

本節的結尾是執行 npm install --productionThis section ends with running npm install --production. 在區段 結尾 新增執行必要工具所需的程式碼區段 DeploymentAdd the code section you need to run the required tool at the end of the Deployment section:

請參閱 MEAN.js 範例中的範例,其中部署腳本也會執行自訂 npm install 命令。See an example in the MEAN.js sample, where the deployment script also runs a custom npm install command.

BowerBower

此程式碼片段會執行 bower installThis snippet runs bower install.

if [ -e "$DEPLOYMENT_TARGET/bower.json" ]; then
  cd "$DEPLOYMENT_TARGET"
  eval ./node_modules/.bin/bower install
  exitWithMessageOnError "bower failed"
  cd - > /dev/null
fi

GulpGulp

此程式碼片段會執行 gulp imageminThis snippet runs gulp imagemin.

if [ -e "$DEPLOYMENT_TARGET/gulpfile.js" ]; then
  cd "$DEPLOYMENT_TARGET"
  eval ./node_modules/.bin/gulp imagemin
  exitWithMessageOnError "gulp failed"
  cd - > /dev/null
fi

GruntGrunt

此程式碼片段會執行 gruntThis snippet runs grunt.

if [ -e "$DEPLOYMENT_TARGET/Gruntfile.js" ]; then
  cd "$DEPLOYMENT_TARGET"
  eval ./node_modules/.bin/grunt
  exitWithMessageOnError "Grunt failed"
  cd - > /dev/null
fi

自訂組建自動化Customize build automation

如果您使用 Git 或 zip 套件並開啟組建自動化來部署應用程式,App Service 組建自動化將會依下列順序逐步執行:If you deploy your app using Git or zip packages with build automation turned on, the App Service build automation steps through the following sequence:

  1. 執行自訂指令碼 (如果 PRE_BUILD_SCRIPT_PATH 已指定)。Run custom script if specified by PRE_BUILD_SCRIPT_PATH.
  2. 執行 php composer.phar installRun php composer.phar install.
  3. 執行自訂指令碼 (如果 POST_BUILD_SCRIPT_PATH 已指定)。Run custom script if specified by POST_BUILD_SCRIPT_PATH.

PRE_BUILD_COMMANDPOST_BUILD_COMMAND 是預設為空值的環境變數。PRE_BUILD_COMMAND and POST_BUILD_COMMAND are environment variables that are empty by default. 若要執行建置前命令,請定義 PRE_BUILD_COMMANDTo run pre-build commands, define PRE_BUILD_COMMAND. 若要執行建置後命令,請定義 POST_BUILD_COMMANDTo run post-build commands, define POST_BUILD_COMMAND.

下列範例會將兩個變數指定給一系列的命令 (以逗號分隔)。The following example specifies the two variables to a series of commands, separated by commas.

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PRE_BUILD_COMMAND="echo foo, scripts/prebuild.sh"
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings POST_BUILD_COMMAND="echo foo, scripts/postbuild.sh"

若要了解其他可自訂組建自動化的環境變數,請參閱 Oryx 設定For additional environment variables to customize build automation, see Oryx configuration.

若要深入瞭解 App Service 如何在 Linux 中執行和建置 PHP 應用程式,請參閱 Oryx 文件:如何偵測和建置 PHP 應用程式For more information on how App Service runs and builds PHP apps in Linux, see Oryx documentation: How PHP apps are detected and built.

自訂啟動Customize start-up

根據預設,內建 PHP 容器會執行 Apache 伺服器。By default, the built-in PHP container runs the Apache server. 它會在啟動時執行 apache2ctl -D FOREGROUND"At start-up, it runs apache2ctl -D FOREGROUND". 如有需要,您可以在啟動時執行不同的命令,方法是在 Cloud Shell 中執行下列命令:If you like, you can run a different command at start-up, by running the following command in the Cloud Shell:

az webapp config set --resource-group <resource-group-name> --name <app-name> --startup-file "<custom-command>"

存取環境變數Access environment variables

在 App Service 中,您可以於應用程式的程式碼外部設定應用程式設定In App Service, you can set app settings outside of your app code. 然後,您可以使用標準的 getenv() 模式來存取這些設定。Then you can access them using the standard getenv() pattern. 例如,若要存取稱為 DB_HOST 的應用程式設定,請使用下列程式碼:For example, to access an app setting called DB_HOST, use the following code:

getenv("DB_HOST")

變更網站根目錄Change site root

您選擇的 web 架構可能會使用子目錄作為網站根目錄。The web framework of your choice may use a subdirectory as the site root. 例如, Laravel會使用 公用/ 子目錄作為網站根目錄。For example, Laravel, uses the public/ subdirectory as the site root.

若要自訂網站根目錄,請使用命令來設定應用程式的虛擬應用程式路徑 az resource updateTo customize the site root, set the virtual application path for the app by using the az resource update command. 下列範例會將網站根目錄設定為存放庫中的 公用/ 子目錄。The following example sets the site root to the public/ subdirectory in your repository.

az resource update --name web --resource-group <group-name> --namespace Microsoft.Web --resource-type config --parent sites/<app-name> --set properties.virtualApplications[0].physicalPath="site\wwwroot\public" --api-version 2015-06-01

依預設,Azure App Service 會將根虛擬應用程式路徑 ( / ) 指向已部署應用程式檔案的根目錄 (sites\wwwroot)。By default, Azure App Service points the root virtual application path (/) to the root directory of the deployed application files (sites\wwwroot).

您選擇的 web 架構可能會使用子目錄作為網站根目錄。The web framework of your choice may use a subdirectory as the site root. 例如,Laravel 使用 public/ 子目錄作為網站根目錄。For example, Laravel, uses the public/ subdirectory as the site root.

App Service 的預設 PHP 映像會使用 Apache,且不會讓您自訂應用程式的網站根目錄。The default PHP image for App Service uses Apache, and it doesn't let you customize the site root for your app. 若要避開這項限制,請將 .htaccess 檔案新增至存放庫根目錄,其中包含下列內容:To work around this limitation, add an .htaccess file to your repository root with the following content:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^(.*)
    RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
</IfModule>

如果您不要使用 .htaccess 重寫,您可以改為使用自訂 Docker 映像來部署 Laravel 應用程式。If you would rather not use .htaccess rewrite, you can deploy your Laravel application with a custom Docker image instead.

偵測 HTTPS 工作階段Detect HTTPS session

在 App Service 中,SSL 終止會在網路負載平衡器上發生,因此所有的 HTTPS 要求都會以未加密 HTTP 要求的形式進入您的應用程式。In App Service, SSL termination happens at the network load balancers, so all HTTPS requests reach your app as unencrypted HTTP requests. 如果您的應用程式邏輯需要檢查使用者要求是否有加密,請檢查 X-Forwarded-Proto 標頭。If your app logic needs to check if the user requests are encrypted or not, inspect the X-Forwarded-Proto header.

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
// Do something when HTTPS is used
}

熱門的 Web 架構可讓您在標準的應用程式模式中存取 X-Forwarded-* 資訊。Popular web frameworks let you access the X-Forwarded-* information in your standard app pattern. CodeIgniter 中,is_https() 預設會檢查 X_FORWARDED_PROTO 的值。In CodeIgniter, the is_https() checks the value of X_FORWARDED_PROTO by default.

自訂 php .ini 設定Customize php.ini settings

如果您需要對 PHP 安裝進行變更,您可以依照下列步驟來變更 php.ini 指示詞If you need to make changes to your PHP installation, you can change any of the php.ini directives by following these steps.

注意

查看 PHP 版本和目前 php.ini 設定的最佳方法是在應用程式中呼叫 phpinfo ()The best way to see the PHP version and the current php.ini configuration is to call phpinfo() in your app.

自訂非 PHP_INI_SYSTEM 指示詞Customize-non-PHP_INI_SYSTEM directives

若要自訂 PHP_INI_USER、PHP_INI_PERDIR 和 PHP_INI_ALL 指示詞 (查看 php.ini 指示詞) ,請將檔案新增 .user.ini 至您應用程式的根目錄。To customize PHP_INI_USER, PHP_INI_PERDIR, and PHP_INI_ALL directives (see php.ini directives), add a .user.ini file to the root directory of your app.

使用在 php.ini 檔案中使用的相同語法,將組態設定新增至 .user.ini 檔案。Add configuration settings to the .user.ini file using the same syntax you would use in a php.ini file. 例如,如果您要開啟 display_errors 設定,並將 upload_max_filesize 設定設為 10M,則 .user.ini 檔案將包含下列文字:For example, if you wanted to turn on the display_errors setting and set upload_max_filesize setting to 10M, your .user.ini file would contain this text:

 ; Example Settings
 display_errors=On
 upload_max_filesize=10M

 ; Write errors to d:\home\LogFiles\php_errors.log
 ; log_errors=On

經由變更重新部署應用程式,並重新啟動應用程式。Redeploy your app with the changes and restart it.

除了使用檔案 .user.ini 以外,您也可以在應用程式中使用 ini_set ( # B1 來自訂這些非 PHP_INI_SYSTEM 指示詞。As an alternative to using a .user.ini file, you can use ini_set() in your app to customize these non-PHP_INI_SYSTEM directives.

若要自訂 PHP_INI_USER、PHP_INI_PERDIR 和 PHP_INI_ALL 指示詞 (請參閱 php.ini指示詞),請將 .htaccess 檔案新增至應用程式的根目錄。To customize PHP_INI_USER, PHP_INI_PERDIR, and PHP_INI_ALL directives (see php.ini directives), add an .htaccess file to the root directory of your app.

.htaccess 檔案中,使用 php_value <directive-name> <value> 語法加入指示詞。In the .htaccess file, add the directives using the php_value <directive-name> <value> syntax. 例如:For example:

php_value upload_max_filesize 1000M
php_value post_max_size 2000M
php_value memory_limit 3000M
php_value max_execution_time 180
php_value max_input_time 180
php_value display_errors On
php_value upload_max_filesize 10M

經由變更重新部署應用程式,並重新啟動應用程式。Redeploy your app with the changes and restart it. 如果您使用 Kudu (例如使用 Git) 部署應用程式,則應用程式會在部署後自動重新啟動。If you deploy it with Kudu (for example, using Git), it's automatically restarted after deployment.

除了使用 .htaccess之外,您還可以在應用程式中使用 ini_set () 來自訂這些非 PHP_INI_SYSTEM 的指示詞。As an alternative to using .htaccess, you can use ini_set() in your app to customize these non-PHP_INI_SYSTEM directives.

自訂 PHP_INI_SYSTEM 指示詞Customize PHP_INI_SYSTEM directives

若要自訂 PHP_INI_SYSTEM 指示詞 (請參閱 php.ini 指示詞),您不能使用 .htaccess 方法。To customize PHP_INI_SYSTEM directives (see php.ini directives), you can't use the .htaccess approach. App Service 會使用 PHP_INI_SCAN_DIR 應用程式設定來提供個別的機制。App Service provides a separate mechanism using the PHP_INI_SCAN_DIR app setting.

首先,在 Cloud Shell 中執行下列命令,以新增名為 PHP_INI_SCAN_DIR 的應用程式設定:First, run the following command in the Cloud Shell to add an app setting called PHP_INI_SCAN_DIR:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PHP_INI_SCAN_DIR="d:\home\site\ini"

流覽至 Kudu 主控台 (https://<app-name>.scm.azurewebsites.net/DebugConsole) ,然後流覽至 d:\home\siteNavigate to the Kudu console (https://<app-name>.scm.azurewebsites.net/DebugConsole) and navigate to d:\home\site.

d:\home\site 中建立名為 ini 的目錄,然後使用要自訂的指示詞在 d:\home\site\ini 目錄中建立 .ini 檔案 (例如 settings.ini)Create a directory in d:\home\site called ini, then create an .ini file in the d:\home\site\ini directory (for example, settings.ini) with the directives you want to customize. 使用要在 php .ini 檔案中使用的相同語法。Use the same syntax you would use in a php.ini file.

例如,若要變更 expose_php 的值,請執行下列命令:For example, to change the value of expose_php run the following commands:

cd /home/site
mkdir ini
echo "expose_php = Off" >> ini/setting.ini

如果要讓變更生效,請重新啟動應用程式。For the changes to take effect, restart the app.

若要自訂 PHP_INI_SYSTEM 指示詞 (請參閱 php.ini 指示詞),您不能使用 .htaccess 方法。To customize PHP_INI_SYSTEM directives (see php.ini directives), you can't use the .htaccess approach. App Service 會使用 PHP_INI_SCAN_DIR 應用程式設定來提供個別的機制。App Service provides a separate mechanism using the PHP_INI_SCAN_DIR app setting.

首先,在 Cloud Shell 中執行下列命令,以新增名為 PHP_INI_SCAN_DIR 的應用程式設定:First, run the following command in the Cloud Shell to add an app setting called PHP_INI_SCAN_DIR:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PHP_INI_SCAN_DIR="/usr/local/etc/php/conf.d:/home/site/ini"

/usr/local/etc/php/conf.dphp .ini 所在的預設目錄。/usr/local/etc/php/conf.d is the default directory where php.ini exists. /home/site/ini 是您要在其中新增自訂 .ini 檔案的自訂目錄。/home/site/ini is the custom directory in which you'll add a custom .ini file. 您可以使用 : 來分隔這些值。You separate the values with a :.

使用 Linux 容器 (https://<app-name>.scm.azurewebsites.net/webssh/host) 瀏覽至 web SSH 工作階段。Navigate to the web SSH session with your Linux container (https://<app-name>.scm.azurewebsites.net/webssh/host).

/home/site 中建立名為 ini 的目錄,然後使用要自訂的指示詞在 /home/site/ini 目錄中建立 .ini 檔案 (例如 settings.ini)Create a directory in /home/site called ini, then create an .ini file in the /home/site/ini directory (for example, settings.ini) with the directives you want to customize. 使用要在 php .ini 檔案中使用的相同語法。Use the same syntax you would use in a php.ini file.

提示

在 App Service 的內建 Linux 容器中,會使用 /home 作為保存的共用儲存體。In the built-in Linux containers in App Service, /home is used as persisted shared storage.

例如,若要變更 expose_php 的值,請執行下列命令:For example, to change the value of expose_php run the following commands:

cd /home/site
mkdir ini
echo "expose_php = Off" >> ini/setting.ini

如果要讓變更生效,請重新啟動應用程式。For the changes to take effect, restart the app.

啟用 PHP 擴充功能Enable PHP extensions

內建 PHP 安裝包含最常使用的擴充功能。The built-in PHP installations contain the most commonly used extensions. 您可以使用與自訂 php.ini 指示詞相同的方式來啟用其他擴充功能。You can enable additional extensions in the same way that you customize php.ini directives.

注意

查看 PHP 版本和目前 php.ini 設定的最佳方法是在應用程式中呼叫 phpinfo ()The best way to see the PHP version and the current php.ini configuration is to call phpinfo() in your app.

若要啟用其他延伸模組,請依照下列步驟執行:To enable additional extensions, by following these steps:

bin 目錄新增至您應用程式的根目錄,並將擴充檔案放 .dll 在其中 (例如 mongodb.dll) 。Add a bin directory to the root directory of your app and put the .dll extension files in it (for example, mongodb.dll). 請確定擴充功能與 Azure 中的 PHP 版本相容,並且與 VC9 及非執行緒安全 (nts) 相容。Make sure that the extensions are compatible with the PHP version in Azure and are VC9 and non-thread-safe (nts) compatible.

部署您的變更。Deploy your changes.

依照自訂 PHP_INI_SYSTEM 指示詞中的步驟,將擴充功能新增至使用 extensionzend_extension 指示詞的自訂 .ini 檔案。Follow the steps in Customize PHP_INI_SYSTEM directives, add the extensions into the custom .ini file with the extension or zend_extension directives.

extension=d:\home\site\wwwroot\bin\mongodb.dll
zend_extension=d:\home\site\wwwroot\bin\xdebug.dll

如果要讓變更生效,請重新啟動應用程式。For the changes to take effect, restart the app.

內建 PHP 安裝包含最常使用的擴充功能。The built-in PHP installations contain the most commonly used extensions. 您可以使用與自訂 php.ini 指示詞相同的方式來啟用其他擴充功能。You can enable additional extensions in the same way that you customize php.ini directives.

注意

查看 PHP 版本和目前 php.ini 設定的最佳方法是在應用程式中呼叫 phpinfo ()The best way to see the PHP version and the current php.ini configuration is to call phpinfo() in your app.

若要啟用其他延伸模組,請依照下列步驟執行:To enable additional extensions, by following these steps:

bin 目錄新增至應用程式的根目錄,並在其中放入 .so 擴充功能檔案 (例如 mongodb.so)。Add a bin directory to the root directory of your app and put the .so extension files in it (for example, mongodb.so). 請確定擴充功能與 Azure 中的 PHP 版本相容,並且與 VC9 及非執行緒安全 (nts) 相容。Make sure that the extensions are compatible with the PHP version in Azure and are VC9 and non-thread-safe (nts) compatible.

部署您的變更。Deploy your changes.

依照自訂 PHP_INI_SYSTEM 指示詞中的步驟,將擴充功能新增至使用 extensionzend_extension 指示詞的自訂 .ini 檔案。Follow the steps in Customize PHP_INI_SYSTEM directives, add the extensions into the custom .ini file with the extension or zend_extension directives.

extension=/home/site/wwwroot/bin/mongodb.so
zend_extension=/home/site/wwwroot/bin/xdebug.so

如果要讓變更生效,請重新啟動應用程式。For the changes to take effect, restart the app.

存取診斷記錄Access diagnostic logs

使用標準 error_log ( # B1 公用程式,讓您的診斷記錄顯示在 Azure App Service 中。Use the standard error_log() utility to make your diagnostic logs to show up in Azure App Service.

若要存取 App Service 中應用程式程式碼內部產生的主控台記錄,請在 Cloud Shell 中執行下列命令,以開啟診斷記錄功能:To access the console logs generated from inside your application code in App Service, turn on diagnostics logging by running the following command in the Cloud Shell:

az webapp log config --resource-group <resource-group-name> --name <app-name> --application-logging true --level Verbose

--level 的可能值為:ErrorWarningInfoVerbosePossible values for --level are: Error, Warning, Info, and Verbose. 後續的每個層級都包含上一個層級。Each subsequent level includes the previous level. 例如:Error 只包含錯誤訊息,而 Verbose 包含所有訊息。For example: Error includes only error messages, and Verbose includes all messages.

開啟診斷記錄後,請執行下列命令來查看記錄資料流:Once diagnostic logging is turned on, run the following command to see the log stream:

az webapp log tail --resource-group <resource-group-name> --name <app-name>

如果您沒有立即看到主控台記錄,請在 30 秒後再查看。If you don't see console logs immediately, check again in 30 seconds.

注意

您也可以在瀏覽器中的 https://<app-name>.scm.azurewebsites.net/api/logs/docker 檢查記錄檔。You can also inspect the log files from the browser at https://<app-name>.scm.azurewebsites.net/api/logs/docker.

若要隨時停止記錄資料流,請輸入 Ctrl+CTo stop log streaming at any time, type Ctrl+C.

您可以存取從容器產生的主控台記錄。You can access the console logs generated from inside the container.

請先執行下列命令來開啟容器記錄:First, turn on container logging by running the following command:

az webapp log config --name <app-name> --resource-group <resource-group-name> --docker-container-logging filesystem

以適合您 Web 應用程式的名稱取代 <app-name><resource-group-name>Replace <app-name> and <resource-group-name> with the names appropriate for your web app.

開啟容器記錄後,請執行下列命令來查看記錄資料流:Once container logging is turned on, run the following command to see the log stream:

az webapp log tail --name <app-name> --resource-group <resource-group-name>

如果您沒有立即看到主控台記錄,請在 30 秒後再查看。If you don't see console logs immediately, check again in 30 seconds.

若要隨時停止記錄資料流,請輸入 Ctrl+CTo stop log streaming at any time, type Ctrl+C .

您也可以在瀏覽器中的 https://<app-name>.scm.azurewebsites.net/api/logs/docker 檢查記錄檔。You can also inspect the log files in a browser at https://<app-name>.scm.azurewebsites.net/api/logs/docker.

疑難排解Troubleshooting

當運作中的 PHP 應用程式在 App Service 中有不同的行為或發生錯誤時,請嘗試下列動作:When a working PHP app behaves differently in App Service or has errors, try the following:

  • 存取記錄資料流Access the log stream.
  • 在生產模式中於本機測試應用程式。Test the app locally in production mode. App Service 在生產模式中執行您的應用程式,因此您必須確定您的專案會在本機的生產模式中如預期般運作。App Service runs your app in production mode, so you need to make sure that your project works as expected in production mode locally. 例如:For example:
    • composer.json 而定,可能會為生產模式安裝不同的套件 (requirerequire-dev)。Depending on your composer.json, different packages may be installed for production mode (require vs. require-dev).
    • 某些 web 架構可以在生產模式中以不同的方式部署靜態檔案。Certain web frameworks may deploy static files differently in production mode.
    • 在生產模式中執行時,某些 web 架構可能會使用自訂啟動指令碼。Certain web frameworks may use custom startup scripts when running in production mode.
  • 在偵錯模式中於 App Service 內執行應用程式。Run your app in App Service in debug mode. 例如,在 Laravel中,您可以透過APP_DEBUG 應用程式設定設為 true,將應用程式設定為在生產環境中輸出偵錯訊息。For example, in Laravel, you can configure your app to output debug messages in production by setting the APP_DEBUG app setting to true.

記錄中的 robots933456robots933456 in logs

您可能會在容器記錄中看到下列訊息:You may see the following message in the container logs:

2019-04-08T14:07:56.641002476Z "-" - - [08/Apr/2019:14:07:56 +0000] "GET /robots933456.txt HTTP/1.1" 404 415 "-" "-"

您可以放心地忽略此訊息。You can safely ignore this message. /robots933456.txt 是一個虛擬 URL 路徑,App Service 會使用該路徑來檢查容器是否可以處理要求。/robots933456.txt is a dummy URL path that App Service uses to check if the container is capable of serving requests. 404 回應只是指出路徑不存在,但其可讓 App Service 知道容器狀況良好,並已準備好回應要求。A 404 response simply indicates that the path doesn't exist, but it lets App Service know that the container is healthy and ready to respond to requests.

後續步驟Next steps