创建 PHP Web 角色和辅助角色Create PHP web and worker roles

概述Overview

本指南说明如何执行以下操作:在 Windows 开发环境中创建 PHP Web 角色或辅助角色,从提供的“内置”版本中选择特定版本的 PHP,更改 PHP 配置,启用扩展,最后部署到 Azure。This guide will show you how to create PHP web or worker roles in a Windows development environment, choose a specific version of PHP from the "built-in" versions available, change the PHP configuration, enable extensions, and finally, deploy to Azure. 它还介绍了如何将 Web 角色或辅助角色配置为使用你提供的 PHP 运行时(带自定义配置和扩展)。It also describes how to configure a web or worker role to use a PHP runtime (with custom configuration and extensions) that you provide.

Azure 提供了三种用于运行应用程序的计算模型:Azure 应用服务、Azure 虚拟机和 Azure 云服务。Azure provides three compute models for running applications: Azure App Service, Azure Virtual Machines, and Azure Cloud Services. 这三种模型都支持 PHP。All three models support PHP. 云服务(包括 Web 角色和辅助角色)提供了平台即服务 (PaaS)Cloud Services, which includes web and worker roles, provides platform as a service (PaaS). 在云服务中,Web 角色提供专门用于托管前端 Web 应用程序的 Internet Information Services (IIS) Web 服务器。Within a cloud service, a web role provides a dedicated Internet Information Services (IIS) web server to host front-end web applications. 辅助角色可运行独立于用户交互或输入的异步任务、运行时间较长的任务或永久性任务。A worker role can run asynchronous, long-running or perpetual tasks independent of user interaction or input.

有关这些选项的详细信息,请参阅 Azure 提供的计算托管选项For more information about these options, see Compute hosting options provided by Azure.

下载 Azure SDK for PHPDownload the Azure SDK for PHP

Azure SDK for PHP 由多个组件构成。The Azure SDK for PHP consists of several components. 本文将使用其中的两个组件:Azure PowerShell 和 Azure 模拟器。This article will use two of them: Azure PowerShell and the Azure emulators. 可以通过 Microsoft Web 平台安装程序安装这两个组件。These two components can be installed via the Microsoft Web Platform Installer. 有关详细信息,请参阅 如何安装和配置 Azure PowerShellFor more information, see How to install and configure Azure PowerShell.

创建云服务项目Create a Cloud Services project

创建 PHP Web 角色或辅助角色的第一步是创建 Azure 服务项目。The first step in creating a PHP web or worker role is to create an Azure Service project. Azure 服务项目用作 Web 角色和辅助角色的逻辑容器,包含项目的服务定义 (.csdef)服务配置 (.cscfg) 文件。an Azure Service project serves as a logical container for web and worker roles, and it contains the project's service definition (.csdef) and service configuration (.cscfg) files.

若要创建新的 Azure 服务项目,请以管理员身份运行 Azure PowerShell 并执行以下命令:To create a new Azure Service project, run Azure PowerShell as an administrator, and execute the following command:

PS C:\>New-AzureServiceProject myProject

此命令将创建可将 Web 角色和辅助角色添加到的新目录 (myProject)。This command will create a new directory (myProject) to which you can add web and worker roles.

添加 PHP Web 角色或辅助角色Add PHP web or worker roles

要将 PHP Web 角色添加到项目,请从项目的根目录中运行以下命令:To add a PHP web role to a project, run the following command from within the project's root directory:

PS C:\myProject> Add-AzurePHPWebRole roleName

对于辅助角色,请使用此命令:For a worker role, use this command:

PS C:\myProject> Add-AzurePHPWorkerRole roleName

备注

roleName 参数是可选的。The roleName parameter is optional. 如果省略该参数,则自动生成角色名称。If it is omitted, the role name will be automatically generated. 创建的第一个 Web 角色将为 WebRole1,第二个 Web 角色为 WebRole2,依此类推。The first web role created will be WebRole1, the second will be WebRole2, and so on. 创建的第一个辅助角色将为 WorkerRole1,第二个辅助角色为 WorkerRole2,依此类推。The first worker role created will be WorkerRole1, the second will be WorkerRole2, and so on.

使用自己的 PHP 运行时Use your own PHP runtime

在某些情况下,可能需要提供你自己的 PHP 运行时,而不是如上所述那样选择并配置内置 PHP 运行时。In some cases, instead of selecting a built-in PHP runtime and configuring it as described above, you may want to provide your own PHP runtime. 例如,可以在 Web 角色或辅助角色中使用你在开发环境中使用的 PHP 运行时,For example, you can use the same PHP runtime in a web or worker role that you use in your development environment. 以便更轻松地确保应用程序不会更改生产环境中的行为。This makes it easier to ensure that the application will not change behavior in your production environment.

