在微软MIX09大会上,Windows Azure发布了它的第3个版本——Match 2009 CTP。从这个版本开始,Windows Azure开始支持FastCGI。(详见MIX09-Windows Azure (March 2009 CTP)里振奋人心的新特性一览,by 流牛木马) 支持FastCGI,也就是意味着支持PHP、Ruby、Python等当前流行的"非微软"web应用程序。我们很欣喜地看到Azure Services Platform迈出了“开放平台”的第一步。
PHP也许是当前web开发界最最流行的语言,所以“在Azure中使用PHP”这个问题引起了很多人的关注,流牛木马本人在最近两天也收到了好几个朋友的焦急求助。
也许是微软的那些牛人们最近在忙MIX09的缘故,提及Azure支持"FastCGI”的新闻报道很多,而却几乎没有提及具体怎样在Azure里配置使用PHP。就连在新版的Azure SDK里面,关于FastCGI的sample也不是使用php的。
不用着急,在本篇中,流牛木马将会演示在Azure中使用PHP的最详细流程。
(已部署到云端的Demo: http://ibm.cloudapp.net/php.php )
第一步,你必须知道的一些有关PHP&FastCGI的基本知识。
FastCGI的官方网站:http://www.fastcgi.com/
PHP的FastCGI模式:http://www.blogme.cn/node/6529
第二步,启用你IIS7中的FastCGI模块。
【注:本地调试环境需要。如果你打算直接部署到Cloud上,则不需要这一步。】
(下面以Vista Ultimate中文版为例:)
- 打开控制面板,进入”打开或关闭Windows 功能”
- 启用以下模块(图中红框的部分)
- 下载安装最新的FastCGI模块:http://support.microsoft.com/kb/967131
第三步,下载一个"xcopy-deployable”版本的PHP。
"xcopy-deployable”这个词来自微软的官方文档,但是很遗憾,在php的官方网站http://php.net/中,我们却没有找到这个词. - -P
根据词义理解一下:可以直接拷贝部署的PHP版本。通俗地说就是不需要安装的。
来到PHP官方网站的下载中心:http://www.php.net/downloads.php,在Windows Binaries部分找到最新版本号的"zip package“就可以了。比如现在php版本的最新版本是2009年3月10日的5.2.9-1,下载地址: http://cn2.php.net/get/php-5.2.9-1-Win32.zip/from/a/mirror
第四步,新建Web Cloud Service项目。
第五步,配置Service Definition 文件(ServiceDefinition.csdef)。
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="CloudService1" 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>
注意:需要在WebRole节点下增加enableNativeCodeExecution="true"属性
第六步:配置FastCGI和PHP
- 将刚刚下载的xcopy-deployable PHP压缩包解压缩到WebRole项目的php子目录下。
- 在WebRole项目根目录下增加Web.roleconfig文件,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<fastCgi>
<application fullPath="%RoleRoot%\php\php-cgi.exe" />
</fastCgi>
</system.webServer>
</configuration>
这里我们使用了"%RoleRoot%"变量来获得Web Role的根目录。这也是Match 2009 CTP版本所提供的新特性之一。
- 在Web.config文件的system.webServer-handlers节下增加对PHP文件的处理:
<system.webServer>
<handlers>
<..>
<add name="PHP via FastCGI" path="*.php" verb="*" modules="FastCgiModule"
scriptProcessor="%RoleRoot%\php\php-cgi.exe"
resourceType="Unspecified" />
</handlers>
</system.webServer>
第七步:写一些简单的代码测试一下吧!
代码:
今年的MIX09大会真的一天比一天振奋人心,ASP.NET MVC 1.0,Silverlight 3.0, IE 8等核武器级的微软神物纷纷发布。当然,本博客的主要任务是介绍Azure,其他的东东就让别人介绍去吧!
Windows Azure在MIX09上迎来了它的第3个CTP版本——March 2009 CTP,这个版本的更新非常之重要!先列举流牛木马认为其最重要的更新:
- 支持部署Full Trust的应用程序!甚至你还可以类似"%RoleRoot%”的Native Code、你可以访问进程、访问注册表!
- 支持FastCGI应用程序。这对于Azure的竞争对手Google AppEngine来说绝对是大规模杀伤性武器级的消息。FastCGI为何物?(请自行参考http://www.fastcgi.com)支持FastCGI,意味着Windows Azure上支持PHP、Python、Ruby等各种流行的web开发语言!
- URL Rewrite。这个版本的Azure提供了非常方便的URL Rewrite模块,只需要在web.config里稍做配置就可以了。
- 支持本地代码调试。 以前的Windows Azure不支持本地代码调试,这算是一个缺陷吧,现在弥补了。
在Windows Azure上跑PHP的效果图:
一段PHP操作Windows Azure Table Storage的示例代码:
function fetch($pkey) {
$date = gmdate('D, d M Y H:i:s \G\M\T');
$stringtosign = "GET\n\n\n$date\n/tweval/TagRatingTable()";
$signature = 'SharedKey tweval:'.base64_encode(
hash_hmac('sha256', $stringtosign, base64_decode($key),true));
$session = curl_init('http://tweval.table.core.windows.net/TagRatingTable()'
.'?$filter=PartitionKey%20eq%20\'' . urlencode($pkey) . '\'');
curl_setopt($session, CURLOPT_HTTPHEADER, array("x-ms-date:$date", "Authorization:$signature"));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$result = array();
foreach(simplexml_load_string($response)->entry as $entry) {
$properties =
$entry>content>children('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata')
>properties>children('http://schemas.microsoft.com/ado/2007/08/dataservices');
$result[] = new Rating((string)($properties>Username), (int)($properties>StatusId),
(float)($properties>Score), (string)($properties>Tweet), new DateTime($properties>CreatedAt));
}
usort($result, 'cmp');
return $result;
}
如果你是Python爱好者,你会惊喜地发现使用Python来操作Windows Azure的服务也是那么容易:
http://github.com/sriramk/winazurestorage/tree/master
下载Windows Azure Tools for Visual Studio and SDK(合二为一的安装文件): http://go.microsoft.com/fwlink/?LinkId=128752