天天看點

使用Windows Azure在雲中托管PHP

More than a buzzword in executive meetings, cloud computing is the next big thing in the world of IT. Clouds offer an infinite amount of resources, both on demand and in pay-per-use models: computer resources on tap! In this article, I’ll focus on one of these cloud platforms, Microsoft’s Windows Azure, and give you all the information you need to get started developing PHP applications on this platform. Although we won’t go too deep into the technicalities, I will point you to further information and resources on specific points as we go.

雲計算不僅是高管會議上的一個時髦詞彙,而且是IT領域的下一件大事。 無論是按需還是按使用付費的模型,雲都提供了無限量的資源:計算機資源随時可用! 在本文中,我将重點介紹這些雲平台之一,即Microsoft的Windows Azure,并為您提供開始在該平台上開發PHP應用程式所需的所有資訊。 盡管我們不會對技術進行太深入的介紹,但是在我們進行的過程中,我将為您指出特定點的更多資訊和資源。

不同的雲 (Different Clouds)

Choice is a good thing. The great news for us developers is that there are many choices when it comes to cloud computing. Microsoft, Google, Amazon, Rackspace, GoGrid, and many others offer cloud products that have their own special characteristics. It looks like the whole world is dividing these offers into two distinct categories: IaaS (Infrastructure-as-a-Service) and PaaS (Platform-as-a-Service)—the difference between the two is illustrated in Figure 1, “The difference between cloud platforms”.

選擇是一件好事。 對于我們開發人員而言,好消息是,在雲計算方面有很多選擇。 微軟,谷歌,亞馬遜,Rackspace,GoGrid和許多其他公司提供具有自己特殊特征的雲産品。 似乎全世界都在将這些優惠分為兩個不同的類别: IaaS (基礎設施即服務)和PaaS (平台即服務)—兩者之間的差別如圖1所示。雲平台之間的差異”。

The difference between cloud platforms

雲平台之間的差別

First, let’s look at IaaS. Amazon EC2 was the first to offer virtual machines that could run your application. These virtual machines, however, are under your control, like physical servers in your data center. This means that you’re in control of patches, security, maintenance to the operating system—and all with full root or administrator access. The cloud platform takes the infrastructure woes out of your hands, as networking, load balancers, and firewalls are handled for you.

首先,讓我們看一下IaaS。 Amazon EC2是第一個提供可以運作您的應用程式的虛拟機的公司。 但是,這些虛拟機在您的控制之下,就像資料中心中的實體伺服器一樣。 這意味着您可以控制更新檔程式,安全性,作業系統維護以及所有操作,并且具有完全的root或管理者通路權限。 由于為您處理了網絡,負載平衡器和防火牆,是以雲平台消除了基礎架構的麻煩。

Next, there’s PaaS. This approach is also based on virtual machines,but you don’t have control over them. Instead, a set of tools and APIs is provided to let you package your application and upload it onto your virtual machine, so the only thing you have to worry about is your application. The networking, operating system, and so on are all maintained by the cloud platform.

接下來是PaaS。 這種方法也基于虛拟機,但是您無法控制它們。 相反,提供了一組工具和API,可讓您打包應用程式并将其上傳到虛拟機,是以您唯一需要擔心的就是您的應用程式。 網絡,作業系統等均由雲平台維護。

All cloud vendors share common features, including virtual machines, and storage that’s available through REST-based protocols. Then again, each offering has its own unique features, which is good: clouds are still in a very innovative phase, and as developers we have the luxury of choosing the platform that’s best suited to our particular applications.

所有雲供應商都共享通用功能,包括虛拟機和可通過基于REST的協定通路的存儲。 再者,每種産品都有其獨特的功能,這是件好事:雲仍處于非常創新的階段,作為開發人員,我們可以選擇最适合我們特定應用程式的平台。

Windows Azure平台概述 (Windows Azure Platform Overview)

Throughout this article, I’ll be describing the Windows Azure Platform, Microsoft’s PaaS offering to the world of cloud computing. But before we dive into technical details, let’s get a feel for the components included in this offering, and what they do.

在整篇文章中,我将描述Windows Azure平台,這是Microsoft向雲計算世界提供的PaaS産品。 但是,在深入探讨技術細節之前,讓我們先了解一下此産品中包含的元件及其作用。

Windows Azure (Windows Azure)

Windows Azure is the core component of the Windows Azure Platform. The marketing folks describe this component as the “operating system for the Azure cloud.” I’m not a big fan of marketing folks and their quotes, but for once, they’re right! Windows Azure is the heart of Microsoft’s offering, and it does what you’d expect of any operating system: it allows you to run your application on a virtual machine, either in a web role (with a web server installed) or in a worker role—a cleaner virtual machine that allows you to host other types of applications.

