Table of Contents
建立Azure发布项目
在本地模拟环境进行测试
发布到Windows Azure
配置cache 
配置Session
访问Azure存储
Please upload a new picture
Home php教程 php手册 将PHP应用发布为Azure Cloud service

将PHP应用发布为Azure Cloud service

Jun 06, 2016 pm 08:01 PM
azure cloud php service release application

介绍了如何将PHP应用发布到Azure website上。Website使用很方便,不过由于Websites对于用户权限有很多限制,比如不能开端口(无法支持memcache)。有的时候,我们希望获得更多的灵活性,比如需要特别的PHP插件、需要不同的PHP版本,或者需要运行其他进程,甚至

介绍了如何将PHP应用发布到Azure website上。Website使用很方便,不过由于Websites对于用户权限有很多限制,比如不能开端口(无法支持memcache)。有的时候,我们希望获得更多的灵活性,比如需要特别的PHP插件、需要不同的PHP版本,或者需要运行其他进程,甚至希望登录到目标虚拟机上去。这时,可以采用Cloud service来运行PHP应用。Cloud service是一组被Azure管理的虚拟机,上面运行了windows操作系统,并且加载了IIS和PHP运行环境,可以支持ASP和PHP的运行。PHP发布到Website时,用FTP把代码上传即可。采用Cloud service时,上传过程稍微不同。上传时需要使用Azure工具进行打包。下面看下具体步骤。

建立Azure发布项目

该工具只能运行在Windows下。从这里下载安装Windows Azure SDK for PHP。该工具包括了一个本地的Azure模拟器和Power shell工具。安装完毕后,在开始菜单中找到Windows Azure PowerShell程序,以管理员身份运行

 接下来就可以进行发布了。首先要为该PHP应用生成一个对应的Cloud service应用

PS C:\>New-AzureServiceProject myProject
Copy after login

 该操作会在C:\下生成一个目录,包含了该服务的基本文件,如服务定义ServiceDefinition.csdef和服务配置信息ServiceConfiguration.Cloud.cscfg。他们定义了该服务由几个角色构成,每个角色有几个虚拟机,以及各种变量信息等。任何一个cloud service都由一个或者若干个角色(Role)构成。一个Role代表了功能相同的一组虚拟机。比如,一个PHP站点有前段Web页面和后端一个批处理程序构成,那么我们可以定义两个role,分别是WebRole和WorkerRole

 接下来可以生成一个角色

PS C:\myProject> Add-AzurePHPWebRole MyWebRole
Copy after login

 该操作会在当前目录下生成一个目录容纳该角色包含的文件,新生成时里面只有index.php和一个bin目录。其中index.php是显示当前PHP环境信息。bin目录是Azure进行系统配置所需的一些脚本,我们可以不用管它。我们可以把已有的PHP应用代码拷贝到角色目录(C:\myProject\MyWebRole)下

在本地模拟环境进行测试

 在上传应用到Azure前,我门可以在本地先测试。在本地启动模拟器

PS C:\MyProject>Start-AzureEmulator
Copy after login

可以看到下面的输出:

Creating local package...
Starting Emulator...
Role is running at http://127.0.0.1:81
Started
Copy after login

然后打开浏览器访问上面给出的地址即可进行测试。下面的命令可以结束调试

PS C:\MyProject> Stop-AzureEmulator
Copy after login

测试完成后,开始最终发布。

发布到Windows Azure

发布时我们要先和Azure订阅关联。执行如下命令:

PS C:\MyProject>Get-AzurePublishSettingsFile
Copy after login

该操作会打开一个浏览器,并提示登录Azure。输入登录信息后,系统会弹出一个下载对话框。把文件下载到本地。

 该文件是Azure订阅信息。下面导入该订阅信息:

PS C:\MyProject>Import-AzurePublishSettingsFile C:\Users\{MyAccount}\Downloads\****-credentials.publishsettings
Copy after login

 最后,执行发布命令。其中ServiceName是服务名,该名称将组成服务URL的前缀,Location是发布的数据中心

PS C:\MyProject> Publish-AzureServiceProject -ServiceName MyService -Location "East Asia"
Copy after login
Copy after login

 发布完成后,登录Azure管理门户,在Cloud service下可以找到新创建的服务。进入后可以查看其Dashboard。点击右侧的Site URL即可访问该服务

将PHP应用发布为Azure Cloud service

下面是结果
将PHP应用发布为Azure Cloud service

 

