在 Azure 应用服务中配置 PHPConfigure PHP in Azure App Service

简介Introduction

本指南演示如何执行以下操作:在 Azure 应用服务中配置 Web 应用、移动后端和 API 应用的内置 PHP 运行时,提供自定义 PHP 运行时,并启用扩展。This guide shows you how to configure the built-in PHP runtime for web apps, mobile back ends, and API apps in Azure App Service, provide a custom PHP runtime, and enable extensions. 若要使用应用服务,请注册 [1 元人民币的试用订阅]。To use App Service, sign up for the [1RMB Trial]. 要充分利用本指南,应先在应用服务中创建一个 PHP 应用。To get the most from this guide, you should first create a PHP app in App Service.

如何:更改内置 PHP 版本How to: Change the built-in PHP version

默认情况下,将安装 PHP 5.6 并且在创建应用服务应用时立即可用。By default, PHP 5.6 is installed and immediately available for use when you create an App Service app. 查看可用发行版、其默认配置以及已启用的扩展的最佳方式是部署一个调用 phpinfo() 函数的脚本。The best way to see the available release revision, its default configuration, and the enabled extensions is to deploy a script that calls the phpinfo() function.

PHP 7.0 和 PHP 7.2 也可用,但它们在默认情况下不启用。PHP 7.0 and PHP 7.2 versions are also available, but not enabled by default. 若要更新 PHP 版本,请使用下列方法之一:To update the PHP version, follow one of these methods:

Azure 门户Azure portal

  1. Azure 门户中浏览到你的应用,并滚动到“配置” 页面。Browse to your app in the Azure portal and scroll to the Configuration page.

  2. 从“配置” 中,选择“常规设置” 并选择新的 PHP 版本。From Configuration, select General Settings and choose the new PHP version.

  3. 单击“常规设置”边栏选项卡顶部的“保存”按钮。 Click the Save button at the top of the General settings blade.

Azure PowerShell (Windows)Azure PowerShell (Windows)

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

  1. 打开 Azure PowerShell 并登录到帐户:Open Azure PowerShell, and login to your account:

     PS C:\> Connect-AzAccount -EnvironmentName AzureChinaCloud
    
  2. 设置应用的 PHP 版本。Set the PHP version for the app.

     PS C:\> Set-AzureWebsite -PhpVersion {5.6 | 7.0 | 7.2} -Name {app-name}
    
  3. 现已设置 PHP 版本。The PHP version is now set. 可以确认这些设置:You can confirm these settings:

     PS C:\> Get-AzureWebsite -Name {app-name} | findstr PhpVersion
    

Azure CLIAzure CLI

必须在计算机上安装 Azure CLI,才能使用 Azure 命令行接口。To use the Azure Command-Line Interface, you must Install the Azure CLI on your computer.

  1. 打开终端,并登录到帐户。Open Terminal, and login to your account.

    az cloud set -n AzureChinaCloud az loginaz cloud set -n AzureChinaCloud az login

  2. 检查受支持运行时的列表。Check to see the list of supported runtimes.

     az webapp list-runtimes | grep php
    
  3. 设置应用的 PHP 版本。Set the PHP version for the app.

     az webapp config set --php-version {5.6 | 7.0 | 7.1 | 7.2} --name {app-name} --resource-group {resource-group-name}
    
  4. 现已设置 PHP 版本。The PHP version is now set. 可以确认这些设置:You can confirm these settings:

     az webapp show --name {app-name} --resource-group {resource-group-name}
    

如何:更改内置 PHP 配置How to: Change the built-in PHP configurations

对于任何内置 PHP 运行时,都可以通过执行以下步骤更改任何配置选项。For any built-in PHP runtime, you can change any of the configuration options by following these steps. (有关 php.ini 指令的信息,请参阅 php.ini 指令的列表。)(For information about php.ini directives, see List of php.ini directives.)

更改 PHP_INI_USER、PHP_INI_PERDIR、PHP_INI_ALL 配置设置Changing PHP_INI_USER, PHP_INI_PERDIR, PHP_INI_ALL configuration settings

  1. .user.ini 文件添加到根目录。Add a .user.ini file to your root directory.

  2. 使用会在 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 设置设为 10 分钟,则 .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
    
     ; OPTIONAL: Turn this on to write errors to d:\home\LogFiles\php_errors.log
     ; log_errors=On
    
  3. 部署应用。Deploy your app.

  4. 重新启动应用。Restart the app. (需要进行重新启动,因为 PHP 读取 .user.ini 文件的频率受 user_ini.cache_ttl 设置的约束,该设置是一个系统级别设置且默认值为 300 秒(5 分钟)。(Restarting is necessary because the frequency with which PHP reads .user.ini files is governed by the user_ini.cache_ttl setting, which is a system level setting and is 300 seconds (5 minutes) by default. 重启应用会强制 PHP 读取 .user.ini 文件中的新设置。)Restarting the app forces PHP to read the new settings in the .user.ini file.)

