Abstract
This article will show how simple it is to run a PHP
application in Windows Azure. We will utilize the Windows Azure Command-line
Tools for PHP and a very simple PHP application. This article should provide
enough information to get started in the Windows Azure environment.
Introduction
One of the strengths and main selling points of Windows
Azure as a cloud platform is that it can be utilized by developers with more
than just .NET skills. One of the many alternative development platforms that
can be used on Windows Azure is PHP. This article discusses how to use the Windows
Azure Command-line Tools for PHP to compile a simple PHP application into a
package that can be deployed on a Windows Azure Web Role. It then demonstrates
that PHP was successfully installed by showing the ubiquitous PHP Info page.
Setup
There isn’t much that needs to be setup for this article. A
quick stop over at http://azurephptools.codeplex.com
to download the latest version of the Windows Azure Command-line Tools for PHP
Developers is all that is needed. Download the current version which at the
time of writing is dated June 2011. The download is a zip file. Save the zip
file and unzip the contents. We will use this later to package our PHP
application.
Note: There are a few pre-requisites before you can
actually get the command line tool to work. You will need to first install the
Windows Azure SDK and PHP, if you don’t already have it. For a quick summary
on how to install these via the Web Platform Installer, check
out this article.
Before continuing on, a word about zip files downloaded from
the web. In Windows Vista and Windows 7 the zip file is marked to tell the
operating system that it may contain harmful applications. We need to remove
this mark before unzipping the contents. To do this right click on the zip file
and select properties from the context menu that appears. In the file properties
dialog click the Unblock button at the bottom. Then continue unzipping the
package.
PHP developers can utilize a large number of tools for
developing their applications. These tools range from simple text editors like
Notepad, vi, and Emacs to large Integrated Development Environments like
Eclipse, Zend Studio, and PHPStorm. This ability to choose from multiple
platforms and tools is a strength of PHP development. The developer isn’t tied
into one platform or development environment. This article will not favor one
development environment over another. It assumes that we already have a PHP
application ready to push up to Windows Azure.
Creating the Project
For the example in this article we will create a real simple
single page PHP application that will output the PHP Information table. Create
a folder for the application and within this folder create a text file called
index.php. Open this file and enter the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>PHP on Azure Demo</title>
</head>
<body>
<?php phpinfo(); ?>
</body>
</html>
Save this code.
Before we begin packaging up the application to deploy on
Azure, we should gather some basic information first. Make a simple table and
fill in the information on the right:
Title | Use | Value |
Azure Tools Directory | We’ll need to know where the tools were downloaded to. | |
Project Name | The name of the project | |
PHP Runtime Path | The path to the local version of PHP. Do not include the trailing
slash, | |
Source Path | The path to the source files for the application. | |
Target Path | The path for where the output of the package should be located. | |
Once we have filled in the information in the right column
we are ready to package our application. Open a command prompt. In Windows
Vista and Windows 7 make sure to run the command prompt as an administrator.
Navigate to the Azure Tools Directory folder and run the following command
replacing the variable values with the values from the table above:
php
package.php --project={Project Name} --source={Source Path} --phpRuntime={PHP
Runtime Path} --target={Target Path}
There are some additional, more advanced, parameters we
could pass in here, but this will give us the basics that we need to package up
our application.
There are a couple of errors that are commonly seen when
running this script.
- If the following error message appears then the PHP executable isn’t in
the path:
Execute this statement then try the packager again:
path
= %path%;{PHP Runtime Path}
- If this message appears:
Then the trailing slash was added to the phpRuntime
parameter.
When any error messages are cleared up we should see output
similar to this:
That’s all there is to building the application into a
package that we can deploy to Windows Azure. Be sure to note the location of
the Deployment Package and Configuration files from this output window. We’ll
need this information when we deploy the application to Windows Azure in the
next section.
Deploying the Project
Deploying the application to Windows Azure is a fairly
simple task. First we’ll need a Windows Azure account. A free Windows Azure
trial is available through the end of September 2011 from this address: http://www.microsoft.com/windowsazure/free-trial/
After we have a Windows Azure account sign into the
account<http://windows.azure.com/> using the Windows Live ID used to
create the account.
Next we need to create a new hosted service. To create a new
hosted service, click on “New Hosted Service” in the top ribbon of the Windows
Azure Platform portal. A modal dialog that looks like this should appear:
Next we need to fill in the fields for this new hosted
service. Enter the name of the service and a URL. Typically this is the same as
the application name, but it doesn’t have to be. Also the URL needs to be
unique across all of Windows Azure. Also enter the deployment name; again this
can be the same as the application name.
Finally we need to upload the application package and the
configuration file. For each of these click the “Browse Locally…” button and
navigate to the package location as specified in the output from above:
Select the .cspkg file for the Package and the .cscfg file
for the Configuration file. Click the OK button and Windows Azure will begin
uploading the package. A warning message will probably appear alerting us to
the fact that we only have one instance and two instances are recommended for
high availability. Click the Yes button to override and submit the app.
Once the application has been uploaded, Windows Azure will
start the deployment. After deployment has finished, we can navigate to the
staging version of our application running in the Windows Azure Cloud. This
staging version will have a Globally Unique Identifier (GUID) for a URL and the
URL can be found in the right side of the main portal.
Clicking on this URL will open a Web Browser showing us our
application:
Conclusion
This has been a very brief overview of how to use the
Windows Azure Command-line Tools for PHP to build a package that can be
uploaded to a new Windows Azure WebRole. There is a lot more that can be done
with PHP within Windows Azure including utilizing Windows Azure SQL Storage.
There are a lot of really good resources of information available on the
Internet to learn how to do more with Windows Azure and PHP. A recommended
first stop is this URL: http://azurephp.interoperabilitybridges.com/
Here links to more information from Stack Overflow, tutorials, and downloads
can be found.
More information on using the Windows Azure Command-line
Tools for PHP can be found on the documentation page of the CodePlex project: http://azurephptools.codeplex.com/documentation