Publish-AzureServiceProject这个命令实际上完成了代码打包、上传、部署这几个步骤。有时我们不希望直接将代码部署,而是希望只打包,然后手动上传到Azure存储上,再从Azure界面上手动执行应用发布。此时,可以用这个命令进行代码的打包:

Save-AzureServiceProjectPackage

 执行后,一个名为cloud_package.cspkg的文件会在项目目录下生成。

配置cache 

网站服务运行PHP的一个问题是,没法添加memcache扩展模块。由于memcache服务的运行需要开启TCP端口,而这正是被网站服务所限制的。Cloud service运行PHP则不存在这样的问题,而且Cloud service本身支持Windows Azure Caching模块,该模块与Memcache功能类似,使用各虚拟机的内存运行缓存数据,但该模块的安装完全是自动在后台完成,只要在配置时启用Caching,Azure就会自动在Cloud service的每个虚拟机上启用该模块。Azure Caching提供与memcache客户端兼容的接口,PHP网页可以按调用Memcache的方式使用Azure Caching,不用做任何修改。

下面就看下怎么配置PHP使用Azure Caching. 这个功能本身不会产生任何费用。

1. 首先,要下载php_memcache.dll,这是memcache的客户端。该模块可以从http://downloads.php.net/pierre/下载。这里我们选择php_memcache-2.2.6-5.3-nts-vc9-x86.zip (2010-10-03 13:46 -0700) - 44K 这个模块

2.下载后,将其解压,把php_memcache.dll放到Role的bin\php\ext目录下,本例中具体地址是C:\MyProject\MyWebRole\bin\php\ext

3.在C:\MyProject\MyWebRole\bin\php下增加一个php.ini文件,内容如下:

extension=php_memcache.dll

4.用记事本打开C:\MyProject\ServiceDefinition.csdef进行编辑

4.1.在内增加Caching模块定义

<import modulename="Caching"></import>
Copy after login

4.2.增加缓存所用的磁盘空间定义

<localresources> 
    <localstorage cleanonrolerecycle="false" name="Microsoft.WindowsAzure.Plugins.Caching.FileStore" sizeinmb="1000"></localstorage> 
 </localresources>
Copy after login

4.3在内增加端口定义

<internalendpoint name="memcache_default" protocol="tcp" port="11211"></internalendpoint>
Copy after login

5.编辑完成后,保存。完整内容如下



  
    
      <import modulename="Caching"></import>
    
    
      
        
          
            
          
          
          
          
        
      
    
    
      
    
    
      
      
    
    
      
        
          
        
      
    
  
Copy after login

6.编辑C:\myProject\ServiceConfiguration.Cloud.cscfg文件,在内增加如下有关Azure Caching的配置信息

      <setting name="Microsoft.WindowsAzure.Plugins.Caching.NamedCaches" value=""></setting>
      <setting name="Microsoft.WindowsAzure.Plugins.Caching.DiagnosticLevel" value="1"></setting>
      <setting name="Microsoft.WindowsAzure.Plugins.Caching.CacheSizePercentage" value="30"></setting>
      <setting name="Microsoft.WindowsAzure.Plugins.Caching.ConfigStoreConnectionString" value="DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=xxxxx"></setting>
Copy after login

其中,ConfigStoreConnectionString后面的AccountName和AccountKey需要自行更改,需要填入一个存储账户的信息

编辑完成后,结果如下


      <setting name="Microsoft.WindowsAzure.Plugins.Caching.NamedCaches" value=""></setting>
      <setting name="Microsoft.WindowsAzure.Plugins.Caching.DiagnosticLevel" value="1"></setting>
      <setting name="Microsoft.WindowsAzure.Plugins.Caching.CacheSizePercentage" value="30"></setting>
      <setting name="Microsoft.WindowsAzure.Plugins.Caching.ConfigStoreConnectionString" value="DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=xxxxx"></setting>

Copy after login

7.接下来,修改php中对cache的访问入口,将其改为如下的形式,其中MyWebRole是Role的名字

$memcache = new Memcache;
$memcache->connect('localhost_MyWebRole', 11211) or die ("Could not connect");
Copy after login

到此为止,PHP使用memcache客户端访问缓存的配置就完成了。下面我们可以用一个小程序测试下。

在C:\myProject\MyWebRole\下创建一个cachetest.php,内容如下:

<?php $memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."<br>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br>\n";

$get_result = $memcache->get('key');
echo "Data from the cache:<br>\n";

var_dump($get_result);

?> 
Copy after login