Windows Azure是Windows Azure平台的核心元件。 市場營銷人員将此元件描述為“ Azure雲的作業系統”。 我不是行銷人士及其報價的忠實擁護者,但有一次他們是對的! Windows Azure是Microsoft産品的核心,它可以完成任何作業系統的預期:它允許您在虛拟機上以Web角色(安裝了Web伺服器)或在工作程式中運作應用程式角色-一個更幹淨的虛拟機,允許您托管其他類型的應用程式。

Windows Azure also allows you to scale up rapidly: simply change a configuration value and you’ll have multiple instances running a the snap of your fingers. Load balancing is taken care of automatically and requires no configuration.

Windows Azure還允許您快速擴充:隻需更改配置值,您就可以輕松運作多個執行個體。 負載平衡是自動進行的,不需要任何配置。

Next to the operating system, a set of storage services is included, which is accessible through a REST-based API. Blob storage allows you to host any file: text files, images, downloads, and more.

在作業系統旁邊,包括一組存儲服務,可通過基于REST的API通路這些存儲服務。 通過Blob存儲,您可以托管任何檔案:文本檔案,圖像,下載下傳檔案等。

Table storage is, in essence, a document database that has limited querying possibilities but can scale massively. And then there are queues, which are mostly used for communications between web and worker roles.

從本質上說,表存儲是一個文檔資料庫,其查詢可能性有限,但可以大規模擴充。 然後是隊列,這些隊列主要用于Web和從業人員角色之間的通信。

Windows Azure is the location where your application will be hosted. A web role will host your web application; you’ll probably use blob storage to store files, and possibly table storage (or SQL Azure, which we’ll discuss in a moment) to store your data. Windows Azure is also used by other components of the platform.

Windows Azure是應用程式的托管位置。 網絡角色将托管您的網絡應用程式; 您可能會使用Blob存儲來存儲檔案,并可能使用表存儲(或我們稍後将讨論SQL Azure)來存儲資料。 平台的其他元件也使用Windows Azure。

SQL Azure (SQL Azure)

In addition to hosting, you will probably need a place where you can store your relational data. This is where SQL Azure comes in: it’s a slightly modified version of Microsoft SQL Server that delivers all the services you’d expect from a database: tables, views, indexes, stored procedures, triggers, and so on.

除了托管之外,您可能還需要一個可以存儲關系資料的地方。 這就是SQL Azure出現的地方:它是Microsoft SQL Server的略微修改版本,可提供您希望從資料庫獲得的所有服務:表,視圖,索引,存儲過程,觸發器等。

SQL Azure provides database services in a scalable and reliable way. Data is replicated across different sites and made available through a load balancer, giving you a lot of performance on the data layer of your application.

SQL Azure以可伸縮且可靠的方式提供資料庫服務。 資料跨不同站點複制,并通過負載平衡器提供,進而在應用程式的資料層上提供了很多性能。

Windows Azure平台AppFabric (Windows Azure Platform AppFabric)

Windows Azure Platform AppFabric is, in essence, a combination of two products. There’s an Access Control Service to which you can delegate the tasks of authentication and authorization of users, and there’s the Service Bus, which, in my opinion, is one of the features that really makes Windows Azure stand out. In short, the service bus allows you to establish communication between two endpoints. That might be a service that publishes messages to a set of subscribers, but the service bus can also be used for punching holes in firewalls!

Windows Azure平台AppFabric本質上是兩種産品的組合。 有一個通路控制服務,您可以委派使用者的身份驗證和授權任務,還有服務總線,在我看來,這是使Windows Azure脫穎而出的功能之一。 簡而言之,服務總線允許您在兩個端點之間建立通信。 那可能是一種将消息釋出給一組訂戶的服務,但是該服務總線也可以用于在防火牆上打Kong!

Imagine having applications A and B, each in different networks, behind different firewalls. No direct communication seems possible, yetthe AppFabric service bus will make sure both applications can communicate. There’s no need to open up ports in your company’s firewall to have your cloud application communicate with an on-premises application.

想象一下,将應用程式A和B分别位于不同的網絡中,位于不同的防火牆之後。 似乎無法進行直接通信,但是AppFabric服務總線将確定兩個應用程式都可以通信。 無需在公司的防火牆中打開端口即可讓您的雲應用程式與本地應用程式進行通信。

現場服務 (Live Services)

Live Services provides an online identity system that you probably already know: Windows Live ID. Live Services also offers features like presence awareness, search, mapping via Bing Maps, synchronization, and more.

