Menetapkan domain kustom ke aplikasi web menggunakan PowerShell

Contoh skrip ini membuat aplikasi web di App Service dengan sumber daya terkait, lalu memetakan www.<yourdomain> kepadanya.

Jika diperlukan, pasang Azure PowerShell menggunakan instruksi yang ditemukan di panduan Azure PowerShell, lalu jalankan Connect-AzAccount untuk membuat koneksi dengan Azure. Selain itu, Anda harus memiliki akses ke halaman konfigurasi DNS pencatat domain Anda.

Skrip sampel

Catatan

Kami menyarankan agar Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Lihat Menginstal Azure PowerShell untuk memulai. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

$fqdn="<Replace with your custom domain name>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"

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

# Create an App Service plan in Free tier.
New-AzAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName $webappname -Tier Free

# Create a web app.
$webapp = New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName $webappname

Write-Host "Sign in to your domain provider's website and configure the following records:"
Write-Host "A CNAME record that maps $fqdn to $webappname.azurewebsites.net"
Write-Host "A TXT record that maps asuid.$fqdn to the domain verification ID $($webapp.CustomDomainVerificationId)"
Read-Host "Press [Enter] key when ready ..."

# Before continuing, go to your DNS configuration UI for your custom domain and follow the 
# instructions at https://aka.ms/appservicecustomdns to configure a CNAME record for the 
# hostname "www" and point it your web app's default domain name.

# Upgrade App Service plan to Shared tier (minimum required by custom domains)
Set-AzAppServicePlan -Name $webappname -ResourceGroupName $webappname `
-Tier Shared

# Add a custom domain name to the web app. 
Set-AzWebApp -Name $webappname -ResourceGroupName $webappname `
-HostNames @($fqdn,"$webappname.azurewebsites.net")

Menghapus penyebaran

Setelah sampel skrip dijalankan, perintah berikut ini dapat digunakan untuk menghapus grup sumber daya, aplikasi web, dan semua sumber daya yang terkait.

Remove-AzResourceGroup -Name myResourceGroup -Force

Penjelasan skrip

Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
Baru-AzResourceGroup Membuat grup sumber daya tempat semua sumber daya disimpan.
New-AzAppServicePlan Buat paket App Service.
New-AzWebApp Membuat aplikasi web.
Set-AzAppServicePlan Memodifikasi paket App Service untuk mengubah tingkat harganya.
Set-AzWebApp Memodifikasi konfigurasi aplikasi web.

Langkah berikutnya

Untuk mengetahui informasi selengkapnya tentang modul Azure PowerShell, lihat dokumentasi Azure PowerShell.

Sampel Azure Powershell tambahan untuk Azure App Service Web Apps dapat ditemukan dalam sampel Azure PowerShell.