I run several websites for various different things, including some of my Home Automation, including a plant database for the garden where I print QR codes on the plant labels (along with the name of the plant) to give the full information about where we bought it, when and details of it's blooming/fruiting/etc. However, I want to do this cheaply and securely, so I use Microsoft Azure.
In this post I'll document how I set up a simple static website using blob storage on Azure and their CDN product to protect the site with encryption and massive scalability. I also recommend you get the Microsoft Azure Storage Explorer and install it on your machine as it makes dealing with the files much easier than the portal (although you can do it all in the portal in a web browser).
Initial Setup
I'm going to assume that you've already signed up for an Azure account and are using the initial credit or have signed up for some sort of pay account, be it pay-as-you-go or a full subscription. You will then need to set up a Resource Group with a name and a data centre location to host it in. You can select availability zones as well, but just remember that the more redundancy you add, the more it will cost. Here, we're going to build something that will cost you a couple of pennies a month.
Next you will need to add a Storage Account for our Blob storage, which is just the ability to store blobs of data, in our case HTML, CSS and JavaScript files. Once you've created this, you will see a Static Website option in the left hand menu list. Make sure that this service is enabled with a default document name entered (index.html is usual). You can add an error page here as well if you want. You should also see a URL here to access your website, similar to: https://<storage_account_name>.z16.web.core.windows.net/.
Now, either in the Storage Browser in the portal or Azure Storage Explorer app on your desktop, navigate to the Blob Containers > $web folder and upload at least one HTML page called index.html (or whatever you set the default document to earlier). Then you can open the URL in a new tab and see your site.
Custom Domain & CDN
However, you'll probably want a better URL and use a custom domain. You can do this the simple way, by following these 4 steps:
- Get the hostname of your storage account (the URL above)
- Create a canonical name (CNAME) record in your DNS with something like www pointing to your core.windows.net URL
- Register your custom domain with Azure under Security + Networking > Networking and selecting the Custom Domain tab at the top of the page
- Test!
So, let's create an Azure CDN endpoint to create our certificates and enable the CDN features:
- In the portal under your storage account, select Security + Networking > Azure CDN
- Under the New endpoint section fill in the details with a resource name, pricing tier (I use Microsoft CDN (classic) as it's cheap and easy), pick a globally unique endpoint name and finally select your static website from the Origin hostname drop down
- Click Create and wait for a few seconds, then you should have a URL at <name>.azureedge.net or something similar.
- This will create an HTTPS endpoint, but let's enforce the use of HTTPS
- In the CDN page, go to Settings > Rules engine to set a new rule
- Click + Add rule at the top of the page and enter a name for the rule, such as 'EnforceHTTPS'
- Add condition: if Request Protocol > Equals > HTTP
- then add action: URL Redirect > Found (302) > HTTPS (and leave the other boxes blank to just copy the URL over)
Now anyone navigating to your site via HTTP will automatically be transferred to the HTTPS version. This is all very good, but we still want to use our custom domain name. If you're happy to use a subdomain (do this, it's much easier) such as www.yourdomain.com then Azure will do everything for you around certificate management for free. Azure won't create root certificates for you though (i.e. yourdomain.com) so, you'll have to look up one of the many tutorials online to do this, but you'll need to pay for a certificate from an external provider. For now, let's assume you're using the easy route:
- Create a CNAME record in your DNS with www (or some other subdomain) pointing to the CDN endpoint, which should be something like yourdomain.azureedge.net
- Under your CDN endpoint in the Azure portal, select Settings > Custom domains and click the + Custom domain button at the top of the page
- Add your domain here, e.g. www.yourdomain.com and save it (it will put a Green tick and allow you to save only if you've created the correct DNS entry above)
- Click on the newly created domain and then toggle the Custom Domain HTTPS to ON
- Leave the defaults of CDN managed and TLS 1.2 selected and save your updates
- Wait for the process to complete, which will take a few minutes
- Once complete, navigate to your new site!
Comments
Post a Comment