Live Services提供了您可能已經知道的線上身份系統:Windows Live ID。 Live Services還提供諸如狀态感覺,搜尋,通過Bing Maps進行地圖繪制,同步等功能。

代号項目:達拉斯和悉尼 (Codename Projects: Dallas and Sydney)

These products are still in their incubation phases, and will probably undergo some changes in the future. Nevertheless, they already offer some great features. Dallas is basically a Data-as-a-Service solution through which you can subscribe to various sets of data offered in an open format, OData, which is based on REST and Atom. It also provides your business with a new source of revenue: if you’re sitting on a lot of useful data, why not make it available via Dallas and have others pay for using it?

這些産品仍處于孵化階段,将來可能會發生一些變化。 盡管如此,它們已經提供了一些很棒的功能。 達拉斯基本上是一種“資料即服務”解決方案,通過它您可以訂閱以REST和Atom為基礎的開放格式OData提供的各種資料集。 它還為您的企業提供了新的收入來源:如果您擁有大量有用的資料,為什麼不通過達拉斯提供這些資料,而讓其他人為此付費呢?

Project Sydney is different: it’s focused on how you communicate with your cloud application. Currently, that communication is completed through the public Internet, but Sydney will allow you to set up a VPN connection to your virtual machines, enabling you to secure communications using your own security certificates and such.

悉尼項目與衆不同:它專注于您與雲應用程式進行通信的方式。 目前,該通信已認證公共Internet完成,但是Sydney将允許您建立與虛拟機的VPN連接配接,進而使您能夠使用自己的安全證書等來保護通信。

适用于PHP的工具和API (Tools and APIs Available for PHP)

When we’re talking about using PHP in a cloud platform like Windows Azure, there are some objectives that we should fulfil before we start to work with the cloud. You’ll need the right tools to build and deploy your application, but also the right APIs—those that allow you to use the platform and all of its features.

當我們談論在Windows Azure這樣的雲平台中使用PHP時,在開始使用雲之前,我們應該實作一些目标。 您将需要正确的工具來建構和部署應用程式,還需要正确的API(使您能夠使用平台及其所有功能的API)。

Microsoft has been doing a lot of good work in this area. Yes, Windows Azure is a Windows-based platform that seems to target only .NET languages. However, when you look at the tools, tutorials, APIs, and blog posts around PHP and Windows Azure, it is clear that PHP is an equally valued citizen of the platform!

微軟在這方面做得很好。 是的,Windows Azure是一個基于Windows的平台,似乎隻針對.NET語言。 但是,當您檢視有關PHP和Windows Azure的工具,教程,API和部落格文章時,很明顯PHP是該平台的同等價值公民!

Let’s take a tour of all the tools and APIs that are available for PHP on Windows Azure today. A lot of these tools are very easy to install using the Web Platform Installer—a “check-next-finish” wizard that allows you to install platforms and tools in an easy and efficient manner.

讓我們浏覽一下當今Windows Azure上可用于PHP的所有工具和API。 使用Web Platform Installer可以很容易地安裝許多工具,這是一個“檢查-下一步完成”向導,可讓您以簡單有效的方式安裝平台和工具。

IDE支援 (IDE Support)

Of course, you can use your favorite editor to work on a PHP application that’ll be hosted on Windows Azure. On the other hand, if you’re using an Eclipse-based editor like Eclipse PDT, Zend Studio, or Aptana, you can take advantage of a great plugin that will speed up your development efforts, shown in Figure 2, “Using Eclipse for development”.

當然,您可以使用自己喜歡的編輯器來處理将在Windows Azure上托管PHP應用程式。 另一方面,如果您使用的是基于Eclipse的編輯器(例如Eclipse PDT,Zend Studio或Aptana),則可以利用功能強大的插件來加快開發工作,如圖2所示,“将Eclipse用于發展”。

The Eclipse plugin for Windows Azure is available at http://windowsazure4e.org. Also, Josh Holmes has prepared a handy post, Easy Setup for PHP on Azure Development.

Windows Azure的Eclipse插件可從http://windowsazure4e.org獲得 。 另外,Josh Holmes還準備了一個友善的文章, 《 Azure開發上PHP輕松安裝》 。

Using Eclipse for development

使用Eclipse進行開發

After installing the plugin, you’ll find the following features have been added to your IDE:

