Module 1: Install and Run PHP Locally
Updated: December 8, 2011
This module demonstrates how to install PHP on your local computer and run a sample application in the Compute Emulator. The estimated time to complete this module is 15 minutes. If you have not already installed the Web Platform Installer (WebPI), you can download it from this location.
Installing PHP on Your Local Computer
Installing PHP By Using the Web Platform Installer
Open the WebPI. Before we search for PHP, we need to configure the WebPI to return results that apply to IIS, not IIS Express. Click Options from the bottom menu; the Change Options dialog appears. From the Which Web Server would you like to use? menu, select IIS, then click OK.
In the search bar, enter “PHP”. Locate the latest version of PHP. At the time of publication, the latest version was version 5.3.8. Select the latest version, click Add, and then click Install.
A list of items is displayed, including the dependencies for PHP. If you want to proceed, click I Accept.
The items will install, and a dialog of installed items will appear. Click Finish to proceed.
PHP 5.3.8 has now been installed. To verify that the installation was successful, we can perform the verification steps listed in the next section.
Verify the PHP Installation
Start the Windows Azure SDK Command Prompt. You can start the prompt by clicking Start, All Programs, Windows Azure SDK 1.5, and finally Windows Azure SDK Command Prompt. Alternatively, the command prompt can be located in the bin folder of your install location for the SDK, under the filename setenv.cmd.
From the Windows Azure SDK Command Prompt, enter the following statement in the prompt and press Enter:
The prompt will display information about your local PHP environment if the installation was successful.
Running a PHP Application in the Compute Emulator
Now that we have verified that PHP is installed correctly, we can run a sample application in the Compute Emulator. The Compute Emulator simulates how an application would run on Windows Azure, but allows for local testing and debugging. For more information about the Compute Emulator, see Overview of the Windows Azure SDK Tools.
We will create a sample application that will display the same information as the
php –i command we used in the previous steps, but it will be displayed in a web browser and served by the Compute Emulator. This file will call a built-in PHP method called
phpinfo(), which displays information about your web server and PHP configuration.
Create an info.php File
We need to create a few folders for our project to reside in. First, create a root folder for the project, such as C:\PHPExample. Inside that folder, create a folder named Deployment. The resulting folder structure should look something like this: C:\PHPExample\Deployment.
Open a new Notepad document and enter the following code:
<?php phpinfo(); ?>
Save this file in the Deployment folder and name it info.php.
Create a ServiceDefinition.csdef File and Generate a Local Deployment Package
All deployments in Windows Azure require a ServiceDefinition.csdef and ServiceConfiguration.cscfg file. We can use a ServiceDefinition.csdef file to generate a ServiceConfiguration.cscfg file, so we will create it first. Open a new Notepad document and enter the following XML:
<?xml version="1.0" encoding="utf-8"?> <ServiceDefinition name="PHPExample" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="Deployment" vmsize="ExtraSmall" enableNativeCodeExecution="true"> <Sites> <Site name="PHPInfo" physicalDirectory=".\Deployment"> <Bindings> <Binding name="HttpEndpoint" endpointName="defaultHttpEndpoint" /> </Bindings> </Site> </Sites> <InputEndpoints> <InputEndpoint name="defaultHttpIn" protocol="http" port="80" /> </InputEndpoints> </WebRole> </ServiceDefinition>
This XML defines a new extra small web role named “Deployment” that also specifies that our site is located within the Deployment directory.
Save this file in the root project directory and name it ServiceDefinition.csdef. For example: C:\PHPExample\ServiceDefinition.csdef.
Once the ServiceDefinition.csdef has been created, we can use a tool included with the Windows Azure SDK called cspack to generate our ServiceConfiguration.cscfg file and create the necessary files for local deployment. cspack is included with the Windows Azure SDK, and the Windows Azure SDK Command Prompt automatically recognizes it as an environment variable. Open the prompt, navigate to the root directory for our project, and enter the following statement into the prompt:
cspack ServiceDefinition.csdef /generateConfigurationFile:ServiceConfiguration.cscfg /copyOnly
Using this statement, cspack will use the specified ServiceDefinition.csdef file to generate a new configuration file named ServiceConfiguration.cscfg. The /copyOnly option creates a folder for local deployment to the Compute Emulator only. To create a deployment package for Windows Azure, remove this option. For more information about cspack, see CSPack Command-Line Tool.
To run a local deployment package in the Compute Emulator, we can use the csrun tool. Like cspack, this tool is included with the SDK, and can be called from within the Windows Azure SDK Command Prompt. In the open prompt, enter the following statement:
csrun ServiceDefinition.csx ServiceConfiguration.cscfg /launchbrowser
Using this statement, csrun will execute the contents of the ServiceDefinition.csx folder created by cspack, in conjunction with the ServiceConfiguration.cscfg file. The /launchbrowser option simply opens your default browser and navigates to the IP address and port reserved by the Compute Emulator for the web role.
If the following steps were successful, your browser will appear and you can navigate to info.php, which displays the phpinfo() page. Next, we can move our application to the cloud on Windows Azure.