Advanced Web and Worker Roles
Exercise 3: Using Start-Up tasks to install PHP with the Web Platform Installer

Collapse All Expand All Language Filter: All Language Filter: Multiple Language Filter: Visual Basic Language Filter: C#

Visual Basic Usage
C#

Applications deployed to the cloud usually have a set of prerequisites that must be installed on the host computer to be able to work correctly. In this kind of scenario, having the capabilities of the Microsoft Web Platform Installer (Web PI) at your service can be very handy.

In this exercise, you explore another possible use for start-up tasks in conjunction with the Web Platform Installer. You will learn how to install complex components such as the binary files for a new scripting language. To do this, you will create a simple PHP page, requiring that the PHP script processor be installed in order to work.

Task 1 – Installing PHP with the Web Platform installer

In this task, you will use multiple start-up tasks to install the Web Platform Installer and then install PHP using the Web PI.

1. Start Microsoft Visual Studio 2010 as an administrator.
2. Open the Begin solution located in Ex3-InstallPHP in the Source folder of the lab. The solution contains a Windows Azure project and a Web role to which you will add a PHP page.
3. Add a new PHP page at the root of the PHPWebRole project named info.php. To do this, you can select the Text File template in the Add New Item dialog and type info.php in the Name field.

   Figure 14
   Creating the info.php page

4. Add the following code to the info.php file.

   PHP | Copy Code
   --- | ---
       <?php
         phpinfo();
       ?>

5. Now, open the Web.config file, locate the system.webServer element, and then insert the following (highlighted) defaultDocument element to set the default document as info.php.

   (Code Snippet – AdvancedWebAndWorkerRoles-Ex3-01-DefaultDocument-XML)

   XML | Copy Code
   --- | ---
       <configuration>
         ...
         <system.webServer>
           <modules runAllManagedModulesForAllRequests="true"/>
           <defaultDocument>
             <files>
               <add value="info.php" />
             </files>
           </defaultDocument>
         </system.webServer>
       </configuration>

6. Set the InstallPHP project as the solution's start-up project.
7. Press F5 to launch the Web Role and wait for the web browser window to appear, which will indicate that the role has been initialized. As PHP is not installed, you will receive an error message like the one shown in the following figure. If the default document is not shown by default, try browsing to info.php.

   Figure 15
   Error when PHP is not installed

8. Close the web browser window.
9. In Solution Explorer, double-click InstallPHP.cmd in the PHPWebRole project to examine this script file in the editor.

   Figure 16
   Startup script to install PHP using the Web Platform Installer

   Note:
   The InstallPHP.cmd script installs the most recent version of PHP using the Web Platform Installer command line tool. This tool simplifies the installation of the latest components of the Microsoft Web Platform, such as IIS, PHP, and SQL Server Express, among others, and enables you to automate the application and service installation of a Windows Azure role. The script first enables the Windows Update service to allow the Web Platform Installer to use it for downloading components required by the PHP installation. Next, it launches the Web PI command line tool to install PHP. Once the installation is complete, it stops the Windows Update service and restores its startup mode to disabled.

   Note:
   Important: All startup script files have their Build Action set to None and Copy to Output Directory property set to Copy always.

10. In Visual Studio, open the ServiceDefinition.csdef file in the InstallPHP Web role project.
11. Add the (highlighted) Startup configuration elements in the following code snippet, below the ConfigurationSettings element closing tag. In general, this section defines startup tasks and, in this case, includes the script required to install PHP on a Windows Azure host.

    (Code Snippet – AdvancedWebAndWorkerRoles-Ex3-02-StartUpTasks-XML)

    XML | Copy Code
    --- | ---
        ...
        <WebRole name="PHPWebRole">
          <Sites>
            ...
          </Sites>
          <ConfigurationSettings>
            ...
          </ConfigurationSettings>
          <Startup>
            <Task commandLine="InstallPHP.cmd" executionContext="elevated" taskType="simple" />
          </Startup>
          <Endpoints>
            ...
          </Endpoints>
          <Imports>
            ...
          </Imports>
        </WebRole>
        ...

12. Press F5 to launch the Web role in the compute emulator.
13. Wait for the role to start and show its default page. While the startup tasks execute to install PHP, Visual Studio may warn you one or more times that the role is taking longer than expected to start. If this happens, click Yes to continue waiting.

    Figure 17
    Visual Studio warning during the PHP installation

    Note:
    Running the start-up task may take some time. Before continuing with the following steps, you should make sure PHP has been installed. You can check this in the Programs and Features list that can be accessed through the Control Panel.

14. Once the role starts successfully, a page similar to the one shown in the following figure is displayed, confirming that PHP was installed correctly.

    Figure 18
    Confirming that the PHP installation was successful