Deploy ASP.NET lên Azure Virtual Machine

Phi Huynh
Azure Vietnam
Published in
3 min readAug 16, 2018

Azure Virtual Machine đã quá quen thuộc với anh em, với Azure có thể tạo ra rất nhiều loại Virtual Machine (VM) khác nhau, có thể là máy Windows (Windows 10, Windows Server, SQL Server, Visual Studio, …), máy Linux (Redhat, Ubuntu, CentOS, …). Tùy theo nhu cầu chúng ta sẽ tạo ra các VM khác nhau, với kích cỡ khác nhau để phù hợp với túi tiền.

Có một điều khá thú vị là tôi nhận được nhiều câu hỏi là làm thế nào để deploy ứng dụng ASP.NET lên một VM Windows Server trên Azure? Câu hỏi tưởng chừng nghe đơn giản nhưng nhiều anh em gặp khó khăn vì từ Visual Studio không thể publish trực tiếp lên VM trên Azure nếu không cấu hình đầy đủ. Và sau đây là một số tips để fix issue này.

Vấn đề:

Giả sử chúng ta đã tạo ra một VM Windows Server 2016 Data Center, mặc định thì VM này sẽ không có:

  • Cài đặt IIS (bao gồm cài đặt ASP.NET 4.6)
  • Web Management Service
  • Web Deploy 3.6
  • Cấu hình firewall (cả Windows Server và Azure Network Security Group) cho port 8172/443/80.

Giải pháp:

Có nhiều cách hoặc dùng command line, hoặc thao tác trên UI, bài này sẽ hướng dẫn dùng command-line. Remote vào máy VM trên Azure, mở PowerShell với quyền admin và thực thi đoạn lệnh sau:

# Cài đặt IIS (with Management Console)
Install-WindowsFeature -name Web-Server -IncludeManagementTools

# Cài đặt ASP.NET 4.6
Install-WindowsFeature Web-Asp-Net45

# Cài đặt Web Management Service
Install-WindowsFeature -Name Web-Mgmt-Service

# Cài đặt Web Deploy 3.6
# Download file from Microsoft Downloads and save to local temp file (%LocalAppData%/Temp/2)
$msiFile = [System.IO.Path]::GetTempFileName() | Rename-Item -NewName { $_ -replace 'tmp$', 'msi' } -PassThru
Invoke-WebRequest -Uri http://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy_amd64_en-US.msi -OutFile $msiFile
# Tạo log file
$logFile = [System.IO.Path]::GetTempFileName()
# Tạo tham số
$arguments= '/i ' + $msiFile + ' ADDLOCAL=ALL /qn /norestart LicenseAccepted="0" /lv ' + $logFile
# Thực thi MSI và chờ đến khi hoàn tất
$proc = (Start-Process -file msiexec -arg $arguments -Passthru)
$proc | Wait-Process
Get-Content $logFile

Và cuối cùng là vào Network Security Group của VM và cho phép các port 8172 / 443/80 để cho phép Web Deploy, và website có thể truy cập được.

Sau đó, từ Visual Studio, chúng ta đã có thể publish lên Azure VM một cách dễ dàng bằng cách nhấn chuột phải vào Solution, chọn Publish, chọn Azure Virutal Machine và chọn VM để deploy.

Chúc bạn thành công.

Tham khảo thêm ở đây:

https://github.com/aspnet/Tooling/blob/AspNetVMs/docs/create-asp-net-vm-with-webdeploy.md

--

--