Tutorial: Secure a Windows web server with TLS certificates in Azure - Azure Virtual Machines (2024)

  • Article

Applies to: ✔️ Windows VMs ✔️ Flexible scale sets

Note

Currently, this doc only works for Generalized images. If you attempt this tutorial by using a Specialized disk you will receive an error.

To secure web servers, a Transport Layer Security (TLS) certificate can be used to encrypt web traffic. TLS certificates can be stored in Azure Key Vault and allow secure deployments of certificates to Windows virtual machines (VMs) in Azure. In this tutorial you learn how to:

  • Create an Azure Key Vault.
  • Generate or upload a certificate to the Key Vault.
  • Create a VM and install the IIS web server.
  • Inject the certificate into the VM and configure IIS with a TLS binding.

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Open Cloudshell from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com/powershell. Select Copy to copy the blocks of code, paste them into the Cloud Shell, and press enter to run them.

Overview

Azure Key Vault safeguards cryptographic keys and secrets, such as certificates or passwords. Key Vault helps streamline the certificate management process and enables you to maintain control of keys that access those certificates. You can create a self-signed certificate inside Key Vault, or you can upload an existing, trusted certificate that you already own.

Rather than by using a custom VM image that includes certificates baked-in, inject certificates into a running VM. This process ensures that the most up-to-date certificates are installed on a web server during deployment. If you renew or replace a certificate, you don't also have to create a new custom VM image. The latest certificates are automatically injected as you create more VMs. During the whole process, the certificates never leave the Azure platform or are exposed in a script, command-line history, or template.

Create an Azure Key Vault

Before you can create a Key Vault and certificates, create a resource group with New-AzResourceGroup. The following example creates a resource group named myResourceGroupSecureWeb in the East US location:

$resourceGroup = "myResourceGroupSecureWeb"$location = "East US"New-AzResourceGroup -ResourceGroupName $resourceGroup -Location $location

Next, create a Key Vault with New-AzKeyVault. Each Key Vault requires a unique name and should be all lower case. Replace mykeyvault with your own unique Key Vault name in the following example:

$keyvaultName="mykeyvault"New-AzKeyVault -VaultName $keyvaultName ` -ResourceGroup $resourceGroup ` -Location $location ` -EnabledForDeployment

Generate a certificate and store it in Key Vault

For production use, you should import a valid certificate signed by a trusted provider with Import-AzKeyVaultCertificate. For this tutorial, the following example shows how you can generate a self-signed certificate with Add-AzKeyVaultCertificate that uses the default certificate policy from New-AzKeyVaultCertificatePolicy.

$policy = New-AzKeyVaultCertificatePolicy ` -SubjectName "CN=www.contoso.com" ` -SecretContentType "application/x-pkcs12" ` -IssuerName Self ` -ValidityInMonths 12Add-AzKeyVaultCertificate ` -VaultName $keyvaultName ` -Name "mycert" ` -CertificatePolicy $policy 

Create a virtual machine

Set an administrator username and password for the VM with Get-Credential:

$cred = Get-Credential

Now you can create the VM with New-AzVM. The following example creates a VM named myVM in the EastUS location. If they don't already exist, the supporting network resources are created. To allow secure web traffic, the cmdlet also opens port 443.

# Create a VMNew-AzVm ` -ResourceGroupName $resourceGroup ` -Name "myVM" ` -Location $location ` -VirtualNetworkName "myVnet" ` -SubnetName "mySubnet" ` -SecurityGroupName "myNetworkSecurityGroup" ` -PublicIpAddressName "myPublicIpAddress" ` -Credential $cred ` -OpenPorts 443# Use the Custom Script Extension to install IISSet-AzVMExtension -ResourceGroupName $resourceGroup ` -ExtensionName "IIS" ` -VMName "myVM" ` -Location $location ` -Publisher "Microsoft.Compute" ` -ExtensionType "CustomScriptExtension" ` -TypeHandlerVersion 1.8 ` -SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server -IncludeManagementTools"}'