安裝插件後,您會發現以下功能已添加到您的IDE中:

  • Project Creation and Migration allows for the easy migration

    通過項目建立和遷移,可以輕松進行遷移

    of an existing application to a Windows Azure application. This

    現有應用程式遷移到Windows Azure應用程式。 這個

    tool will get your application ready for packaging and deployment

    工具将使您的應用程式準備好進行打包和部署

    to Windows Azure.

    Windows Azure。

  • Storage Explorer provides access to your Windows Azure

    Storage Explorer提供對Windows Azure的通路

    storage accounts and allows you to upload and download blobs,

    存儲帳戶,并允許您上傳和下載下傳Blob,

    query tables, list queues, and so on.

    查詢表,清單隊列等。

  • Debugging and local testing is also included: there’s no

    調試和本地測試也包括在内:

    need to deploy and test your application immediately on Windows

    需要立即在Windows上部署和測試您的應用程式

    Azure. A “local cloud” simulation environment is available.

    Azure。 可以使用“本地雲”模拟環境。

打包 (Packaging)

Once your application is ready for deployment, it should be packaged for Windows Azure. Packaging is basically the process of creating a ZIP archive of your application and embedding a manifest of all the included files and their configuration requirements.

準備好要部署的應用程式後,應将其打包為Windows Azure。 打包基本上是建立應用程式的ZIP存檔并嵌入所有包含的檔案及其配置要求的清單的過程。

The Eclipse plugin for Windows Azure contains this feature. However, if you don’t use Eclipse as your IDE, or if you’re working in a non-Windows environment, you can package your application using the Windows Azure command-line tools for PHP developers.

Windows Azure的Eclipse插件包含此功能。 但是,如果您不使用Eclipse作為IDE,或者在非Windows環境中工作,則可以使用适用于PHP開發人員的Windows Azure指令行工具打包應用程式。

開發工具和SDK (Development Tools and SDKs)

Next, let’s take a spin around some of the tools and SDKs that Windows Azure makes available to developers.

接下來,讓我們來看看Windows Azure提供給開發人員的一些工具和SDK。

适用于PHP的Windows Azure SDK (Windows Azure SDK for PHP)

If you’re planning on migrating an application or building a new one for Windows Azure, chances are that you’ll need storage. This is where the Windows Azure SDK for PHP comes in handy: it gives you easy access to the blob storage, table storage and queue services provided by Windows Azure. You can download this SDK as a stand-alone, open-source package that allows you to access storage from both on-premises locations and your cloud application. If you’re using the Eclipse plug-in we discussed earlier, you’ll find this API is included.

如果您打算遷移應用程式或為Windows Azure建構新的應用程式,則很可能需要存儲。 這是用于PHP的Windows Azure SDK派上用場的地方:它使您可以輕松通路Windows Azure提供的Blob存儲,表存儲和隊列服務。 您可以将此SDK作為獨立的開源程式包下載下傳,該程式包使您可以從本地位置和雲應用程式通路存儲。 如果您使用的是我們前面讨論的Eclipse插件,則會發現此API包含在内。

The process of utilizing storage always starts with setting up your credentials: an account name and a shared key (think of this as a very long password). Then, you can use one of the specific classes available for blob storage, table storage, or queue storage.

利用存儲空間的過程總是從設定憑據開始:一個帳戶名和一個共享密鑰(将其視為一個很長的密碼)。 然後,您可以使用可用于Blob存儲,表存儲或隊列存儲的特定類之一。

Here’s an example of blob storage in action. First, I create a container (think of this as a virtual hard drive). Then, I upload a file from my local hard drive to blob storage:

這是一個實際的Blob存儲示例。 首先,我建立一個容器(将其視為虛拟硬碟驅動器)。 然後,我将檔案從本地硬碟驅動器上傳到Blob存儲:

/** Microsoft_WindowsAzure_Storage_Blob */
require_once 'Microsoft/WindowsAzure/Storage/Blob.php';

$storageClient = new Microsoft_WindowsAzure_Storage_Blob();
$storageClient->createContainer('testcontainer');

// upload /home/maarten/example.txt to Windows Azure
$result = $storageClient->putBlob('testcontainer', 'example.txt', '/home/maarten/example.txt');
           

Reading the blob afterwards is fairly straightforward:

之後讀取blob非常簡單:

/** Microsoft_WindowsAzure_Storage_Blob */
require_once 'Microsoft/WindowsAzure/Storage/Blob.php';

$storageClient = new Microsoft_WindowsAzure_Storage_Blob();

// download file to /home/maarten/example.txt
$storageClient->getBlob('testcontainer', 'example.txt', '/home/maarten/example.txt');
           

Table storage is a bit more complex. It’s like a very scalable database that’s not bound to a schema, and has limited querying possibilities. To use table storage, you’ll require some classes that can be used both by your PHP application and Windows Azure table storage. Here’s an example class representing a person:

表存儲有點複雜。 它就像一個非常可伸縮的資料庫,沒有綁定到架構,并且查詢的可能性有限。 若要使用表存儲,您将需要一些可以由PHP應用程式和Windows Azure表存儲使用的類。 這是一個代表人的示例類:

