Azure is a cloud computing platform offered by Microsoft that provides a wide range of services and features for building, deploying and apps and services in the cloud.

If your application needs to interact with Azure's AI and ML services then it makes sense to deploy your Django app to their infrastructure.

In this article you will see how to deploy your Django application on Azure VM.

If you are looking for tutorial for another cloud provider checkout out one of these:

Table of contents

Requirements

  1. An application to deploy
  2. Appliku Account
  3. Azure Account

Django Application

The application used for this tutorial can be found here: https://github.com/appliku/djangotutorial

Your app should be pushed into a git repository. You can use GitHub, GitLab.

For ease of deployment we have the following recommendations:

  • have the requirements.txt file in the root of your git repository. Don't forget to add gunicorn(or alternative) and psycopg2-binary==2.9.5 for Postgres support (or psycopg[binary])
  • Your app needs to respect environment variables like DATABASE_URL for the database credentials, ALLOWED_HOSTS for the list of allowed domains, SECRET_KEY and REDIS_URL if applicable.
  • Please, don't forget to have DEBUG turned off when you deploy to a publicly accessible environment.

We have a full How to Start and Deploy a Django Project if you want to learn more

Also: check our SpeedPy.com SaaS boilerplate

Appliku Account

If you don't already have an Appliku Account create it by going here: https://app.appliku.com/

Click on "Add a server" Appliku dashboard: Click on "Add a server"

Pick a Custom Server:

Appliku dashboard: Pick a Custom Server

Select this key, we will need it at server creation step. Appliku dashboard: Select this key, we will need it at server creation step

Azure account

If you don't already have an Azure account you should sign up here.

When you created your account, log in and you will see a welcome screen. Click on "Go to Azure portal"

Azure portal: When you created your account, log in and you will see a welcome screen

Click on "Deploy a Virtual Machine" Azure portal: Click on "Deploy a Virtual Machine"

Click "Create" under "Create a Linux virtual machine" Azure portal: Click "Create" under "Create a Linux virtual machine"

You will see the page "Create a virtual machine" with a lengthy form fll of options.

First, create a new "Resource group" by clicking "Create new"

Azure portal: First, create a new "Resource group" by clicking "Create new"

Write a name for it and click OK

Azure portal: Write a name for it

Scroll down, and in the section "Instance details" give server a name and select a region for your server.

For the "Image" option chose Ubuntu 20.04 LTS

Scroll down and find "Size". Click on "see all sizes" and pick the one that suites your needs. For the purpose of this tutorial we will pick the one market "Free services" and click "Select"

Azure portal: Scroll down and find "Size"

For authentication type choose "SSH public key".

You can leave "azureuser" for the "Username", you can't specify "root" here.

Add a public key given to you in the Appliku dashboard earlier.

For the "Public inbound ports" select Allow selected ports and in the dropdown select HTTP(80), HTTPS(443) and SSH (22)

Then click "Review + create"

Azure portal: Then click "Review + create"

You are presented with the overview of a server to be created. Click "Create" at the bottom of the screen.

Azure portal: You are presented with the overview of a server to be created

Azure VM creation will begin. Wait for it to finish.

Azure portal: Azure VM creation will begin

When the server is created – click on Go to resource.

Azure portal: When the server is created – click on Go to resource

You will see the overview of the server. We need the IP address of the Azure VM.

Azure portal: You will see the overview of the server

It is on the right, hover over the IP address to see the copy icon. Click on it to copy.

Azure portal: It is on the right, hover over the IP address to see the copy icon

Go back to the Appliku Dashboard and paste IP address and type "azureuser" for the username and click "Check credentials and add server"

Azure portal: Go back to the Appliku Dashboard and paste IP address and type "azureuser" for the...

You will see that server is added and soon will start setting up.

Watch for "Setup Status" changes. Wait for it to become "Finished". It will take a few minutes as Appliku is installing and configuring all the packages. If "Setup Status" will become "Error" check the tab "Setup Logs" for errors.

Appliku dashboard: Watch for "Setup Status" changes

When server setup is complete and server is ready to deploy your apps you will see server resources gauges come to live.

Azure portal: When server setup is complete and server is ready to deploy your apps you will see...

Time to deploy our Django app!

Create a Django Application on Azure

Click on the "Applications" menu link Appliku dashboard: Click on the "Applications" menu link

You will get to the Git Provider selection. Appliku dashboard: You will get to the Git Provider selection

Creating application from GitHub

Select the "Setup Credentials" for GitHub. Appliku dashboard: Select the "Setup Credentials" for GitHub

You need to get a personal token from GitHub.

