
Azure offers a simple and cost-effective way to host a static website. This is made possible through Azure Blob Storage, which allows you to store and serve static website content.
To get started, you'll need to create a storage account in the Azure portal. This will serve as the foundation for your static website's hosting environment.
Azure provides a variety of storage account options, including Hot and Cool storage. Hot storage is ideal for frequently accessed data, while Cool storage is suitable for less frequently accessed data.
Once you've created your storage account, you can upload your static website files to Azure Blob Storage. This is done by creating a container and then uploading your files to it.
Related reading: Image Hosting Online
Setting Up
To set up Azure to host your static site, you'll need to create a storage account through the Azure portal, Azure PowerShell, or Azure CLI. This will be the central hub for your site's files and data.
First, ensure you have an Azure subscription, which you can create for free if you don't already have one. Then, create a storage account, which is the foundation for accessing Azure Storage.
To enable static website hosting, select the name of your default file and optionally provide a path to a custom 404 page. If a blob storage container named $web doesn't already exist, one will be created for you.
Prerequisites
To set up static website hosting, you'll first need to enable it on your storage account. This feature is not enabled by default, so you'll need to take a few steps to get started.
You'll need an Azure subscription to access Azure Storage. If you don't already have one, you can create a free account.
A storage account is the central hub for all access to Azure Storage. To follow this tutorial, create a storage account using the Azure portal, Azure PowerShell, or Azure CLI.
For this setup, you'll need a general-purpose v2 Standard storage account or a storage account with hierarchical namespace enabled. This will allow you to host your static website.
To deploy your static website to Azure Storage, you'll need to install the Azure Storage preview extension in Visual Studio Code. This extension will integrate Azure Storage management functionality with Visual Studio Code.
Here's an interesting read: Azure Data Studio Connect to Azure Sql
Setup Site Storage
To set up site storage, create a storage account in Azure, which requires StorageV2 as the default storage type. You can do this by logging into the Azure portal and clicking on Storage Accounts.
To create a new storage account, click on Add and enter a suitable Subscription, Resource Group name, Storage Account Name, and Location. You can leave the other options at their defaults, but make sure to set Account kind to StorageV2.
Here are the steps to create a storage account:
- Login to the Azure portal;
- Either click on Storage Accounts or search for Storage Accounts and click on it;
- Click Add;
- I recommend creating a separate resource group for hosting your site but if you have one you want to use, select it in the dropdown Resource group box. Otherwise click Create new and enter into Name the new Resource Group;
- Enter the Storage account name which is just a name to identify this account;
- For this blog post we are going to leave the Location, Performance, Account kind, Replication and Access tier at their defaults. However, explore and understand those options and change them as you need to. Note that Account kind needs to stay as StorageV2 for you to host a static website. You may also want to look and change Location if you will not be using Azure CDN;
- Click Review + create. Review the choices and click Create when you’re happy;
- It will take a minute or so for Azure to create the Storage Account;
Once your storage account is created, you can enable static website hosting by selecting the name of your default file and optionally providing a path to a custom 404 page.
Hosting
To host a static site on Azure, you can enable static website hosting in the Azure portal, Azure CLI, or Azure PowerShell module.
You can enable static website hosting in the Azure portal by signing in, locating your storage account, and selecting the Capabilities tab to enable static website hosting.
The Azure CLI allows you to enable static website hosting with a single command, az storage blob service-properties update.
The Azure PowerShell module requires more steps, but can also be used to enable static website hosting with the Enable-AzStorageStaticWebsite command.
To configure your storage account for static website hosting, you'll need to specify a default index page and error page.
Here are the default index and error page settings:
- Default index page: index.html
- Default error page: 404.html
You can also enable static website hosting with the Azure CLI by appending --content-type 'text/html; charset=utf-8' to the command.
Additional reading: Wix Website - Site Page to Different Webpage on Site
Uploading and Managing Content
You can upload files to your Azure Storage account using the Azure portal or AzCopy. The Azure portal is a great option if you want to upload files manually, while AzCopy is a command-line tool that can automate the process.
If this caught your attention, see: Azure Azcopy
To upload files using the Azure portal, navigate to the storage account containing your static website and select the $web container. Then, select the Upload icon to open the Upload blob pane and choose the file you want to upload.
You can also upload objects to the $web container from a source directory using the Azure CLI. This method is useful if you want to upload multiple files at once. To do this, use the Azure CLI command `az storage blob upload` and specify the source path and file name.
Here's a summary of the Azure CLI command options:
Adding HTTP Headers
Adding HTTP headers to your static website can be a bit tricky, but it's doable. You can't configure headers directly as part of the static website feature.
One way to add headers is by using Azure CDN. This allows you to append or overwrite header values.
If you want to control caching behavior, you should check out the caching rules for Azure CDN.
Expand your knowledge: Azure Auth Json Website Azure Ad Authentication
Customizing and Securing
You can make your static website available via a custom domain.
Azure Storage natively supports HTTP access for custom domains, but for HTTPS, you'll need to use Azure CDN because Azure Storage doesn't yet support HTTPS with custom domains.
Enabling HTTPS for your custom domain requires using Azure CDN.
If the storage account requires secure transfer over HTTPS, users must use the HTTPS endpoint.
Azure Storage supports HTTP access for custom domains, making it easier to set up.
Suggestion: Cdn Azure
Monitoring and Analytics
Monitoring and analytics are crucial for any website, and hosting a static site on Azure provides a range of tools to help you keep track of your site's performance.
You can enable metrics on your static website pages to get traffic statistics on files in the $web container, which are then reported in the metrics dashboard. This allows you to monitor how your site is performing and make data-driven decisions.
To enable metrics, click on the Metrics option under the Monitor section of your storage account menu. This will display the metrics data, but you may need to expand the time frame to see all the API members used.
You might enjoy: Azure Data Studio vs Azure Data Explorer
Click on the time frame button, choose a time frame, and then click Apply to expand the time frame. This will allow you to see all the API members used within that time frame.
Select Blob from the Namespace dropdown and then select the Egress metric. This will give you information on the amount of data being transferred out of your Azure storage.
To filter the metrics report to show only the data you're interested in, click the Add filter button and choose API name from the Property selector. Then, check the box next to GetWebContent in the Values selector to populate the metrics report.
DNS and Storage
To host a static site on Azure, you need to set up storage and DNS. First, create a storage account in Azure, which requires logging into the Azure portal and clicking on Storage Accounts. You can either click on Storage Accounts or search for it and click on it.
To create a storage account, you'll need to enter a subscription, resource group name, storage account name, and location. You can also explore other options available, but for most purposes, the defaults are fine. Set the Secure Transfer Required to Disabled for HTTP access.
Once your storage account is set up, you can create a verification CNAME to prove you're not abusing the service. This involves creating a CNAME with a hostname like asverify.SUBDOMAIN, where SUBDOMAIN is the subdomain you want to use. The destination/target value should be the Primary Endpoint from the storage account, with asverify in front.
To enable static website hosting, go to your Storage Store and click on Static Website. Click Enable and note down the settings, especially the Index document name and Primary Endpoint. These will be used for CNAME creation and accessing your website.
Here's a summary of the steps:
- Create a storage account in Azure.
- Set up a verification CNAME to prove you're not abusing the service.
- Enable static website hosting in your storage account.
With these steps complete, you'll have a storage account set up and ready to host your static site.
Setup Your DNS
To set up your DNS, you'll need to create a verification CNAME and a final CNAME. First, log into your DNS provider's website, such as 123-Reg.co.uk, and navigate to the Advanced DNS section.
Create a verification CNAME with a hostname like asverify.SUBDOMAIN, where SUBDOMAIN is the subdomain you want to use for your website. For example, if you want to use myweb as your subdomain, the hostname would be asverify.myweb.
The destination/target for this CNAME should be the Primary Endpoint, preceded by asverify. For instance, if your Primary Endpoint is https://storageaccountname.z22.web.core.windows.net, your destination/target would be asverify.storageaccountname.z22.web.core.windows.net.
Next, create a final CNAME with the DNS entry "www" and the destination/target as the storage URL, such as storageaccountname.z22.web.core.windows.net.
Consider reading: Azure Dns Hosting
Create Storage Account
To create a storage account in Azure, you'll need to log into the Azure portal.
Click on Storage Accounts, either by searching for it or clicking on it directly. Then, click Add to create a new storage account.
You'll need to choose a subscription, resource group name, storage account name, and location. You can leave the rest of the defaults as they are, but make sure to select StorageV2 as the Account kind.
Here's a quick summary of the required information:
Once you've entered this information, click Review + create to review your choices. If everything looks good, click Create to create your storage account. It may take a minute or so for Azure to create the account.
Frequently Asked Questions
What is Azure static web?
Azure Static Web is a service that automatically deploys full-stack web apps to Azure from a code repository, streamlining the development process. It builds and deploys apps based on code changes, making it a seamless part of a developer's daily workflow.
Sources
- https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website
- https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website-how-to
- https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website-host
- https://nextnextnextfinished.wordpress.com/2020/05/27/hosting-a-static-website-with-azure/
- https://blog.pauby.com/post/migrate-static-website-to-azure/
Featured Images: pexels.com