将 Web 角色配置为使用你自己的 PHP 运行时Configure a web role to use your own PHP runtime

要将 Web 角色配置为使用提供的 PHP 运行时,请执行下列步骤:To configure a web role to use a PHP runtime that you provide, follow these steps:

  1. 创建一个 Azure 服务项目并添加 PHP Web 角色,如本主题前面所述。Create an Azure Service project and add a PHP web role as described previously in this topic.

  2. 在位于 Web 角色的根目录中的 bin 文件夹中创建一个 php 文件夹,然后将 PHP 运行时(所有二进制文件、配置文件、子文件夹等)添加到该 php 文件夹中。Create a php folder in the bin folder that is in your web role's root directory, and then add your PHP runtime (all binaries, configuration files, subfolders, etc.) to the php folder.

  3. (可选)如果 PHP 运行时使用 Microsoft Drivers for PHP for SQL Server,则需要将 Web 角色配置为在预配它时安装 SQL Server Native Client 2012(OPTIONAL) If your PHP runtime uses the Microsoft Drivers for PHP for SQL Server, you will need to configure your web role to install SQL Server Native Client 2012 when it is provisioned. 为此,将 sqlncli.msi x64 安装程序添加到 Web 角色的根目录中的 bin 文件夹。To do this, add the sqlncli.msi x64 installer to the bin folder in your web role's root directory. 下一步中所述的启动脚本在设置角色时以静默方式运行安装程序。The startup script described in the next step will silently run the installer when the role is provisioned. 如果 PHP 运行时不使用 Microsoft Drivers for PHP for SQL Server,则可从下一步所示的脚本中删除以下行:If your PHP runtime does not use the Microsoft Drivers for PHP for SQL Server, you can remove the following line from the script shown in the next step:

    msiexec /i sqlncli.msi /qn IACCEPTSQLNCLILICENSETERMS=YES
    
  4. 定义一个启动任务,用以将 Internet Information Services (IIS) 配置为使用 PHP 运行时来处理对 .php 页面的请求。Define a startup task that configures Internet Information Services (IIS) to use your PHP runtime to handle requests for .php pages. 为此,请在文本编辑器中打开 setup_web.cmd 文件(位于 Web 角色的根目录的 bin 文件夹中),并将其内容替换为以下脚本:To do this, open the setup_web.cmd file (in the bin file of your web role's root directory) in a text editor and replace its contents with the following script:

    @ECHO ON
    cd "%~dp0"
    
    if "%EMULATED%"=="true" exit /b 0
    
    msiexec /i sqlncli.msi /qn IACCEPTSQLNCLILICENSETERMS=YES
    
    SET PHP_FULL_PATH=%~dp0php\php-cgi.exe
    SET NEW_PATH=%PATH%;%RoleRoot%\base\x86
    
    %WINDIR%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='%PHP_FULL_PATH%',maxInstances='12',idleTimeout='60000',activityTimeout='3600',requestTimeout='60000',instanceMaxRequests='10000',protocol='NamedPipe',flushNamedPipe='False']" /commit:apphost
    %WINDIR%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='%PHP_FULL_PATH%'].environmentVariables.[name='PATH',value='%NEW_PATH%']" /commit:apphost
    %WINDIR%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='%PHP_FULL_PATH%'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']" /commit:apphost
    %WINDIR%\system32\inetsrv\appcmd.exe set config -section:system.webServer/handlers /+"[name='PHP',path='*.php',verb='GET,HEAD,POST',modules='FastCgiModule',scriptProcessor='%PHP_FULL_PATH%',resourceType='Either',requireAccess='Script']" /commit:apphost
    %WINDIR%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /"[fullPath='%PHP_FULL_PATH%'].queueLength:50000"
    
  5. 将应用程序文件添加到 Web 角色的根目录。Add your application files to your web role's root directory. 这将是 Web 服务器的根目录。This will be the web server's root directory.

  6. 按照以下发布应用程序部分中所述发布应用程序。Publish your application as described in the Publish your application section below.

备注