作为使用 .user.ini 文件的替代方法,可以在脚本中使用 ini_set() 函数来设置不是系统级别指令的配置选项。As an alternative to using a .user.ini file, you can use the ini_set() function in scripts to set configuration options that are not system-level directives.

更改 PHP_INI_SYSTEM 配置设置Changing PHP_INI_SYSTEM configuration settings

  1. 向应用添加一个键为 PHP_INI_SCAN_DIR 且值为 d:\home\site\ini 的应用设置Add an App Setting to your app with the key PHP_INI_SCAN_DIR and value d:\home\site\ini

  2. 使用 Kudu 控制器 (http://<site-name>.scm.azurewebsite.net) 在 d:\home\site\ini 目录中创建一个 settings.ini 文件。Create an settings.ini file using Kudu Console (http://<site-name>.scm.azurewebsite.net) in the d:\home\site\ini directory.

  3. 使用会在 php.ini 文件中使用的语法,将配置设置添加到 settings.ini 文件。Add configuration settings to the settings.ini file using the same syntax you would use in a php.ini file. 例如,如果希望将 curl.cainfo 设置指向 *.crt 文件并将“wincache.maxfilesize”设置为 512K,则 settings.ini 文件应包含以下文本:For example, if you wanted to point the curl.cainfo setting to a *.crt file and set 'wincache.maxfilesize' setting to 512K, your settings.ini file would contain this text:

     ; Example Settings
     curl.cainfo="%ProgramFiles(x86)%\Git\bin\curl-ca-bundle.crt"
     wincache.maxfilesize=512
    
  4. 重启应用以重载更改。To reload the changes, restart your app.

如何:在默认 PHP 运行时中启用扩展How to: Enable extensions in the default PHP runtime

如上一部分所述,查看默认 PHP 版本、其默认配置以及已启用的扩展的最佳方式是部署一个调用 phpinfo() 的脚本。As noted in the previous section, the best way to see the default PHP version, its default configuration, and the enabled extensions is to deploy a script that calls phpinfo(). 若要启用其他扩展,请执行下列步骤:To enable additional extensions, by following these steps:

通过 ini 设置进行配置Configure via ini settings

  1. ext 目录添加到 d:\home\site 目录。Add a ext directory to the d:\home\site directory.

  2. .dll 扩展文件置于 ext 目录中(例如 php_xdebug.dll)。Put .dll extension files in the ext directory (for example, php_xdebug.dll). 确保扩展与默认版本的 PHP兼容,并且是 VC9 版本且与非线程安全 (nts) 兼容。Make sure that the extensions are compatible with default version of PHP and are VC9 and non-thread-safe (nts) compatible.

  3. 向应用添加一个键为 PHP_INI_SCAN_DIR 且值为 d:\home\site\ini 的应用设置Add an App Setting to your app with the key PHP_INI_SCAN_DIR and value d:\home\site\ini

  4. d:\home\site\ini 中创建名为 extensions.iniini 文件。Create an ini file in d:\home\site\ini called extensions.ini.

  5. 使用会在 php.ini 文件中使用的语法,将配置设置添加到 extensions.ini 文件。Add configuration settings to the extensions.ini file using the same syntax you would use in a php.ini file. 例如,如果你想要启用 MongoDB 和 XDebug 扩展,则 extensions.ini 文件包含此文本:For example, if you wanted to enable the MongoDB and XDebug extensions, your extensions.ini file would contain this text:

     ; Enable Extensions
     extension=d:\home\site\ext\php_mongo.dll
     zend_extension=d:\home\site\ext\php_xdebug.dll
    
  6. 重启应用以加载更改。Restart your app to load the changes.

通过应用设置进行配置Configure via App Setting

  1. bin 目录添加到根目录。Add a bin directory to the root directory.
  2. .dll 扩展文件置于 bin 目录中(例如 php_xdebug.dll)。Put .dll extension files in the bin directory (for example, php_xdebug.dll). 确保扩展与默认版本的 PHP兼容,并且是 VC9 版本且与非线程安全 (nts) 兼容。Make sure that the extensions are compatible with default version of PHP and are VC9 and non-thread-safe (nts) compatible.
  3. 部署应用。Deploy your app.
  4. 在 Azure 门户中浏览到你的应用,并单击位于“设置”部分下方的“配置”。 Browse to your app in the Azure portal and click on the Configuration located below Settings section.
  5. 从“配置” 边栏选项卡上,选择“应用程序设置”。 From the Configuration blade, select Application Settings.
  6. 在“应用程序设置” 部分中,单击“+ 新建应用程序设置” 并创建一个 PHP_EXTENSIONS 密钥。In the Application settings section, click on + New application setting and create a PHP_EXTENSIONS key. 此键的值会是相对于网站根目录的一个路径:bin\your-ext-fileThe value for this key would be a path relative to website root: bin\your-ext-file.
  7. 单击底部的“更新” 按钮,然后单击“应用程序设置”选项卡上方的“保存”。 Click the Update button at the bottom then click Save above the Application settings tab.

通过使用 PHP_ZENDEXTENSIONS 键,还可以支持 Zend 扩展。Zend extensions are also supported by using a PHP_ZENDEXTENSIONS key. 若要启用多个扩展,请包括应用设置值的 .dll 文件的逗号分隔列表。To enable multiple extensions, include a comma-separated list of .dll files for the app setting value.

如何:使用自定义 PHP 运行时How to: Use a custom PHP runtime

应用服务可以使用提供的 PHP 运行时(而非默认 PHP 运行时)来执行 PHP 脚本。Instead of the default PHP runtime, App Service can use a PHP runtime that you provide to execute PHP scripts. 提供的运行时可由 php.ini 文件(也是你提供的)配置。The runtime that you provide can be configured by a php.ini file that you also provide. 要在应用服务中使用自定义 PHP 运行时,请执行下列步骤。To use a custom PHP runtime with App Service, following these steps.

  1. 获取非线程安全、VC9 或 VC11 兼容版本的 PHP for Windows。Obtain a non-thread-safe, VC9 or VC11 compatible version of PHP for Windows. 可在此处找到 PHP for Windows 最新版本:https://windows.php.net/download/Recent releases of PHP for Windows can be found here: https://windows.php.net/download/. 可在此处的存档中找到旧版本:https://windows.php.net/downloads/releases/archives/Older releases can be found in the archive here: https://windows.php.net/downloads/releases/archives/.
  2. 修改运行时的 php.ini 文件。Modify the php.ini file for your runtime. 应用服务将忽略作为任何仅在系统级别使用的指令的配置设置。Any configuration settings that are system-level-only directives are ignored by App Service. (有关仅在系统级别使用的指令的信息,请参阅 php.ini 指令的列表)。(For information about system-level-only directives, see List of php.ini directives).
  3. (可选)将扩展添加到 PHP 运行时并在 php.ini 文件中启用这些扩展。Optionally, add extensions to your PHP runtime and enable them in the php.ini file.
  4. bin 目录添加到根目录,并将包含 PHP 运行时的目录置于该目录中(例如 bin\php)。Add a bin directory to your root directory, and put the directory that contains your PHP runtime in it (for example, bin\php).
  5. 部署应用。Deploy your app.
  6. 在 Azure 门户中浏览到你的应用,并单击“配置”边栏选项卡。 Browse to your app in the Azure portal and click on the Configuration blade.
  7. 从“配置” 边栏选项卡上,选择“路径映射” 。From the Configuration blade, select Path mappings.
  8. 单击“+ 新建处理程序” ,将 *.php 添加到“扩展”字段,并在“脚本处理器”中添加 php-cgi.exe 可执行文件的路径。 Click + New Handler and add *.php to the Extension field and add the path to the php-cgi.exe executable in Script processor. 如果将 PHP 运行时放在应用程序根目录中的 bin 目录下,路径将为 D:\home\site\wwwroot\bin\php\php-cgi.exeIf you put your PHP runtime in the bin directory in the root of your application, the path is D:\home\site\wwwroot\bin\php\php-cgi.exe.
  9. 在底部,单击“更新” 以完成添加处理程序映射。At the bottom, click Update to finish adding the handler mapping.
  10. 单击“保存” 以保存更改。Click Save to save changes.

如何:在 Azure 中启用编辑器自动化How to: Enable Composer automation in Azure

默认情况下,应用服务与 composer.json(如果 PHP 项目中有)不相关。By default, App Service doesn't do anything with composer.json, if you have one in your PHP project. 如果使用 Git 部署,则可以通过在 git push 期间启用编辑器扩展来启用 composer.json 处理。If you use Git deployment, you can enable composer.json processing during git push by enabling the Composer extension.

  1. Azure 门户的 PHP 应用的边栏选项卡中,单击“工具” > “扩展” 。In your PHP app's blade in the Azure portal, click Tools > Extensions.

    Azure 门户设置边栏选项卡,用于在 Azure 中启用编辑器自动化

  2. 单击“添加”,并单击“编辑器”。 Click Add, then click Composer.

    添加编辑器扩展,以在 Azure 中启用编辑器自动化

  3. 单击“确定” 接受法律条款。Click OK to accept legal terms. 再次单击“确定” 以添加该扩展。Click OK again to add the extension.

    “已安装扩展”边栏选项卡将显示编辑器扩展 。The Installed extensions blade shows the Composer extension. 接受法律条款以在 Azure 中启用编辑器自动化Accept legal terms to enable Composer automation in Azure

  4. 现在,在本地计算机上的终端窗口中,对应用执行 git addgit commitgit pushNow, in a terminal window on your local machine, perform git add, git commit, and git push to your app. 请注意,编辑器正在安装在 composer.json 中定义的依赖项。Notice that Composer is installing dependencies defined in composer.json.

    在 Azure 中使用编辑器自动化的 Git 部署

后续步骤Next steps

有关详细信息,请参阅 PHP 开发人员中心For more information, see the PHP Developer Center.