It takes a few minutes for the VM to be created. The last step uses the Azure Custom Script Extension to install the IIS web server with Set-AzVmExtension.

Add a certificate to VM from Key Vault

To add the certificate from Key Vault to a VM, obtain the ID of your certificate with Get-AzKeyVaultSecret. Add the certificate to the VM with Add-AzVMSecret:

$certURL=(Get-AzKeyVaultSecret -VaultName $keyvaultName -Name "mycert").id$vm=Get-AzVM -ResourceGroupName $resourceGroup -Name "myVM"$vaultId=(Get-AzKeyVault -ResourceGroupName $resourceGroup -VaultName $keyVaultName).ResourceId$vm = Add-AzVMSecret -VM $vm -SourceVaultId $vaultId -CertificateStore "My" -CertificateUrl $certURL | Update-AzVM

Configure IIS to use the certificate

Use the Custom Script Extension again with Set-AzVMExtension to update the IIS configuration. This update applies the certificate injected from Key Vault to IIS and configures the web binding:

$publicSettings = '{ "fileUris":["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/secure-iis.ps1"], "commandToExecute":"powershell -ExecutionPolicy Unrestricted -File secure-iis.ps1"}'Set-AzVMExtension -ResourceGroupName $resourceGroup ` -ExtensionName "IIS" ` -VMName "myVM" ` -Location $location ` -Publisher "Microsoft.Compute" ` -ExtensionType "CustomScriptExtension" ` -TypeHandlerVersion 1.8 ` -SettingString $publicSettings

Test the secure web app

Obtain the public IP address of your VM with Get-AzPublicIPAddress. The following example obtains the IP address for myPublicIP created earlier:

Get-AzPublicIPAddress -ResourceGroupName $resourceGroup -Name "myPublicIPAddress" | select "IpAddress"

Now you can open a web browser and enter https://<myPublicIP> in the address bar. To accept the security warning if you used a self-signed certificate, select Details and then Go on to the webpage:

Tutorial: Secure a Windows web server with TLS certificates in Azure - Azure Virtual Machines (1)

Your secured IIS website is then displayed as in the following example:

Tutorial: Secure a Windows web server with TLS certificates in Azure - Azure Virtual Machines (2)

Next steps

In this tutorial, you secured an IIS web server with a TLS certificate stored in Azure Key Vault. You learned how to:

  • Create an Azure Key Vault.
  • Generate or upload a certificate to the Key Vault.
  • Create a VM and install the IIS web server.
  • Inject the certificate into the VM and configure IIS with a TLS binding.

For prebuilt virtual machine script samples, see:

Tutorial: Secure a Windows web server with TLS certificates in Azure - Azure Virtual Machines (2024)

FAQs

How to enable TLS on Azure VM? ›

Navigate to your storage account in the Azure portal. Under Settings, select Configuration. Under Minimum TLS version, use the drop-down to select the minimum version of TLS required to access data in this storage account.

How do I securely store certificates in Azure? ›

Azure Key Vault is a cloud service that provides a secure store for secrets. You can securely store keys, passwords, certificates, and other secrets. Azure key vaults may be created and managed through the Azure portal. In this quickstart, you create a key vault, then use it to store a certificate.

How to add SSL certificate to Azure Virtual Machine? ›

Log into the Azure portal, and from the left menu, select App Services, then the app name. From the app's navigation menu, go to TLS/SSL settings > Private Key Certificates(. pfx) > Upload Certificate. In the PFX Certificate File section, choose your PFX file.

How do I make my Azure VM secure? ›

When you create an Azure virtual machine (VM), you must create a virtual network or use an existing virtual network and configure the VM with a subnet. Ensure that all deployed subnets have a Network Security Group applied with network access controls specific to your applications trusted ports and sources.

How to enable TLS on a virtual machine? ›

Perform the following:
  1. In the source, ensure that the following are true: The Hostname for the source matches the hostname in the virtual appliance's configuration. ...
  2. Change the Port to 636.
  3. If available, enable the Use TLS option.
  4. Test the connection.

How to enable TLS 1.2 in Azure VM? ›

Follow these steps:
  1. In the Azure portal, search for and select Microsoft Entra ID.
  2. In the Overview page menu, select Sign-in logs.
  3. Select a sign-in log entry for a user.
  4. Select the Additional details tab. ...
  5. Check for a Legacy TLS (TLS 1.0, 1.1, or 3DES) value that's set to True.
Apr 11, 2024

Which Azure service can be used to securely manage and store certificates? ›

Azure Key Vault is one of several key management solutions in Azure, and helps solve the following problems: Secrets Management - Azure Key Vault can be used to Securely store and tightly control access to tokens, passwords, certificates, API keys, and other secrets.

Which Azure service should you use to store certificates? ›

Store certificate in Azure Key Vault

Key Vault is an Azure service that helps safeguard cryptographic keys and secrets used by cloud applications and services. For App Service certificates, the storage of choice is Key Vault.

Where do you store certificates in Azure? ›

Azure Key Vault is a cloud service that provides a secure store for secrets. You can securely store keys, passwords, certificates, and other secrets.

How to check TLS version in Azure Virtual Machine? ›

On the "TLS/SSL settings" page select the Bindings tab, scroll down and under the "Protocol Settings" check the "Minimum TLS Version".

How to configure TLS certificate? ›

Applying a TLS configuration to a domain
  1. Click Domains.
  2. Find the card for the domain with the certificates on which you want to add additional TLS activations.
  3. Click View details next to the certificate on which you want to add additional activations.
  4. Click Add TLS activation.
Apr 18, 2024

How do I get an SSL certificate for my virtual machine? ›

Manual Procedure
  1. Use the keytool utility to import the public certificate file to the keystore. Replace the kmgr alias with the VDI-in-a-Box server hostname. ...
  2. Type the keystore password when prompted.
  3. A 'Certificate reply was installed in keystore' message appears if successful, as displayed in the following screen shot:

What is the most secure way to connect to Azure VM? ›

A bastion host provides secure and seamless Remote Desktop Protocol (RDP) connectivity to your VMs directly in the Azure portal over SSL. When you connect via a bastion host, your VMs don't need a public IP address, and you don't need to use network security groups to expose access to RDP on TCP port 3389.

What is the most secure way to access Azure VM? ›

RDP using a Private IP address across a Site to Site VPN

The ideal form of RDP connection is RDP across a Site to Site VPN connection. This keeps your communication with the Virtual Machine off the public internet granting protection against port scanning, brute force and DdoS attacks.

Do Azure VMs have antivirus protection? ›

Help protect your virtual machines from viruses and malware

Remotely install, configure, and maintain antimalware solutions on your virtual machines through the Azure portal, Azure PowerShell, and from the command line.

How do I check my TLS version in Azure Virtual Machine? ›

On the "TLS/SSL settings" page select the Bindings tab, scroll down and under the "Protocol Settings" check the "Minimum TLS Version".

How do I force TLS 1.2 in Azure App Service? ›

Azure Portal

Navigate to App Services. In the left navigation, select TLS/SSL settings. In Minimum TLS Version, select 1.2.

Does Azure support TLS? ›

Transport Layer Security (TLS) is a security protocol that establishes encryption channels over computer networks. TLS 1.2 is the current industry standard and is supported by Azure Resource Manager.

How do I enable HTTP on Azure VM? ›

To do this, follow the below steps:
  1. Click on your VM in the Azure portal.
  2. Click on Network Interfaces > The IP address tab > Network Security groups > Click on the NSG that is available there.
  3. On the tab that appears click on Inbound Security rules > Add > Click on the Service dropdown and select http > Click on OK.

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 5850

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.