在按照上述使用你自己的 PHP 运行时的步骤进行操作后,可以删除 download.ps1 脚本(位于 Web 角色的根目录的 bin 文件夹中)。The download.ps1 script (in the bin folder of the web role's root directory) can be deleted after you follow the steps described above for using your own PHP runtime.

将辅助角色配置为使用自己的 PHP 运行时Configure a worker role to use your own PHP runtime

要将辅助角色配置为使用提供的 PHP 运行时,请执行下列步骤:To configure a worker role to use a PHP runtime that you provide, follow these steps:

  1. 创建一个 Azure 服务项目并添加 PHP 辅助角色,如本主题前面所述。Create an Azure Service project and add a PHP worker role as described previously in this topic.

  2. 在辅助角色的根目录中创建一个 php 文件夹,然后将 PHP 运行时(所有二进制文件、配置文件、子文件夹等)添加到该 php 文件夹中。Create a php folder in the worker role's root directory, and then add your PHP runtime (all binaries, configuration files, subfolders, etc.) to the php folder.

  3. (可选)如果 PHP 运行时使用 Microsoft Drivers for PHP for SQL Server,则需要将辅助角色配置为在预配它时安装 SQL Server Native Client 2012(OPTIONAL) If your PHP runtime uses Microsoft Drivers for PHP for SQL Server, you will need to configure your worker role to install SQL Server Native Client 2012 when it is provisioned. 为此,将 sqlncli.msi x64 安装程序添加到辅助角色的根目录。To do this, add the sqlncli.msi x64 installer to the worker role's root directory. 下一步中所述的启动脚本在设置角色时以静默方式运行安装程序。The startup script described in the next step will silently run the installer when the role is provisioned. 如果 PHP 运行时不使用 Microsoft Drivers for PHP for SQL Server,则可从下一步所示的脚本中删除以下行:If your PHP runtime does not use the Microsoft Drivers for PHP for SQL Server, you can remove the following line from the script shown in the next step:

    msiexec /i sqlncli.msi /qn IACCEPTSQLNCLILICENSETERMS=YES
    
  4. 定义在设置角色时将 php.exe 可执行文件添加到辅助角色的 PATH 环境变量中的启动任务。Define a startup task that adds your php.exe executable to the worker role's PATH environment variable when the role is provisioned. 为此,请在文本编辑器中打开 setup_worker.cmd 文件(位于辅助角色的根目录中),并将其内容替换为以下脚本:To do this, open the setup_worker.cmd file (in the worker role's root directory) in a text editor and replace its contents with the following script:

    @echo on
    
    cd "%~dp0"
    
    echo Granting permissions for Network Service to the web root directory...
    icacls ..\ /grant "Network Service":(OI)(CI)W
    if %ERRORLEVEL% neq 0 goto error
    echo OK
    
    if "%EMULATED%"=="true" exit /b 0
    
    msiexec /i sqlncli.msi /qn IACCEPTSQLNCLILICENSETERMS=YES
    
    setx Path "%PATH%;%~dp0php" /M
    
    if %ERRORLEVEL% neq 0 goto error
    
    echo SUCCESS
    exit /b 0
    
    :error
    
    echo FAILED
    exit /b -1
    
  5. 将应用程序文件添加到辅助角色的根目录。Add your application files to your worker role's root directory.

  6. 按照以下发布应用程序部分中所述发布应用程序。Publish your application as described in the Publish your application section below.

在计算和存储模拟器中运行应用程序Run your application in the compute and storage emulators

Azure 模拟器提供了一个本地环境,可在将 Azure 应用程序部署到云之前在该本地环境中测试此应用程序。The Azure emulators provide a local environment in which you can test your Azure application before you deploy it to the cloud. 模拟器与 Azure 环境之间存在一些差异。There are some differences between the emulators and the Azure environment. 若要更好地了解该模拟器,请参阅使用 Azure 存储模拟器进行开发和测试To understand this better, see Use the Azure Storage Emulator for development and testing.

请注意,必须本地安装 PHP 才能使用计算模拟器。Note that you must have PHP installed locally to use the compute emulator. 计算模拟器将使用本地 PHP 安装来运行应用程序。The compute emulator will use your local PHP installation to run your application.

要在模拟器中运行项目,请从项目的根目录中执行以下命令:To run your project in the emulators, execute the following command from your project's root directory:

PS C:\MyProject> Start-AzureEmulator

你会看到类似于下面的输出:You will see output similar to this:

Creating local package...
Starting Emulator...
Role is running at http://127.0.0.1:81
Started

通过打开 Web 浏览器并浏览到输出中所示的本地地址(上面的示例输出中的 http://127.0.0.1:81),可以查看正在模拟器上运行的应用程序。You can see your application running in the emulator by opening a web browser and browsing to the local address shown in the output (http://127.0.0.1:81 in the example output above).

若要停止模拟器,请执行此命令:To stop the emulators, execute this command:

PS C:\MyProject> Stop-AzureEmulator

发布应用程序Publish your application

若要发布应用程序,需要先使用 Import-AzurePublishSettingsFile cmdlet 导入发布设置。To publish your application, you need to first import your publish settings by using the Import-AzurePublishSettingsFile cmdlet. 然后使用 Publish-AzureServiceProject cmdlet 发布用户的应用程序。Then you can publish your application by using the Publish-AzureServiceProject cmdlet. 有关登录的信息,请参阅如何安装和配置 Azure PowerShellFor information about signing in, see How to install and configure Azure PowerShell.

后续步骤Next steps

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