Go to this link to create a token: https://github.com/settings/tokens

GitHub: Go to this link to create a token

Click on Generate new token -> Generate new token(Classic) Fill in the note, select expiration date, check the "repo" checkbox and "user:email"

GitHub: Fill in the note, select expiration date, check the "repo" checkbox and "user:email"

GitHub: Fill in the note, select expiration date, check the "repo" checkbox and "user:email" (2)

And click "Generate token".

Copy the token from the next page.

GitHub: Copy the token from the next page

Go back to Appliku dashboard and paste the GitHub Token, and click Test Credentials and Save

Appliku dashboard: Go back to Appliku dashboard and paste the GitHub Token

Status must change to "Active".

Click on the logo in top left corner to get back to your team and go to Applications again and create an app, select GitHub.

Appliku dashboard: Click on the logo in top left corner to get back to your team and go to Applic...

Fill the form with the app name, select the repository with your application, branch and select the server, click on "Create Application". Appliku dashboard: Fill the form with the app name, select the repository with your application...

You will see your app has been created, but not yet deployed. Appliku dashboard: You will see your app has been created, but not yet deployed

Creating application from GitLab

Alternatively if you use GitLab you should add credentials for GitLab and create an app from GitLab.

Go to Applications menu link, Click "Add Application" and Setup credentials for GitLab.

Appliku dashboard: Go to Applications menu link, Click "Add Application" and Setup credentials fo...

You need to create a Personal Access Token here: https://gitlab.com/-/profile/personal_access_tokens

Give the key a name, set or clear expiration date, select "api" checkbox and click "Create Personal Access token"

GitLab: Give the key a name, set or clear expiration date, select "api" checkbox

Copy the token and paste it in Appliku dashboard.

Appliku dashboard: Copy the token and paste it in Appliku dashboard

After you click "Test Credentials and Save" the Status should turn green and say "Active".

Appliku dashboard: After you click "Test Credentials and Save" the Status should turn green and s...

Click on the logo to go back to your team, go to Applications, Add Application and select GitLab

Appliku dashboard: Click on the logo to go back to your team, go to Applications, Add Application...

Give your application a name, pick repository and the branch, select the server and click "Create Application"

Appliku dashboard: Give your application a name, pick repository and the branch, select the server

Setup the app

Databases

First, create a Postgres Database. Click on Add Database on the right

Appliku dashboard: First, create a Postgres Database

Select the Postgres 16 option and click "Create database" Appliku dashboard: Select the Postgres 16 option

Your database will start deploying, wait for the deployment status to become "deployed". Appliku dashboard: Your database will start deploying, wait for the deployment status to become "...

Define processes

Let's tell Appliku how to run your app

On the Application Overview page you will see a red triangle saying "No processes found". Click on the "Add Processes" button. Appliku dashboard: Click on the "Add Processes" button

Add two processes:

  • one MUST be called web to answer to HTTP requests and command should be gunicorn project.wsgi --log-file -(pay attention to the last dash). It can be slightly different for your project
  • second one MUST be called release and this is the command that will be executed after each successful deployment. If not specified it will default to python manage.py migrate. I recommend to create a bash script if your app needs to run several commands executed on each release. In our example, the command is bash release.sh Appliku dashboard: second one MUST be called release and this is the command that will be execute...

Click save and deploy button, a deployment will start.

Environment variables

Go to the "Environment Variables" tab and add env vars needed for your project.

At least specify the SECRET_KEY and ALLOWED_HOSTS. These variables should be used by your app you can check here how to do that: https://github.com/appliku/djangotutorial/blob/master/project/settings.py

The SECRET_KEY variable value should be some long hard to guess string. Appliku dashboard: The SECRET_KEY variable value should be some long hard to guess string

The ALLOWED_HOSTS variable should be a list of domains on which your app is accessible. As I already mentioned above, don't hardcode this, but make your app respect this environment variable.

By default, each app in Appliku receives a subdomain APPNAME.applikuapp.com, so in our example it should be mydjangoapp.applikuapp.com. If you add custom domains, make a comma-separated list like this mydjangoapp.applikuapp.com,example.com,www.example.com. You can disable the default subdomain in build settings.

When you are finished with environment variables click the "Save and deploy" button and deployment will start.

Reviewing application logs

From the Application Overview page you can go to App Logs to check if there are any errors.

Appliku dashboard: From the Application Overview page you can go to App Logs to check if there ar...

Appliku dashboard: From the Application Overview page you can go to App Logs to check if there ar... (2)

Open your application

Appliku dashboard: Open your application

To access your app click on "Open App" and click on the domain name. It will open your app in a new window.