下面,重新发布该项目

PS C:\MyProject> Publish-AzureServiceProject -ServiceName MyService -Location "East Asia"
Copy after login
Copy after login


发布完成后,访问http://myservice.cloudapp.nen/cachetest.php,如果能看到如下结果,说明Cache已经配置成功

将PHP应用发布为Azure Cloud service


配置Session

Azure云服务的一个局限是不支持应用层负载均衡,即,在负载均衡用户请求时,不去判断请求中的session信息,这样,一个用户的不同请求可能会发送到不同的后台虚拟机实例上,造成session失效。为了解决这个问题,要么不用session(数据存在cache里面),要么需要实现session复制,即在每个实例上都可以访问同样的session数据。PHP原生支持利用memcache提供session复制。在Windows Azure上,我们在上一步已经实现了对memcache的兼容,因此可以基于现有的PHP session复制方法,让云服务中的PHP支持session。具体配置方法为:修改角色目录下的bin\php\php.ini,在末尾加上下面的内容:

session.save_handler = memcache
session.save_path = "tcp://localhost:11211"

当然,事先要按照上一节的方法配置cache


访问Azure存储

在PHP中,我们可以通过Azure PHP SDK去操作Azure的多种服务,包括存储、服务总线等。SDK的安装方法,是下载相应的软件包到云项目相应角色的目录中。具体方法可参考https://github.com/windowsazure/azure-sdk-for-php


安装之后,就可以在PHP代码中引用类库进行操作了。下面的例子是在PHP中上传图片并且列表展示

<?php require_once 'vendor\autoload.php';

use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Common\CloudConfigurationManager;


$connectionString = "$YOURSTRORAGEACCOUNT"

// Create blob REST proxy.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);

$createContainerOptions = new CreateContainerOptions(); 
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);

try {
    // Create container.
    $blobRestProxy->createContainer("imagecontainer", $createContainerOptions);
}
catch(ServiceException $e){
    // Handle exception based on error codes and messages.
    // Error codes and messages are here: 
    // http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
    $code = $e->getCode();
	if($code != 409) {
	   $error_message = $e->getMessage();
       echo $code.": ".$error_message."<br>";		
	}    
}

// Insert any new image into storage

if (isset($_REQUEST['completed']) and $_REQUEST['completed'] == 1) {
        $instr = fopen($_FILES['imagefile']['tmp_name'],"rb");
		$blob_name = $_FILES['imagefile']['name'];
		try {
		    //Upload blob
			$blobRestProxy->createBlockBlob("imagecontainer", $blob_name, $instr);			
		}
		catch(ServiceException $e){
			$code = $e->getCode();
			$error_message = $e->getMessage();
			echo $code.": ".$error_message."<br>";
	    }		
        
        fclose($instr);
}

try {
    // List blobs.
    $blob_list = $blobRestProxy->listBlobs("imagecontainer");
    $blobs = $blob_list->getBlobs();

    foreach($blobs as $blob)
    {
        echo $blob->getName()."<br>";
		echo "<img  src="%22.%24blob->getUrl().%22" alt="将PHP应用发布为Azure Cloud service" ><p>";
    }
} catch(ServiceException $e){
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br>";
}

?>


 </p>
    <style type="text/css">
    h2 div{ 
	   width: 100%; 
	   margin: 0 auto;
	   background-color:#0d2d80;
	   color:white;
	   }
	h3 div{ 
	   width: 100%; 
	   margin: 0 auto;
	   background-color:#e6c671;
	   color:black;
	   }
    </style>  
    <title>Upload an image to a storage</title>
 
 
   <h2>
      <div>Upload an image to a storage</div>
   </h2>
 <hr>
   <h2 id="Please-upload-a-new-picture">Please upload a new picture </h2>
   
Copy after login
Please choose an image to upload:




 

此外我们还可以对该Cloudservice进行管理,比如扩容、监控、修改配置等,可以参考http://www.windowsazure.com/en-us/manage/services/cloud-services/

这里并没有讨论如何发布数据库,可以参考http://blog.csdn.net/shaunfang/article/details/8555574

 

目前缺省的PHP版本是5.3,如果我们希望变更PHP版本,或者自定义PHP运行环境,可以参考http://www.windowsazure.com/en-us/develop/php/common-tasks/create-web-and-worker-roles/#CreateProject

而更多Powershell命令,可以参考http://www.windowsazure.com/en-us/develop/php/how-to-guides/powershell-cmdlets/#ImportPubSettings

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles