We will deploy a ‘helloworld’ Django application that we created in our previous article Create a Django ‘helloworld’ application using Visual Studio Code in an Azure App Services. This will give us an overview on how deploy a Django web application to a Serverless Azure App Services.
Serverless app services gives us an advantage on effortless Continuous Deployment as well as Management of your Infrastructure for your application. Another advantage is, in-case of any failures, Azure in-built features like Application Insights, Azure Monitor, for security Azure DDoS Protection Plan as well as Azure Defender, Azure Security Center will take care for your application security.
Pre-Requisite —
1. Python — Install Python in your Desktop (Download Python | Python.org)
2. Install Visual Studio Code as well (Download Visual Studio Code — Mac, Linux, Windows)
STEP — 1) Deploy an Azure App Services in your Azure Subscription. We have covered that detailed step-to-step creation in our previous article, take a look — Create an Azure App Service with GitHub Continuous Deployment Integration.
STEP — 2) We suggest users to use Visual Studio Code as it gives an easy extension to connect to your Azure Account and One-click management of your Azure Services. Download Visual Studio Code from here — Download Visual Studio Code — Mac, Linux, Windows.
STEP — 3) To create a new django application we suggest you go through our detailed article on how to Create a Django ‘helloworld’ application using Visual Studio Code. We have covered there detailed step-to-step on how to create a Django “helloworld” from scratch and used the same application here to be deployed in our Azure App Services that we created in STEP — 1.
STEP — 4) In our last article our we deployed the Django application in our localhost server and were able to browse the Helloworld webpage we have created.
STEP — 5) To Deploy the application we created, we have to make small additions inside the applications that includes
- Requirement.txt file
- Adding Hostname inside ALLOWED_HOSTS
STEP — 5.1) Let’s create requirements.txt inside parent project directory ‘helloworldproject’
Add the following line(s) inside the requirements.txt file and save it.
Django<2.2,>=2.1.0
Note: You can use the latest version of Django as per your project requirement.
STEP — 5.2) Let’s now add the hostname to allow access to Django application, we do this to avoid external host access and restrict DDoS attacks.
- Let’s get the Hostname from the App Service we created. On the right you’ll get the URL to access the App Service, copy that URL, here we have ‘https://firsthelloworldapp.azurewebsitites.net’
2. Open ‘helloworldproject / settings.py ’ and add the copied URL removing the protocol(‘https’) from it and add ‘firsthelloworldapp.azurewebsites.net’ in ALLOWED_HOSTS.
ALLOWED_HOSTS = ['testhelloworldapp.azurewebsites.net']
STEP — 6) Now Let’s Add the Extension for the Azure App Services. Browser to Bottom Left to Settings Icon and Click on Extensions.
Or Use Shortcut Ctrl+Shift+X
STEP — 7) Search for ‘Azure App Service’ and click on Install. Make sure provider is Microsoft.
STEP — 8) After Installing Select your Azure Subscription in which you created your App Services. Here in Visual Studio Enterprise Subscription we have two App Services — sbapp, testhelloworldapp.
STEP — 9) Click on the Blue Arrow Icon on the Top to upload your Django Project to app services.
STEP — 10) Select the Parent Folder in which Django Project is created.
When we created the django-project using below command —
django-admin startproject helloworldproject
A Parent folder got created by name ‘helloworldproject ’within which it’s two subfolders ‘helloworldapp’ and ‘helloworldproject’ as well as ‘manage.py’ file.
We have to choose the Parent Folder here to upload.
STEP — 11) Choose the App Service where you want to deploy the project, make sure it’s URL is mentioned in ALLOWED_HOSTS as we added in STEP — 5.2.
Here we are using ‘testhelloworldpp’.
STEP — 12) Click on “Yes” to update the workspace configuration to run the build commands on the app services you created.
STEP — 13) Click on “Deploy”. It will overwrite the pre-existing configuration and remove all previous deployments. You CANNOT UNDO IT.
STEP — 14) Below on the Output Console, deployment will get started. Once Your Deployment is completed, click on Browse Website.
STEP — 15) Go to the URL of the app-services, you’d be able to see the live website.
In-case the Microsoft Default website is visible, restart the Azure App Service from portal.
Congratulations, you have successfully deployed your Django Application in a Serverless Azure App Services.
If you want to Integrate Azure SQL Servers which is a Microsoft Managed Platform as Service with your Django Application deployed in Azure App Services — Read our Article on —
This Article has been published by OrionLab.io