使用 PowerShell 创建 WordPress VMCreate a WordPress VM with PowerShell

此脚本创建一个虚拟机,并使用 Azure 虚拟机自定义脚本扩展安装 WordPress。This script creates a virtual machine and uses the Azure Virtual Machine custom script extension to install WordPress. 运行脚本后,可在 http://<public IP of VM>/wordpress 访问 WordPress 配置站点。After running the script, you can access the WordPress configuration site at http://<public IP of VM>/wordpress.

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell 模块,然后运行 Connect-AzAccount -Environment AzureChinaCloud 创建与 Azure 的连接。If needed, install the Azure PowerShell module using the instructions found in the Azure PowerShell guide, and then run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure. 此外,用户配置文件的 .ssh 目录中需具备名为 id_rsa.pub 的 SSH 公钥。Also, you need to have an SSH public key named id_rsa.pub in the .ssh directory of your user profile.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

示例脚本Sample script

# Sign-in the Azure China Cloud
Connect-AzAccount -Environment AzureChinaCloud

# Variables for common values
$resourceGroup = "myResourceGroup"
$location = "chinanorth"
$vmName = "myVM"

# Definer user name and blank password
$securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("azureuser", $securePassword)

# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $location

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
  -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
  -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4

# Create an inbound network security group rule for port 22
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleSSH  -Protocol Tcp `
  -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
  -DestinationPortRange 22 -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleHTTP = New-AzNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleHTTP  -Protocol Tcp `
  -Direction Inbound -Priority 2000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
  -DestinationPortRange 80 -Access Allow

# Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
  -Name myNetworkSecurityGroup -SecurityRules $nsgRuleSSH,$nsgRuleHTTP

# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzNetworkInterface -Name myNic -ResourceGroupName $resourceGroup -Location $location `
  -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration
$vmConfig = New-AzVMConfig -VMName $vmname -VMSize Standard_D1 | `
Set-AzVMOperatingSystem -Linux -ComputerName $vmName -Credential $cred -DisablePasswordAuthentication | `
Set-AzVMSourceImage -PublisherName Canonical -Offer UbuntuServer -Skus 14.04.2-LTS -Version latest | `
Add-AzVMNetworkInterface -Id $nic.Id

# Configure SSH Keys
$sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub"
Add-AzVMSshPublicKey -VM $vmconfig -KeyData $sshPublicKey -Path "/home/azureuser/.ssh/authorized_keys"

# Create a virtual machine
New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

# Start a CustomScript extension to use a simple bash script to update, download and install WordPress and MySQL 
$PublicSettings = '{"fileUris":["https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/wordpress-single-vm-ubuntu/install_wordpress.sh"],"commandToExecute":"sh install_wordpress.sh"}'

Set-AzVMExtension -Name "WordPress" -ResourceGroupName $resourceGroup -VMName $vmName `
  -Publisher "Microsoft.Azure.Extensions" -ExtensionType "CustomScript" -TypeHandlerVersion 2.0 `
  -SettingString $PublicSettings `
  -Location $location

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建部署。This script uses the following commands to create the deployment. 表中的每一项均链接到特定于命令的文档。Each item in the table links to command specific documentation.

命令Command 注释Notes
New-AzResourceGroupNew-AzResourceGroup 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
New-AzVirtualNetworkSubnetConfigNew-AzVirtualNetworkSubnetConfig 创建子网配置。Creates a subnet configuration. 在虚拟网络创建过程中会使用此配置。This configuration is used with the virtual network creation process.
New-AzVirtualNetworkNew-AzVirtualNetwork 创建虚拟网络。Creates a virtual network.
New-AzPublicIpAddressNew-AzPublicIpAddress 创建公共 IP 地址。Creates a public IP address.
New-AzNetworkSecurityRuleConfigNew-AzNetworkSecurityRuleConfig 创建网络安全组规则配置。Creates a network security group rule configuration. 创建 NSG 时会使用此配置创建 NSG 规则。This configuration is used to create an NSG rule when the NSG is created.
New-AzNetworkSecurityGroupNew-AzNetworkSecurityGroup 创建网络安全组。Creates a network security group.
Get-AzVirtualNetworkSubnetConfigGet-AzVirtualNetworkSubnetConfig 获取子网信息。Gets subnet information. 创建网络接口时会使用此信息。This information is used when creating a network interface.
New-AzNetworkInterfaceNew-AzNetworkInterface 创建网络接口。Creates a network interface.
New-AzVMConfigNew-AzVMConfig 创建 VM 配置。Creates a VM configuration. 此配置包括 VM 名称、操作系统和管理凭据等信息。This configuration includes information such as VM name, operating system, and administrative credentials. 在创建 VM 期间使用此配置。The configuration is used during VM creation.
New-AzVMNew-AzVM 创建虚拟机。Create a virtual machine.
Set-AzVMExtensionSet-AzVMExtension 将自定义脚本扩展添加到虚拟机,此扩展将调用脚本来安装 WordPress。Add the Custom Script Extension to the virtual machine, which invokes a script to install WordPress.
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组及其中包含的所有资源。Removes a resource group and all resources contained within.

后续步骤Next steps

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 PowerShell 脚本示例。Additional virtual machine PowerShell script samples can be found in the Azure Linux VM documentation.