建立 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 角色提供專用的 Internet Information Services (IIS) Web 伺服器,用來代管前端 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 Platform Installer 來安裝。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

此命令會建立新目錄 (myProject),讓您可將 Web 和背景工作角色新增至該處。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、第二個將是 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 執行階段使用適用於 PHP for SQL Server 的 Microsoft 驅動程式,您就必須將 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 執行階段並未使用適用於 PHP for SQL Server 的 Microsoft 驅動程式,您可以從下一個步驟所顯示的指令碼中移除以下一行: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 執行階段使用適用於 PHP for SQL Server 的 Microsoft 驅動程式,您就必須將背景工作角色設定為在佈建時安裝 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 執行階段並未使用適用於 PHP for SQL Server 的 Microsoft 驅動程式,您可以從下一個步驟所顯示的指令碼中移除以下一行: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

您可以開啟網頁瀏覽器,並瀏覽至輸出中顯示的本機位址 (在前述範例輸出中為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.