class Person extends Microsoft_WindowsAzure_Storage_TableEntity
{
  /**
   * @azure Name
   */
  public $Name;

  /**
   * @azure Age Edm.Int64
   */
  public $Age;
}
           

Inserting an instance of

Person

into the table is as easy as creating a new instance and assigning it some properties. After that, the table storage API in the Windows Azure SDK for PHP allows you to insert the entity into a table named

testtable

:

Person

執行個體插入表中就像建立一個新執行個體并為其配置設定一些屬性一樣容易。 之後,用于PHP的Windows Azure SDK中的表存儲API允許您将實體插入名為

testtable

的表中:

/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';

$entity = new Person('partition1', 'row1');
$entity->Name = "Maarten";
$entity->Age = 25;

$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$storageClient->insertEntity('testtable', $entity);
           

That was a lot of information in one code snippet! First of all, what are partition1 and row1? Well, those are the partition key and row key. The partition key is a logical grouping of entities. In an application where users can contribute blog posts, for example, a good candidate for the partition key would be the username—this would allow you to easily query for all data related to a given user. The row key is the unique identifier for the row.

一個代碼段中包含了很多資訊! 首先, partition1和row1是什麼? 好吧,這些是分區鍵和行鍵。 分區鍵是實體的邏輯分組。 例如,在使用者可以發表部落格文章的應用程式中,分區鍵的最佳選擇就是使用者名-這将使您輕松查詢與給定使用者相關的所有資料。 行鍵是該行的唯一辨別符。

Queues follow the same idea—there’s an API that allows you to put, get, and delete messages from the queue on Windows Azure. Queues are also guaranteed to be processed: when a message is read from the queue, data is made invisible for a specific time. If, after that time, the message has not been explicitly removed, for example because a batch script has crashed, the message will re-appear and be available for processing again.

隊列遵循相同的想法-有一個API,您可以通過它在Windows Azure上從隊列中放入,擷取和删除消息。 還保證隊列得到處理:從隊列中讀取消息時,資料在特定時間内不可見。 如果在那之後,仍未明确删除該消息,例如由于批處理腳本崩潰,則該消息将重新出現并可以再次處理。

The Windows Azure SDK for PHP also has some extra features that are specific to both PHP and Windows Azure. This includes features like a session storage provider that allows you to share web session data over multiple web role instances. Another feature is a stream wrapper that allows you to use standard file functions like

fopen

on blob storage.

