Azure 虚拟机部署实战:3种主流OS配置与80端口开放(附CLI命令) 📅 2026/7/12 12:06:43 Azure 虚拟机部署实战3种主流OS配置与80端口开放附CLI命令在云计算时代基础设施即服务(IaaS)已成为企业数字化转型的核心支柱。作为全球领先的云服务提供商Microsoft Azure凭借其强大的全球基础设施网络和丰富的服务矩阵为开发者提供了灵活高效的虚拟机部署能力。本文将深入探讨如何在Azure平台上快速部署Windows Server、Ubuntu和CentOS三种主流操作系统并完成关键的80端口配置帮助中级开发者和运维人员掌握自动化部署的核心技能。1. 环境准备与Azure CLI基础在开始部署前我们需要确保已安装Azure CLI并完成身份验证。Azure CLI是管理Azure资源的强大工具支持跨平台操作能够通过命令行实现所有管理功能。# 安装Azure CLI以Ubuntu为例 curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash # 登录Azure账户 az login # 设置默认订阅如有多个订阅 az account set --subscription 您的订阅ID提示使用az account list命令可查看所有可用订阅。建议在生产环境中使用服务主体(Service Principal)进行认证而非交互式登录。基础环境配置完成后我们需要创建资源组作为虚拟机的逻辑容器# 创建资源组 az group create --name MyResourceGroup --location eastus下表列出了常用的Azure区域及其编码区域名称区域编码地理位置美国东部eastus弗吉尼亚美国西部westus加利福尼亚北欧northeurope爱尔兰东南亚southeastasia新加坡2. Windows Server部署与配置Windows Server是企业环境中广泛使用的服务器操作系统Azure提供了多个版本的系统镜像。以下演示如何部署Windows Server 2022 Datacenter版本# 创建Windows Server虚拟机 az vm create \ --resource-group MyResourceGroup \ --name WinServerVM \ --image MicrosoftWindowsServer:WindowsServer:2022-datacenter-azure-edition:latest \ --admin-username azureuser \ --admin-password MySecurePassword!123 \ --size Standard_B2s \ --public-ip-sku Standard关键参数说明--image指定系统镜像格式为发布商:产品:SKU:版本--size定义虚拟机规格B系列适合中等负载--public-ip-sku标准SKU提供静态IP和区域冗余部署完成后我们需要开放80端口以支持Web服务# 开放80端口 az vm open-port \ --port 80 \ --resource-group MyResourceGroup \ --name WinServerVM # 安装IIS服务器通过自定义脚本扩展 az vm extension set \ --resource-group MyResourceGroup \ --vm-name WinServerVM \ --name CustomScriptExtension \ --publisher Microsoft.Compute \ --settings {fileUris:[https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-iis.ps1],commandToExecute:powershell -ExecutionPolicy Unrestricted -File configure-iis.ps1}验证部署成功的快捷方式是通过公共IP访问默认IIS页面# 获取公共IP地址 az vm show \ --resource-group MyResourceGroup \ --name WinServerVM \ --show-details \ --query [publicIps] \ --output tsv3. Ubuntu Server部署与Nginx配置Ubuntu Server以其轻量级和稳定性著称是运行Web应用的理想选择。以下是部署Ubuntu 20.04 LTS的完整流程# 创建Ubuntu虚拟机使用SSH密钥认证更安全 az vm create \ --resource-group MyResourceGroup \ --name UbuntuVM \ --image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \ --admin-username azureuser \ --generate-ssh-keys \ --size Standard_B1s \ --public-ip-sku Standard对于生产环境建议使用SSH密钥对而非密码认证# 生成SSH密钥对如果尚未生成 ssh-keygen -t rsa -b 2048 # 使用现有SSH密钥创建VM az vm create \ --resource-group MyResourceGroup \ --name UbuntuVM \ --image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \ --admin-username azureuser \ --ssh-key-values ~/.ssh/id_rsa.pub开放80端口并安装Nginx# 开放80端口 az vm open-port \ --port 80 \ --resource-group MyResourceGroup \ --name UbuntuVM # 通过自定义脚本安装Nginx az vm extension set \ --resource-group MyResourceGroup \ --vm-name UbuntuVM \ --name customScript \ --publisher Microsoft.Azure.Extensions \ --settings {fileUris:[https://raw.githubusercontent.com/Azure-Samples/azure-cli-samples/main/virtual-machine/nginx-ubuntu/install_nginx.sh],commandToExecute:bash install_nginx.sh}手动安装Nginx的详细步骤连接到虚拟机ssh azureuser公共IP执行安装命令sudo apt update sudo apt install -y nginx sudo systemctl enable nginx sudo systemctl start nginx自定义默认页面echo h1Hello from Azure Ubuntu VM/h1 | sudo tee /var/www/html/index.html4. CentOS部署与Apache配置CentOS以其企业级稳定性和长期支持受到欢迎。以下是CentOS 8的部署示例# 创建CentOS虚拟机 az vm create \ --resource-group MyResourceGroup \ --name CentOSVM \ --image OpenLogic:CentOS:8_5:latest \ --admin-username azureuser \ --admin-password MySecurePassword!123 \ --size Standard_B1s \ --public-ip-sku Standard开放端口并安装Apache# 开放80端口 az vm open-port \ --port 80 \ --resource-group MyResourceGroup \ --name CentOSVM # 安装Apache通过SSH执行 az vm run-command invoke \ --resource-group MyResourceGroup \ --name CentOSVM \ --command-id RunShellScript \ --scripts sudo yum install -y httpd sudo systemctl enable httpd sudo systemctl start httpd防火墙配置是CentOS部署中的关键环节# 配置防火墙通过SSH执行 az vm run-command invoke \ --resource-group MyResourceGroup \ --name CentOSVM \ --command-id RunShellScript \ --scripts sudo firewall-cmd --permanent --add-servicehttp sudo firewall-cmd --reload5. 三种系统配置对比与优化建议不同操作系统在Azure上的表现各有特点下表对比了关键配置差异特性Windows Server 2022Ubuntu 20.04 LTSCentOS 8默认Web服务器IISNginxApache包管理工具PowerShellaptyum/dnf防火墙管理高级安全防火墙ufw/iptablesfirewalld系统更新命令Install-WindowsUpdateapt update apt upgradeyum update推荐应用场景.NET应用、AD域Python/Docker企业级Java应用性能优化建议Windows Server启用加速网络可提升网络性能az network nic update \ --resource-group MyResourceGroup \ --name WinServerVMVMNic \ --accelerated-networking trueUbuntu使用Azure优化的内核sudo apt install -y linux-azureCentOS配置磁盘调度器为deadlineecho ACTIONadd|change, KERNELsd*[!0-9], ATTR{queue/scheduler}deadline | sudo tee /etc/udev/rules.d/60-scheduler.rules6. 自动化部署与脚本整合为提高效率我们可以将上述步骤整合为可重复使用的脚本。以下是完整的Bash脚本示例可一次性部署三种系统#!/bin/bash # 参数配置 RESOURCE_GROUPMultiOSDeployment LOCATIONeastus WIN_VM_NAMEWinServerVM UBUNTU_VM_NAMEUbuntuVM CENTOS_VM_NAMECentOSVM ADMIN_USERazureadmin ADMIN_PASSWORDSecurePassword123! # 创建资源组 az group create --name $RESOURCE_GROUP --location $LOCATION # 函数部署虚拟机并配置Web服务器 deploy_vm() { local vm_name$1 local image$2 local custom_script$3 echo 正在部署 $vm_name... az vm create \ --resource-group $RESOURCE_GROUP \ --name $vm_name \ --image $image \ --admin-username $ADMIN_USER \ --admin-password $ADMIN_PASSWORD \ --public-ip-sku Standard echo 开放80端口... az vm open-port \ --port 80 \ --resource-group $RESOURCE_GROUP \ --name $vm_name echo 配置Web服务器... az vm extension set \ --resource-group $RESOURCE_GROUP \ --vm-name $vm_name \ --name customScript \ --publisher Microsoft.Azure.Extensions \ --settings {\fileUris\:[\$custom_script\],\commandToExecute\:\bash $(basename $custom_script)\} } # 部署Windows Server deploy_vm $WIN_VM_NAME MicrosoftWindowsServer:WindowsServer:2022-datacenter-azure-edition:latest \ https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-iis.ps1 # 部署Ubuntu deploy_vm $UBUNTU_VM_NAME Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \ https://raw.githubusercontent.com/Azure-Samples/azure-cli-samples/main/virtual-machine/nginx-ubuntu/install_nginx.sh # 部署CentOS deploy_vm $CENTOS_VM_NAME OpenLogic:CentOS:8_5:latest \ https://raw.githubusercontent.com/Azure-Samples/azure-cli-samples/main/virtual-machine/apache-centos/install_apache.sh echo 部署完成7. 安全加固与监控配置基础部署完成后安全加固至关重要。以下是针对三种系统的通用安全建议网络隔离配置网络安全组(NSG)限制访问# 创建NSG规则仅允许HTTP和SSH az network nsg rule create \ --resource-group MyResourceGroup \ --nsg-name MyNSG \ --name AllowWebAndSSH \ --access Allow \ --protocol Tcp \ --direction Inbound \ --priority 100 \ --source-address-prefix Internet \ --source-port-range * \ --destination-address-prefix * \ --destination-port-ranges 80 22系统更新确保所有系统及时更新Windows:Install-WindowsUpdate -AcceptAll -AutoRebootUbuntu:sudo apt update sudo apt upgrade -yCentOS:sudo yum update -y日志监控启用Azure Monitor收集系统日志# 创建Log Analytics工作区 az monitor log-analytics workspace create \ --resource-group MyResourceGroup \ --workspace-name MyWorkspace \ --location eastus # 启用虚拟机监控 az vm extension set \ --resource-group MyResourceGroup \ --vm-name WinServerVM \ --name OmsAgentForLinux \ --publisher Microsoft.EnterpriseCloud.Monitoring \ --protected-settings {workspaceKey:您的密钥} \ --settings {workspaceId:您的工作区ID}8. 成本优化与资源管理合理控制云资源成本是运维的重要环节。Azure提供了多种成本优化工具自动关机非生产环境设置自动关机az vm auto-shutdown \ --resource-group MyResourceGroup \ --name WinServerVM \ --time 1900 \ --email your-emailexample.com规模调整根据负载动态调整虚拟机大小# 垂直扩展更改VM大小 az vm resize \ --resource-group MyResourceGroup \ --name UbuntuVM \ --size Standard_B2s # 水平扩展创建规模集 az vmss create \ --resource-group MyResourceGroup \ --name MyScaleSet \ --image UbuntuLTS \ --vm-sku Standard_B1s \ --instance-count 2 \ --autoscale-rules {metric:{name:Percentage CPU,timeGrain:PT1M},scale:{min:2,max:5,increase:{value:1,cooldown:PT5M},decrease:{value:1,cooldown:PT5M}}}预留实例长期使用可节省成本az reservations reservation order calculate \ --applied-scope-type Shared \ --billing-scope /subscriptions/您的订阅ID \ --instance-flexibility On \ --location eastus \ --reserved-resource-type virtualMachines \ --sku Standard_B1s \ --term P1Y \ --quantity 1在实际项目中根据流量模式选择合适的虚拟机规格至关重要。B系列适合突发工作负载D系列适合常规应用而F系列则优化了计算性能。