Mengikat sertifikat TLS/SSL kustom ke aplikasi web menggunakan PowerShell

Skrip sampel ini membuat aplikasi web di Azure App Service dengan sumber daya terkait, lalu mengikat sertifikat TLS/SSL dari nama domain kustom ke dalam aplikasi.

Jika diperlukan, pasang Azure PowerShell menggunakan instruksi yang ditemukan di panduan Azure PowerShell, lalu jalankan Connect-AzAccount untuk membuat koneksi dengan Azure. Selain itu, pastikan bahwa:

  • Koneksi dengan Azure telah dibuat menggunakan perintah az login.
  • Anda memiliki akses ke halaman konfigurasi DNS pencatat domain Anda.
  • Anda memiliki file .PFX valid dan kata sandinya untuk sertifikat TLS/SSL yang Anda ingin unggah dan ikat.

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>"
$pfxPath="<Replace with path to your .PFX file>"
$pfxPassword="<Replace with your .PFX password>"
$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 Basic tier (minimum required by custom SSL certificates)
Set-AzAppServicePlan -Name $webappname -ResourceGroupName $webappname `
-Tier Basic

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

# Upload and bind the SSL certificate to the web app.
New-AzWebAppSSLBinding -WebAppName $webappname -ResourceGroupName $webappname -Name $fqdn `
-CertificateFilePath $pfxPath -CertificatePassword $pfxPassword -SslState SniEnabled

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.
New-AzWebAppSSLBinding Membuat pengikatan sertifikat TLS/SSL untuk aplikasi web.

Langkah berikutnya

Untuk 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.