此腳本會建立 Azure 虛擬機,然後使用 Azure 虛擬機自定義腳本擴充功能來安裝 NGINX。 執行腳本之後,您可以在虛擬機的公用IP位址上存取示範網站。
如有需要,請使用 Azure PowerShell 指南中找到的指示來安裝 Azure PowerShell 模組,,然後執行 Connect-AzAccount
以建立與 Azure 的連線。 此外,您必須在使用者配置檔的 .ssh 目錄中有名為 id_rsa.pub
的 SSH 公鑰。
如果您沒有 Azure 訂用帳戶,請在開始之前,先建立 Azure 免費帳戶。
範例腳本
# Variables for common values
$resourceGroup = "myResourceGroup"
$location = "westeurope"
$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
# Install NGINX.
$PublicSettings = '{"commandToExecute":"apt-get -y update && apt-get -y install nginx"}'
Set-AzVMExtension -ExtensionName "NGINX" -ResourceGroupName $resourceGroup -VMName $vmName `
-Publisher "Microsoft.Azure.Extensions" -ExtensionType "CustomScript" -TypeHandlerVersion 2.0 `
-SettingString $PublicSettings -Location $location
整理部署
執行下列命令來移除資源群組、VM 和所有相關資源。
Remove-AzResourceGroup -Name myResourceGroup
腳本說明
此腳本會使用下列命令來建立部署。 表格中的每個項目都會連結到與該命令相關的文件。
指令 | 備註 |
---|---|
New-AzResourceGroup | 建立用來存放所有資源的資源群組。 |
New-AzVirtualNetworkSubnetConfig | 建立子網組態。 此組態會與虛擬網路建立程式搭配使用。 |
New-AzVirtualNetwork | 建立虛擬網路。 |
New-AzPublicIpAddress | 建立公用IP位址。 |
New-AzNetworkSecurityRuleConfig | 建立網路安全組規則設定。 建立 NSG 時,此設定可用來建立 NSG 規則。 |
New-AzNetworkSecurityGroup | 建立網路安全組。 |
Get-AzVirtualNetworkSubnetConfig | 取得子網資訊。 建立網路介面時會使用這項資訊。 |
New-AzNetworkInterface | 建立網路介面。 |
New-AzVMConfig | 建立 VM 組態。 此設定包含 VM 名稱、作系統和系統管理認證等資訊。 設定會在 VM 建立期間使用。 |
New-AzVM | 建立虛擬機。 |
Set-AzVMExtension | 將 VM 擴充功能新增至虛擬機。 在此範例中,會使用自定義腳本擴充功能來安裝 NGINX。 |
Remove-AzResourceGroup | 移除資源群組及其內含的所有資源。 |
後續步驟
如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件。
如需其他虛擬機 PowerShell 腳本範例,請參閱 Azure Linux VM 檔案。