Post on 10-Jun-2020
transcript
Page | 1
Hands-On Lab
Windows Azure and PHP
Lab version: 2.0.0
Last updated: 11/16/2010
Page | 2
CONTENTS
OVERVIEW ................................................................................................................................................... 3
EXERCISE 1: USING FASTCGI TO HOST PHP APPLICATIONS ON IIS 7.0 .......................................... 5 Task 1 – Enabling FastCGI Support in IIS 7.0 ......................................................................................... 5
Task 2 – Installing and Configuring PHP ................................................................................................ 7
Task 3 – Creating a Simple PHP Application.......................................................................................... 9
EXERCISE 2: USING WINDOWS AZURE TO HOST PHP APPLICATIONS ........................................... 16 Task 1 – Updating the Web.roleconfig File ......................................................................................... 16
Task 2 – Configuring Handlers for FastCGI .......................................................................................... 17
Task 3 – Creating a Service Definition File .......................................................................................... 18
Task 4 – Copying your PHP Interpreter into the PHP Project ............................................................. 18
Task 5 – Preparing your Service for Deployment ................................................................................ 19
Task 6 – Deploying your Service to the Azure Development Fabric ................................................... 21
Verification .......................................................................................................................................... 23
EXERCISE 3: CREATING PHP APPLICATIONS WITH VISUAL STUDIO .............................................. 25 Task 1 – Creating the Visual Studio Project ........................................................................................ 25
Task 2 – Updating the Web.roleconfig File ......................................................................................... 30
Task 3 – Configuring Handlers for FastCGI .......................................................................................... 30
Task 4 – Copying your PHP Interpreter into the PHP Project ............................................................. 31
Verification .......................................................................................................................................... 32
SUMMARY .................................................................................................................................................. 34
Page | 3
Overview
FastCGI provides a high-performance alternative to the Common Gateway Interface (CGI), a standard
way of interfacing external applications with Web servers that has been supported as part of the IIS
feature-set since the very first release.
CGI programs are executables launched by the web server for each request in order to process the
request and generate dynamic responses that are sent back to the client. As many of these frameworks
do not support multi-threaded execution, CGI enables them to execute reliably on IIS through executing
one request per process. Unfortunately, this can provide poor performance due to the high cost of
starting and shutting down a process for each request.
FastCGI overcomes this performance penalty by re-using CGI processes to service subsequent requests,
while continuing to ensure single request concurrency. For further information on how FastCGI works,
visit the FastCGI Web Site and review the FastCGI Specification. The IIS FastCGI component enables popular application frameworks like PHP to be hosted on the IIS web
server in a high-performance and reliable way. As Windows Azure uses IIS 7.0 at the heart of the web
role, it also supports the FastCGI component.
In this lab, you will examine the basic process of installing and configuring PHP on IIS 7.0 with FastCGI
and discover the symmetry for developers wishing to host PHP applications in the cloud with Windows
Azure.
Objectives
In this hands-on lab, you will learn how to:
Enable FastCGI support in IIS 7.0
Use FastCGI to host PHP applications on IIS 7.0
Use Windows Azure to host PHP applications
Prerequisites
The following is required to complete this hands-on lab:
PHP (non thread-safe version recommended)
Note: This lab was built using PHP version 5.3.2. Other versions were not tested.
IIS 7.0 (with ASP.NET, WCF HTTP Activation)
Page | 4
Microsoft .NET Framework 4.0
Microsoft Visual Studio 2010 (required for Exercise 3)
Windows Azure Software Development Kit
Windows Azure Tools for Microsoft Visual Studio 1.2 (June 2010) (required for Exercise 3)
A user account with administrative privileges is required to complete this lab.
Setup
For convenience, much of the code used in this hands-on lab is available as Visual Studio code snippets.
To check the prerequisites of the lab and install the code snippets:
1. Open a Windows Explorer window and browse to the lab’s Source\Setup folder.
2. Double-click the Dependencies.dep file in this folder to launch the Dependency Checker tool
and install any missing prerequisites and the Visual Studio code snippets.
3. If the User Account Control dialog is shown, confirm the action to proceed.
Note: This process may require elevation. The .dep extension is associated with the Dependency
Checker tool during its installation. For additional information about the setup procedure and how to
install the Dependency Checker tool, refer to the Setup.docx document in the Assets folder of the
training kit.
Using the Code Snippets
Throughout the lab document, you will be instructed to insert code blocks. For your convenience, most
of that code is provided as Visual Studio Code Snippets, which you can use from within Visual Studio
2010 to avoid having to add it manually.
If you are not familiar with the Visual Studio Code Snippets, and want to learn how to use them, you can
refer to the Setup.docx document in the Assets folder of the training kit, which contains a section
describing how to use them.
Exercises
This hands-on lab includes the following exercises:
1. Using FastCGI to Host PHP Applications on IIS 7.0
2. Using Windows Azure to Host PHP Applications
Page | 5
3. Creating PHP Applications with Visual Studio
Estimated time to complete this lab: 45 minutes.
Note: When you first start Visual Studio, you must select one of the predefined settings collections.
Every predefined collection is designed to match a particular development style and determines
window layouts, editor behavior, IntelliSense code snippets, and dialog box options. The procedures in
this lab describe the actions necessary to accomplish a given task in Visual Studio when using the
General Development Settings collection. If you choose a different settings collection for your
development environment, there may be differences in these procedures that you need to take into
account.
Exercise 1: Using FastCGI to Host PHP
Applications on IIS 7.0
Note: Exercise 1 - Using FastCGI to host PHP applications on IIS 7.0 - is an optional exercise. If you
have configured PHP with FastCGI before, then you may already understand these concepts. It is
provided as part of this lab for your convenience.
In this exercise, you enable FastCGI support in IIS 7.0 and use FastCGI to host a simple PHP application
on IIS 7.0. The PHP application displays information about the current state of PHP, which includes
components such as PHP compilation options and extensions, the PHP version, the PHP environment, OS
version information, paths, etc…
Task 1 – Enabling FastCGI Support in IIS 7.0
In this task, you enable the FastCGI module in IIS 7.0.
1. Add the CGI role Service.
If your Operating System is Windows Server 2008, add the CGI role service by using Server
Manager -> Roles ->Web Server-> Add Role Services.
Page | 6
Figure 1
Add Role Services dialog showing the available services for the Web Server (IIS) role
If your operating system is Windows Vista or Windows 7, add the CGI feature by using Control
Panel -> Programs -> Programs and Features -> Turn Windows features on or off. In the
Windows Features dialog, choose Internet Information Services -> World Wide Web Services ->
Application Development Features -> CGI.
Page | 7
Figure 2
Windows Features dialog window showing the available Windows Features
Note: If you are running Windows Vista SP1 or Windows Server 2008, please make sure you
have the following hotfixes installed:
1. Hotfix to resolve an issue in which a PHP application dependent on the REQUEST_URI server
variable fails in Internet Information Services (IIS) 7.0
2. Hotfix to fix an issue with FastCGI module not working if you are using the HWC functionality
of IIS 7.0 and the process that hosts the HWC is part of a Windows job object
Task 2 – Installing and Configuring PHP
In this task, you install PHP and configure it using FastCGI.
It is recommended to use a non thread-safe build of PHP with IIS 7.0 FastCGI. A non thread-safe build of
PHP provides significant performance gains over the standard build by not doing any thread-safety
checks. These are not necessary as FastCGI provides a single threaded execution environment.
1. Download the latest non thread-safe zip package with binaries of PHP from
http://www.php.net/downloads.php.
2. Unpack the zip package to C:\PHP.
Page | 8
3. Rename the file php.ini-development to php.ini.
Note: Depending on the PHP distribution, you might find php.ini-recommended instead.
4. Open the php.ini file in a text editor.
5. Locate the extension_dir setting, uncomment the line if necessary, and change its value to
"./ext".
6. Locate the cgi.force_redirect setting, remove the comment and set the value to 0.
Note: cgi.force_redirect is set to 0 for IIS and to 1 if you are using Apache or iPlanet web
servers.
7. Locate the cgi.fix_pathinfo setting and uncomment it. Leave the value as 1.
Note: cgi.fix_pathinfo provides full PATH_INFO/PATH_TRANSLATED support for CGI.
Previously, the behavior of PHP was to set PATH_TRANSLATED to SCRIPT_FILENAME and to
ignore PATH_INFO. For more information on PATH_INFO, see the CGI specs. Setting this to 1
will cause PHP CGI to fix its paths to conform to the spec.
8. Locate the fastcgi.impersonate setting, uncomment it and leave the value as 1.
Note: FastCGI under IIS supports the ability to impersonate security tokens of the calling client.
This allows IIS to define the security context used to execute the request.
9. Locate the date.timezone setting, uncomment it and set the value to your time zone.
Note: Use a valid time zone identifier that matches the time zone of your system. For example,
date.timezone = " Antarctica/South_Pole"
For a list of supported time zones, see http://www.php.net/manual/en/timezones.php.
10. Save and close the file.
11. At the command prompt, change the current directory to the C:\PHP folder, and then execute
the following command to test if the PHP installation is successful.
Command Prompt
C:\PHP>php -info
Page | 9
Note: If you installed PHP correctly and all its dependencies are available in the machine, then
this command will output the current PHP configuration information.
Task 3 – Creating a Simple PHP Application
In this task, you host a sample PHP application on IIS 7.0 using FastCGI and use the Appcmd.exe
command-line tool to manage your PHP website.
Appcmd exposes all key server management functionality through a set of intuitive management objects
that can be manipulated from the command line or from scripts.
1. Open index.php inside Ex01 - FastCgiOnIIS7\begin\myphpapp in the Source folder of the lab.
Use Notepad or your favorite text editor and change the content of index.php to the following.
PHP
<?php phpinfo(); ?>
Note: phpinfo() is commonly used to check configuration settings and available predefined
variables on a given system.
2. Open an elevated (Run as administrator) command prompt window.
3. At the command prompt, use Appcmd to create a virtual directory for your PHP application
located under the Default Web Site.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" add app /site.name:"Default Web Site"
/path:"/myphpapp"
/physicalPath:"C:\WindowsAzurePlatformKit\Labs\WindowsAzurePHPVS2010\Source\Ex
01 - FastCgiOnIIS7\begin\myphpapp"
Note: The physicalPath parameter points to a location within the Source folder of the lab. You
may need to adjust this path if you installed the lab material to a different location.
If the virtual directory already exists under Default Web Site, use the following command to
delete the virtual directory before executing the command above.
"%windir%\system32\inetsrv\appcmd.exe" delete app "Default Web Site/myphpapp"
Page | 10
Figure 3
Creating a virtual directory in IIS for the PHP application
4. Use Appcmd to clear any existing FastCGI settings before updating the applicationHost.config
file with your new settings.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" clear config -section:fastCGI
Figure 4
Clearing existing FastCGI settings
5. Now, update the applicationHost.config file with the recommended FastCGI settings. Make
sure that FastCGI always recycles the php-cgi.exe processes before the native PHP recycling
kicks in, by setting the instanceMaxRequests attribute.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" set config -section:fastCgi
/+"[fullPath='C:\PHP\php-
cgi.exe',arguments='',maxInstances='4',idleTimeout='300',activityTimeout='30',
requestTimeout='90',instanceMaxRequests='10000',
protocol='NamedPipe',flushNamedPipe='False']" /commit:apphost
Page | 11
Figure 5
Configuring FastCGI for PHP
6. Also, add the PHP_FCGI_MAX_REQUESTS environment variable and set its value to 10000,
which is equal to the value chosen for instanceMaxRequests in the command above.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" set config -section:fastCgi
/+[fullPath='C:\PHP\php-
cgi.exe'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']
Note: By setting instanceMaxRequests to be smaller or equal to PHP_FCGI_MAX_REQUESTS,
you ensure that the native PHP process recycling logic will never kick in. If you do not
configure these settings, then the following default settings are used: instanceMaxRequests =
200, PHP_FCGI_MAX_REQUESTS = 500 (on most PHP builds).
The fullPath attribute in the Appcmd refers to the PHP interpreter that can be found in your
PHP installation directory.
Figure 6
Configuring the PHP environment
7. Check the applied FastCGI settings using Appcmd to list the relevant configuration section.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" list config /section:fastCGI
Page | 12
8. If you applied the settings correctly, the output should be similar to that shown in the figure
below, which displays the settings applied in the FastCGI section using an XML format.
Figure 7
Verifying the applied FastCGI settings
9. Next, set the PHP handler for your PHP application.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" set config "Default Web Site/myphpapp"
-section:system.webServer/handlers
/+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',script
Processor='C:\PHP\php-cgi.exe',resourceType='Unspecified']
Note: This step creates a Web.config file with the required settings in the application
directory. If any handler mappings already exist for your PHP application, use the following
command to clear them:
"%windir%\system32\inetsrv\appcmd.exe" clear config "Default Web Site/myphpapp" -
section:system.webServer/handlers
Figure 8
Configuring the PHP handler for the application
Page | 13
10. Set the default document for your PHP application to index.php. This is necessary because, by
default, IIS 7.0 does not identify index.php as a default document.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" set config "Default Web Site/myphpapp"
-section:defaultDocument /enabled:true /+files.[@start,value='index.php']
Note: This command updates the Web.config file created in the previous step. If a default
document is already configured for your PHP application, use the following console command
to clear it:
"%windir%\system32\inetsrv\appcmd.exe" clear config "Default Web Site/myphpapp" -
section:defaultDocument
Figure 9
Configuring the default document
11. You can now list the default documents defined for your PHP application.
Command Prompt
"%windir%\system32\inetsrv\appcmd.exe" list config "Default Web Site/myphpapp"
/section:defaultDocument
12. If you configured the default document correctly, the output should be similar to the figure
shown below that includes index.php among the list default documents configured for your
application and Default Web Site.
Page | 14
Figure 10
Listing default documents configured for the PHP application
13. Open the Web.config file inside Ex01 - FastCgiOnIIS7\begin\myphpapp in the Source folder of
the lab. Verify the PHP handler and default page configuration, which should match the settings
shown below.
Web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="PHP_via_FastCGI" path="*.php" verb="*"
modules="FastCgiModule"
scriptProcessor="C:\PHP\php-cgi.exe" resourceType="Unspecified" />
</handlers>
<defaultDocument enabled="true">
<files>
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Note: The Web.config file shows the PHP handler mapping applied to your PHP application.
14. Verify that you can now browse your PHP application by pointing your favorite Web browser to
http://localhost/myphpapp. If you have configured your PHP application correctly, you should
be able to see your current PHP configuration, as shown in the following figure.
Page | 15
Figure 11
Application output after connecting successfully to your IIS Web server
Note: If you are using the end solution to perform the verification, remember to configure the
physical path of the myphpapp virtual directory to point to the directory of the end solution
instead.
To configure the virtual directory, open an elevated (run as Administrator) command prompt.
If myphpapp already exists under Default Web Site, use the following command to delete it.
"%windir%\system32\inetsrv\appcmd.exe" delete app "Default Web Site/myphpapp"
To create the virtual directory and point it to the end folder of the solution, use the following
command.
"%windir%\system32\inetsrv\appcmd.exe" add app /site.name:"Default Web Site"
/path:"/myphpapp"
Page | 16
/physicalPath:"C:\WindowsAzurePlatformKit\Labs\WindowsAzurePHP\Source\Ex01 -
FastCgiOnIIS7\end\myphpapp"
Remember to adjust the physicalPath parameter if you installed the lab material to a different
location.
Exercise 2: Using Windows Azure to Host
PHP Applications
Windows Azure supports the IIS 7.0 FastCGI module. Using this module, developers may host web roles
that call applications written in third-party languages such as PHP.
In this exercise, you host the sample PHP application you created in Exercise 1 on Windows Azure. You
deploy your PHP application to the Development Fabric. The Development Fabric is provided in the
Windows Azure SDK to simulate the Windows Azure environment in your local machine and allow you to
run and test your service locally before deploying it to the cloud.
Task 1 – Updating the Web.roleconfig File
In this task, you update the Web.roleconfig file, which is used to configure IIS to run FastCGI in the
Development Fabric and in the Windows Azure cloud. The Web.roleconfig file is a special file applicable
only to Windows Azure that holds the same FastCGI settings that you find in the applicationHost.config
file in a standalone IIS 7.0 role. The reason to hold these settings in a separate file as opposed to the
applicationHost.config file is that Windows Azure does not expose the other system level settings in
applicationHost.config for clients to change (e.g. Application Pools or Security Settings). The addition
of this special file was necessary to separate the settings that could be changed on a system-wide basis.
1. Open the Web.roleconfig file located inside Ex02 - PHPonWindowsAzure\begin\myphpapp in
the Source folder of the lab. Use Notepad or your favorite text editor and replace the contents
of the file with the following configuration settings. You may need to create this text file if you
continue from the completed solution in Exercise 1 instead of using the begin solution for
Exercise 2.
(Code Snippet – Windows Azure PHP - Ex1 Step1-1 Web.roleconfig – XML)
XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<fastCgi>
Page | 17
<application fullPath="%RoleRoot%\approot\php\php-cgi.exe" />
</fastCgi>
</system.webServer>
</configuration>
Note: This configuration provides the absolute path to the PHP interpreter. To specify the
absolute path, precede the interpreter file name with %RoleRoot%\approot, which returns
the absolute path to the directory in which the role is running and approot, which corresponds
to the root of your web role project; the same location as the project's Web.config file and the
Web.roleconfig file.
This is similar to the way that the on-premises configuration of IIS 7.0 pointed to the local file
system for the PHP interpreter in Exercise 1. The %RoleRoot% environment variable returns
the absolute path to the directory in which the role is running, whether you host the
application in the Windows Azure cloud or in the local Development Fabric.
Task 2 – Configuring Handlers for FastCGI
In this task, you update the PHP handler configuration to point to the PHP interpreter that you deploy
with your application.
1. Open Web.config located inside Ex02 – PHPonWindowsAzure\begin\myphpapp in the Source
folder of the lab.
2. In the system.webServer\handlers section, locate the entry for the handler named
PHP_via_FastCGI and replace the current value of the scriptProcessor setting with
"%RoleRoot%\approot\php\php-cgi.exe".
Figure 12
Configuring the PHP handler for deployment to Azure
Page | 18
Note: Previously, the handler specified a path to your local disk. In order to deploy your
application to the Windows Azure environment, you update this value to be relative to the
root directory in which the role executes. You will copy the PHP distribution to this directory
shortly.
Task 3 – Creating a Service Definition File
In this task, you create a service definition file. The service definition file indicates the type of role
implemented in your service, which may consist of any number of web roles or worker roles. For a web
role, the service definition file specifies whether it exposes an HTTP endpoint or an HTTPS endpoint, or
both.
1. Update the service definition file to define a Web role to host the PHP application built in
Exercise 1. To do this, open ServiceDefinition.csdef inside Ex02 – PHPonWindowsAzure\begin
in the Source folder of the lab and insert the following settings.
(Code Snippet – Windows Azure PHP- Ex1 Step3-1 ServiceDefinition.csdef – XML)
XML
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="myphpapp"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole" enableNativeCodeExecution="true">
<InputEndpoints>
<InputEndpoint name="HttpIn" protocol="http" port="80" />
</InputEndpoints>
</WebRole>
</ServiceDefinition>
Note: Setting the enableNativeCodeExecution attribute to true for a role in your service
definition file enables support for native code applications. Native code support is necessary
for FastCGI processes to execute.
Task 4 – Copying your PHP Interpreter into the PHP Project
In this task, you include the binaries for the PHP interpreter in your project in order to use FastCGI to
execute the application. For an interpreter to be supported in Windows Azure, it must be possible to
deploy it using XCOPY.
1. Open an elevated Windows Azure SDK command prompt. To do this, right-click the Windows
Azure SDK Command Prompt shortcut in Start | All Programs | Windows Azure SDK v1.2 and
select Run as administrator.
Page | 19
2. At the command prompt, change the current directory to the exercise folder.
Windows Azure SDK Command Prompt
cd "C:\WindowsAzurePlatformKit\Labs\WindowsAzurePHPVS2010\Source\Ex02 -
PHPonWindowsAzure\begin"
3. Now, execute the following commands to create the Web role application folder where you will
create your service package.
Windows Azure SDK Command Prompt
C:\...\Ex02 - PHPonWindowsAzure\begin>md myphpapp_WebRole
C:\...\Ex02 - PHPonWindowsAzure\begin>md myphpapp_WebRole\bin
4. Next, copy the PHP application and PHP assemblies to the web role folder.
Windows Azure SDK Command Prompt
C:\...\Ex02 - PHPonWindowsAzure\begin>robocopy myphpapp myphpapp_WebRole\bin
/E
C:\...\Ex02 - PHPonWindowsAzure\begin>robocopy C:\PHP myphpapp_WebRole\bin\php
/E
Note: The location of the interpreter must correspond to the relative path and file name that
you specified in the fullPath attribute of the application element in the webRole.config file. In
this case, the value is relative to %RoleRoot%\approot, which corresponds to the root of the
web role project.
Task 5 – Preparing your Service for Deployment
In this task, you use the cspack.exe command-line tool. This tool is included in the Windows Azure SDK
to prepare your service for deployment to the local Development Fabric.
1. At the command prompt, execute cspack.exe to build the service package specifying the name
of the service definition file and the type of role for the service.
Windows Azure SDK Command Prompt
cspack.exe "ServiceDefinition.csdef" /role:WebRole;"myphpapp_WebRole\bin"
/copyOnly
/generateConfigurationFile:"ServiceDefinition.csx\ServiceConfiguration.cscfg"
Note: You must call cspack.exe with the /copyOnly option only if you want to prepare the
service for local deployment.
Page | 20
/copyOnly
Creates a directory layout for the role binaries, which the development fabric can use to run
the service locally.
/generateConfigurationFile
Generates a skeleton configuration file, ServiceConfiguration.cscfg, for your service that can
be edited to specify the value of the settings and number of role instances in the service
before deploying it.
2. Verify that the command to build the service package executed successfully. Open Windows
Explorer and navigate to Ex02 – PHPonWindowsAzure\begin\ in the Source folder of the lab.
You should see a new directory layout created in your lab folder as shown in the following
figure.
Figure 13
Windows Explorer showing the application directory layout created by CSPACK
3. For further verification that the command executed successfully, open
ServiceConfiguration.cscfg, located inside Ex02 – PHPonWindowsAzure\begin\
ServiceDefinition.csx in the Source folder of the lab and ensure that it contains the following
settings.
(Code Snippet – Windows Azure PHP- Ex1 Step5-3 ServiceConfiguration.csfg – XML)
Page | 21
XML
<?xml version="1.0"?>
<ServiceConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" serviceName="myphpapp"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguratio
n">
<Role name="WebRole">
<ConfigurationSettings />
<Instances count="1" />
</Role>
</ServiceConfiguration>
Note: The Instances element specifies the required number of role instances for the service to
operate. By increasing the value of this attribute, you instruct the Development Fabric and the
Windows Azure hosting environment (if deployed to the cloud) to run multiple instances of the
Web role, which is generally recommended.
Task 6 – Deploying your Service to the Azure Development Fabric
In this task, you use the csrun.exe command-line tool to deploy your service to the local Development
Fabric. This tool is included in the Windows Azure SDK.
1. At the Windows Azure command prompt, execute the csrun command to deploy your service to
the development fabric specifying the location of the service package directory
(ServiceDefinition.csx) and the service configuration file (ServiceConfiguration.cscfg).
Windows Azure SDK Command Prompt
csrun.exe "ServiceDefinition.csx"
"ServiceDefinition.csx\ServiceConfiguration.cscfg" /launchBrowser
Note: The service directory and the service configuration file were created using the cspack
command in the previous task. If you are running as a non-administrative user, you are
prompted for administrator credentials when you perform this action.
2. If the command executes successfully, the output should be similar to that shown in the figure
below that displays your service deployment endpoint. When hosting your service in the
Development Fabric for 32-bit systems, a message is also shown warning that Windows Azure
hosted services run in a 64-bit environment.
Page | 22
Note: The deployment endpoint is typically http://127.0.0.1:81/. Each time you create a new
deployment, the development fabric assigns the first available port at or above the port
number specified in the service definition file.
Figure 14
Command prompt showing the service deployment endpoint
3. If the deployment is successful, the above command will also launch your service using the
default browser, as shown in the following figure.
Page | 23
Figure 15
Running application following the successful deployment to the Development Fabric
Verification
In this task, you verify that you deployed the PHP application to the Azure Development fabric
successfully. You first point your browser at the deployment endpoint, and then check to see the
service running in the development fabric.
1. Point your browser to the deployment endpoint http://127.0.0.1:81/.
2. Open the development fabric UI by right clicking its icon in the system tray and selecting Show
Development Fabric UI.
Page | 24
Figure 16
Opening the development fabric UI
3. Notice the deployment for the myphpapp service and a single Web role instance associated
with the service, as shown in the figure below.
Figure 17
Development Fabric showing the deployed PHP Web role
4. To stop the running service deployment, right-click the deployment in the Development Fabric
and select Remove.
Page | 25
Figure 18
Removing a service deployment in the Development Fabric
Exercise 3: Creating PHP Applications
with Visual Studio
In this exercise, you use Microsoft Visual Studio and the Windows Azure Tools for Microsoft Visual
Studio to create a PHP application and execute it in the local Development Fabric. To do this, you repeat
essentially the same tasks that you performed in the previous exercise to appreciate how these are
streamlined using the tools and templates included in Visual Studio.
Task 1 – Creating the Visual Studio Project
In this task, you create a new Cloud Service project in Visual Studio for building a web role that hosts a
FastCGI application.
Page | 26
1. Open Microsoft Visual Studio 2010 elevated as Administrator from Start | All Programs |
Microsoft Visual Studio 2010 by right-clicking Microsoft Visual Studio 2010 and choosing Run
as administrator.
2. If the User Account Control dialog appears, click Continue.
3. From the File menu, choose New and then Project.
4. In the New Project dialog, expand the language of your preference (Visual C# or Visual Basic) in
the Installed Templates list and select Cloud. Choose the Windows Azure Cloud Service
template, set the Name of the project to myphpapp and the name of the solution to begin,
change the location to Ex03 - PHPAppsWithVisualStudio in the Source folder of the lab, and
ensure that Create directory for solution is checked. Click OK to create the project.
Note: The language chosen here (Visual C# or Visual Basic) is not relevant unless you decide to
include non-PHP content in the project.
Figure 19
Creating a new Windows Azure Cloud Service project
5. In the New Cloud Service Project dialog, inside the Roles panel, expand the tab for the language
of your choice (Visual C# or Visual Basic), select CGI Web Role from the list of available roles
and click the arrow (>) to add an instance of this role to the solution. Before closing the dialog,
select the new role in the right panel, click the pencil icon and rename the role as
PHP_WebRole. Click OK to create the cloud service solution.
Page | 27
Figure 20
Assigning roles to the cloud service project
6. Add a PHP script to the project. In Solution Explorer, right-click the PHP_WebRole project,
point to Add and select New Item. In the Add New Item dialog, choose the General category
and then the Text File template, set the name to index.php and click Add.
Figure 21
Page | 28
Adding a PHP script to the project
7. Open index.php and insert the following content into the file.
PHP
<?php phpinfo(); ?>
Note: phpinfo() is commonly used to check configuration settings and available predefined
variables on a given system.
8. Configure a default document for the application. Open the Web.config file in the
PHP_WebRole project, locate the system.webServer section and insert the following
(highlighted) configuration fragment.
XML
<system.webServer>
...
<modules ... />
<handlers>
...
</handlers>
<defaultDocument enabled="true">
<files>
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
9. In Solution Explorer, review the structure of the created solution.
Page | 29
Figure 22
Solution Explorer showing the PHP cloud application
Note: The generated solution contains two separate projects. The first project, named
myphpapp, holds the configuration for the web and worker roles that compose the cloud
application. It includes the service definition file, ServiceDefinition.csdef, which contains
metadata needed by the Windows Azure fabric to understand the requirements of your
application, such as which roles are used, their trust level, the endpoints exposed by each role,
the local storage requirements and the certificates used by the roles. The service definition
also establishes configuration settings specific to the application. The service configuration file,
ServiceConfiguration.cscfg, specifies the number of instances to run for each role and sets the
value of configuration settings defined in the service definition file. This separation between
service definition and configuration allows you to update the settings of a running application
by uploading a new service configuration file.
The Roles node in the cloud service project enables you to configure what roles the service
includes (Web, worker or both) as well as which projects to associate with these roles. Adding
and configuring roles through the Roles node will update the ServiceDefinition.csdef and
ServiceConfiguration.cscfg files.
The second project, named PHP_WebRole, is a Web Application project to host your PHP
application. It contains a Web.config file containing configuration settings for the Web
application and a Web.roleconfig file with FastCGI-specific settings.
Page | 30
Task 2 – Updating the Web.roleconfig File
In this task, you update the Web.roleconfig file, which is used to configure IIS to run FastCGI in the
Development Fabric and in the Windows Azure cloud.
1. Open the Web.roleconfig file in the PHP_WebRole project to configure the FastCGI application.
2. Locate the fastCGI section inside the system.webServer section and uncomment the
application element it contains.
Note: For CGI Web Roles, Visual Studio includes this setting in its template. To complete the
configuration, you need to uncomment the corresponding element and specify the path to the
CGI application.
3. In the application element, change the value of the fullPath attribute to
"%RoleRoot%\approot\php\php-cgi.exe" and set the arguments attribute to an empty string.
Figure 23
Configuring the path to the FastCGI application
Note: The fullPath attribute points to the location of the CGI application binaries, in this case
the PHP interpreter. The path is given relative to the root directory of the role when deployed
in the Azure environment and is indicated by using the special environment variable
%RoleRoot% and the approot directory that corresponds to the root of the web role project.
Task 3 – Configuring Handlers for FastCGI
Page | 31
In this task, you configure the PHP handler to point to the PHP interpreter that you deploy with your
application.
1. Open the Web.config file in the PHP_WebRole project to configure the handler for the PHP
content.
2. Locate the handlers section inside system.webServer and uncomment the handler named
FastCGI Handler.
3. Change the path attribute of the FastCGI Handler to the string "*.php" to indicate that this
handler should process any request for documents with this extension.
4. Next, change the scriptProcessor attribute to the value "%RoleRoot%\approot\php\php-
cgi.exe".
Figure 24
Configuring the FASTCGI handler
Note: Visual Studio includes the necessary handler settings in the Web.config file it generates
for CGI Web Role projects. To enable the handler, you need to uncomment the corresponding
element in the configuration file, provide the location of the scriptProcessor and configure the
path for PHP content.
Task 4 – Copying your PHP Interpreter into the PHP Project
Page | 32
In this task, you copy the PHP interpreter into your project in order to include its binaries in the service
package that you deploy to the Azure environment. For an interpreter to be supported in Windows
Azure, it must be possible to deploy it using xcopy.
1. Using Windows Explorer, browse to the folder where you installed the PHP interpreter in
Exercise 1. If you followed the lab instructions, it should be located in C:\PHP.
2. Ensure that the Visual Studio window is visible, then drag and drop the PHP folder from the
Windows Explorer window onto the PHP_WebRole project node in Solution Explorer. This
copies the contents of this folder into the project.
Figure 25
Including the PHP interpreter in the Visual Studio solution
Verification
You are now ready to test the completed PHP application in the Development Fabric.
1. Press F5 to build and run the solution. Once the build completes and the application is deployed
to the Development Fabric, the browser opens and shows the default document, which you
defined in Task 1.
Page | 33
Figure 26
Viewing the PHP application in the browser
2. Open the development fabric UI by right clicking its icon in the system tray and selecting Show
Development Fabric UI.
3. Notice the deployment for the myphpapp service and a single Web role instance associated
with the service, as shown in the figure below.
Page | 34
Figure 27
Fast CGI role running in the Development Fabric
Summary
By completing this hands-on lab, you learnt how to enable FastCGI and host PHP applications on IIS 7.0
and Windows Azure. In addition, you saw how to use the Windows Azure Tools for Microsoft Visual
Studio to create a cloud service project that can host PHP applications.