用于PHP的Windows Azure SDK還具有一些特定于PHP和Windows Azure的額外功能。 這包括會話存儲提供程式之類的功能,該功能使您可以在多個Web角色執行個體上共享Web會話資料。 流包裝器的另一個功能是允許您在blob存儲上使用标準檔案功能(例如

fopen

An example application, ImageCloud, which uses all the features described above, is available for download on my blog.

使用上述所有功能的示例應用程式ImageCloud可在我的部落格上下載下傳。

适用于PHPSQL Server驅動程式 (SQL Server Driver for PHP)

The SQL Server Driver for PHP allows PHP developers to access SQL Server databases that are hosted on SQL Server or SQL Azure. The SQL Server Driver for PHP relies on the Microsoft SQL Server ODBC Driver to handle low-level communication with SQL Server. As a result, the SQL Server Driver for PHP is only supported on Windows and Windows Azure. It can be downloaded and installed as a PHP extension.

PHPSQL Server驅動程式允許PHP開發人員通路SQL Server或SQL Azure上托管SQL Server資料庫。 用于PHPSQL Server驅動程式依賴于Microsoft SQL Server ODBC驅動程式來處理與SQL Server的低級通信。 是以,僅Windows和Windows Azure支援PHPSQL Server驅動程式。 可以下載下傳并作為PHP擴充安裝。

When you download this driver, be sure to download version 2.0. This version has the additional benefit that it provides PDO (PHP Data Objects) support, which allows you to quickly switch between, for example, MySQL and SQL Server.

當您下載下傳此驅動程式時,請確定下載下傳版本2.0。 此版本的另一個好處是,它提供了PDO(PHP資料對象)支援,使您可以在例如MySQL和SQL Server之間快速切換。

Now, let’s imagine you have an SQL Azure database. The following code shows how you can connect to the

blog

database on your SQL Azure database server and retrieve the posts ordered by publication date:

現在,讓我們假設您有一個SQL Azure資料庫。 以下代碼顯示如何連接配接到SQL Azure資料庫伺服器上的

blog

資料庫并檢索按釋出日期排序的文章:

// Connect to SQL Azure using PDO
$connection = new PDO('bvoj6aovnk.database.windows.net', '[email protected]', 'mypassword', array('Database' => 'blog'));

// Fetch specific post
$posts = array();
$query = 'SELECT * FROM posts ORDER BY PubDate DESC';
$statement = $connection->query($query);
while ( $row = $statement->fetchObject('Post') ) {
  $posts[] = $row;
}
           

适用于PHP的AppFabric SDK (AppFabric SDK for PHP)

As I mentioned before, the Windows Azure Platform AppFabric (not to be confused with the Windows Server AppFabric) enables you to delegate user authentication and authorization, and to punch firewalls and connect applications across different protected networks with ease. You can download it from http://dotnetservicesphp.codeplex.com.

如前所述,Windows Azure Platform AppFabric(不要與Windows Server AppFabric混淆)使您可以委派使用者身份驗證和授權,并輕松打通防火牆并跨不同的受保護網絡連接配接應用程式。 您可以從http://dotnetservicesphp.codeplex.com下載下傳。

In terms of authentication and authorization, it’s important to know a little about claims-based authentication and federation—a topic on which some interesting resources are available. Basically, your application establishes a trust relationship with an authentication authority (like Windows Azure Platform AppFabric), which means that your application trusts users that are authenticated with that authority. Next, your application will ask its users to claim their rights. For example, my application could ask the user to claim that they can create orders:

在身份驗證和授權方面,重要的一點是要了解基于聲明的身份驗證和聯合身份,這是一個可以擷取一些有趣資源的主題。 基本上,您的應用程式與身份驗證機構(例如Windows Azure Platform AppFabric)建立信任關系,這意味着您的應用程式信任使用該身份驗證的使用者。 接下來,您的應用程式将要求其使用者聲明其權利。 例如,我的應用程式可以要求使用者聲明可以建立訂單:

$requiredClaims = array('CreateOrder' => true);
if (ValidateClaimUtil::ValidateClaims($requiredClaims, "phpservice", 'http://localhost/SalesDashboard/', $signingKey))
{
  // User is allowed to create an order!
}
else
{
  // User is not authorized.
}
           

The Windows Azure Platform AppFabric Access Control Service will validate that the user has this claim, and sign a security token with that information. Since your application trusts this authority, it will either continue or fail on the basis of whether or not the claim is valid.

Windows Azure平台AppFabric通路控制服務将驗證使用者是否擁有此聲明,并使用該資訊簽名安全令牌。 由于您的應用程式信任此權限,是以它将根據聲明是否有效來繼續執行或失敗。

Now magine having two applications that cannot connect to each other because of firewall-related policies. If both applications can establish an outgoing connection to the service bus, the service bus will relay communication between the two applications. It’s as easy as that—and incredibly useful if you have a tough IT department!

現在想象一下,有兩個應用程式由于與防火牆相關的政策而無法互相連接配接。 如果兩個應用程式都可以建立到服務總線的傳出連接配接,則服務總線将中繼兩個應用程式之間的通信。 就這麼簡單-如果您的IT部門很艱巨,那就非常有用!

The benefits of Windows Azure Platform AppFabric Service Bus

Windows Azure平台AppFabric Service Bus的好處

Showing you example code of how this works would lead us too far (since it would involve some configuration and set up tasks). But if you think this sounds like a great feature, check the AppFabric for PHP website, which contains plenty of tutorials on this matter.

向您顯示此工作方式的示例代碼會使我們走得太遠(因為它将涉及一些配置和設定任務)。 但是,如果您認為這聽起來像是一個很棒的功能,請通路AppFabric for PHP網站 ,其中包含有關此問題的大量教程。

其他特性 (Other Features)

In addition all the features and APIs we’ve already investigated, there are a number of other features and products that are worth looking at. These features aren’t always Windows Azure-specific, like the URL rewriting module for IIS7, but your application can benefit greatly from them all the same.

除了我們已經研究過的所有功能和API外,還有許多其他功能和産品值得研究。 這些功能并不總是特定于Windows Azure的,例如IIS7的URL重寫子產品,但是您的應用程式可以從中受益匪淺。

PHP Azure貢獻 (PHP Azure Contributions)

The Windows Azure platform provides some useful features, like reading configuration files (which can be modified even after a deployment has been done), or logging into the Windows Azure environment and accessing local storage on a virtual machine to store files temporarily. Unfortunately, these features are baked in to the Windows Azure Cloud Guest OS, and not available as REST services.

Windows Azure平台提供了一些有用的功能,例如讀取配置檔案(即使在完成部署後也可以修改),或登入Windows Azure環境并通路虛拟機上的本地存儲以臨時存儲檔案。 不幸的是,這些功能已嵌入Windows Azure Cloud Guest OS中,不能作為REST服務使用。

Luckily however, these features are exposed as a C dynamic link library, which means that writing a PHP extension to interface with them is a logical step. And that’s exactly what the PHP Azure Contributions library provides: a PHP extension to make use of configuration data, logging, and local storage. Imagine having a configuration value named

EmailSubject

in your

ServiceConfiguration.csdef

file. Reading this value is very easy using the PHP Azure Contributions extension:

但是幸運的是,這些功能作為C動态連結庫公開,這意味着編寫PHP擴充以與它們進行接口是一個邏輯步驟。 這正是PHP Azure貢獻庫提供的功能:一個PHP擴充,用于使用配置資料,日志記錄和本地存儲。 想象一下,在

ServiceConfiguration.csdef

檔案中有一個名為

EmailSubject

的配置值。 使用PHP Azure貢獻擴充,讀取此值非常容易:

$$emailSubject = azure_getconfig("EmailSubject");
           

We can also write data to the Windows Azure diagnostics log. Here’s an example in which I’m writing an informational message in the diagnostics log:

我們還可以将資料寫入Windows Azure診斷日志。 這是一個示例,其中我在診斷日志中寫了一條參考消息:

azure_log("This is some useful information!", "Information");
           

The PHP Azure Contributions project is available on CodePlex athttp://phpazurecontrib.codeplex.com.

可通過CodePlex的http://phpazurecontrib.codeplex.com獲得PHP Azure貢獻項目。

URL重寫 (URL Rewriting)

As a PHP developer, you may already use URL rewriting. In Apache’s

.htaccess

files, it’s very easy to enable the rewrite engine, and to rewrite incoming URLs to real scripts. For example, the URL

http://www.example.com/products/books

may be mapped to

http://www.example.com/index.php?page=products&category=books

on your server. This technique is also available in IIS7, the Microsoft web server that’s also used in Windows Azure web roles. The above URL rewriting example can be defined in the

Web.config

file in the root of your Windows Azure application:

作為PHP開發人員,您可能已經在使用URL重寫。 在Apache的

.htaccess

檔案中,啟用重寫引擎并将傳入的URL重寫為真實腳本非常容易。 例如,URL

http://www.example.com/products/books

可能映射到伺服器上的

http://www.example.com/index.php?page=products&category=books

。 IIS7中也可以使用此技術,IIS 7是Windows Azure Web角色中也使用的Microsoft Web伺服器。 可以在Windows Azure應用程式根目錄中的

Web.config

檔案中定義以上URL重寫示例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RewriteProductsUrl" enabled="true" stopProcessing="true">
          <match url="^products/([^/]+)/?$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php?page=products&category={R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
           

Also note that, because your application is hosted on an IIS web server in Windows Azure, you can use any HttpModule for IIS, just as you would for a traditionally hosted application. This makes it easy to enable output compression, leverage the IIS authentication and authorization features, and more. Download the IIS URL Rewrite module from http://www.iis.net/download/urlrewrite.

還要注意,由于您的應用程式托管在Windows Azure中的IIS Web伺服器上,是以您可以對IIS使用任何HttpModule,就像對傳統托管的應用程式一樣。 這使得啟用輸出壓縮,利用IIS身份驗證和授權功能以及其他功能變得容易。 從http://www.iis.net/download/urlrewrite下載下傳IIS URL重寫子產品。

WinCache擴充 (WinCache Extension)

As you may know, PHP files are interpreted into bytecode and executed from that bytecode on every request. This process is quite fast, but on high-traffic websites, it’s recommended that we cache the bytecode and skip script interpretation. This technique increases a website’s performance without requiring additional resources.

您可能知道,PHP檔案被解釋為位元組碼,并在每次請求時從該位元組碼執行。 這個過程相當快,但是在人流量大的網站上,建議我們緩存位元組碼并跳過腳本解釋。 此技術無需其他資源即可提高網站的性能。

On Linux, accelerator modules that utilize these techniques, like APC and IonCube, are very common. These also work on Windows and could potentially also work on Windows Azure. However, Microsoft also released its own module that applies this technique: the WinCache extension for PHP. This extension is the fastest PHP accelerator on Windows, and also provides features like storing session data in this cache layer. The Wincache extension for PHP can be downloaded from http://www.iis.net/download/wincacheforphp.

在Linux上,利用這些技術的加速器子產品(例如APC和IonCube)非常常見。 這些也可以在Windows上運作,并且還可能在Windows Azure上運作。 但是,Microsoft還釋出了自己的應用此技術的子產品:WinCache PHP擴充。 該擴充是Windows上最快PHP加速器,還提供了一些功能,例如在此緩存層中存儲會話資料。 可以從http://www.iis.net/download/wincacheforphp下載下傳适用于PHP的Wincache擴充。

CDN –内容傳遞網絡 (CDN – Content Delivery Network)

When using Windows Azure blob storage, you’ll find that a full-featured content delivery network (CDN) is available as well. A CDN ensures that, for example, when a user downloads an image, that image will be retrieved from a storage server that’s close to that user’s client. This ensures that the download speed and latency are optimal, and the user receives the image very quickly.

使用Windows Azure Blob存儲時,您會發現還提供功能齊全的内容傳遞網絡(CDN)。 CDN確定例如當使用者下載下傳圖像時,将從與該使用者用戶端接近的存儲伺服器中檢索該圖像。 這樣可以確定最佳的下載下傳速度和延遲,并且使用者可以非常快速地接收圖像。

With blob storage, enabling the CDN is as easy as clicking a button. After that, your public containers are replicated to the CDN, which allows your site’s users to retrieve files and resources as swiftly as possible!

使用blob存儲,啟用CDN就像單擊按鈕一樣簡單。 之後,将您的公共容器複制到CDN,這使您站點的使用者可以盡快檢索檔案和資源!

Using the Windows Azure CDN

使用Windows Azure CDN

域名映射 (Domain Name Mapping)

With Windows Azure, your application will be assigned a domain name under the cloudapp.net domain—for example,

myphpapp.cloudapp.net

. I think you’ll agree that this isn’t the greatest URL. It gets even worse when you’re using blob storage for hosting files:

myphpappstorage.blob.core.windows.net

is, well, just plain ugly!

使用Windows Azure,将為您的應用程式配置設定cloudapp.net域下的域名,例如

myphpapp.cloudapp.net

。 我想您會同意這不是最大的URL。 當您使用Blob存儲托管檔案時,情況變得更糟:

myphpappstorage.blob.core.windows.net

真是醜陋!

Luckily, all URLs in Windows Azure can be mapped a custom domain name. So, to map

www.myphpapp.com

to

myphpapp.cloudapp.net

, you just need to add a CNAME record to your name server. The same applies to blob storage:

storage.myphpapp.com

can be mapped to the very long

myphpappstorage.blob.core.windows.net

through the addition of a CNAME record to your DNS server.

幸運的是,可以将Windows Azure中的所有URL映射為自定義域名。 是以,要将

www.myphpapp.com

映射到

myphpapp.cloudapp.net

,隻需要将CNAME記錄添加到名稱伺服器中。 這同樣适用于blob存儲:通過向DNS伺服器添加CNAME記錄,可以将

storage.myphpapp.com

映射到很長的

myphpappstorage.blob.core.windows.net

Check Azure team member Steve Marx’s post on custom domain names for more on this.

有關更多資訊,請檢視Azure團隊成員Steve Marx關于自定義域名的文章 。

結論 (Conclusion)

In this article, we’ve taken a snapshot of the Windows Azure platform from a PHP perspective. While I’m slightly biased by having contributed to the Windows Azure SDK for PHP, I do think that the Windows Azure platform is a great choice for hosting PHP applications in a highly-scalable cloud environment. I also feel that there’s great value to be found in features like the Windows Azure AppFabric Service Bus. The bottom line is: I believe that Microsoft is doing their best in making PHP a first-class citizen on their cloud platform.

在本文中,我們從PHP的角度拍攝了Windows Azure平台的快照。 盡管我為Windows Azure SDK for PHP做出了一些貢獻,但我确實認為Windows Azure平台是在高度可擴充的雲環境中托管PHP應用程式的絕佳選擇。 我還覺得在Windows Azure AppFabric Service Bus等功能中可以找到很大的價值。 底線是:我相信微軟正在盡最大努力使PHP成為其雲平台上的一流公民。

Another great resource related to PHP and Windows Azure is http://www.interoperabilitybridges.com, the website that lists all PHP and open-source projects from Microsoft.

與PHP和Windows Azure有關的另一個重要資源是http://www.interoperabilitybridges.com ,該網站列出了Microsoft的所有PHP和開源項目。

note: Microsoft: A SitePoint Business Partner This article has been made possible with the support of Microsoft. We work closely with Microsoft to help develop the content that’s most useful and relevant to you—our readers.

注意: Microsoft:SitePoint業務合作夥伴在Microsoft的支援下,使本文成為可能。 我們與Microsoft緊密合作,以幫助開發最有用且與您(我們的讀者)相關的内容。

翻譯自: https://www.sitepoint.com/host-php-in-the-cloud-with-windows-azure/