Deploy Django on Azure
How to deploy Django to Azure
Microsoft Azure is a cloud computing platform that allows you to run your applications and store your data in the cloud instead of on your local computer. It provides a range of services such as virtual machines, databases, and storage that can be easily accessed and managed through a web-based portal. With Azure, you can quickly scale up or down depending on your needs and only pay for what you use, making it a cost-effective solution for businesses of all sizes.
Topics covered
Azure
Account CreationAzure
Creating a VMAzure
Connect to VM- The project to be deployed
Azure
Database set upAzure
Environment set upAzure
Domain settingsAzure
SSL certificatesAzure
CI/CD from GithubAzure
LIVE Service monitoring
Azure
Account Creation​
Microsoft Azure offers a 30-day free trial for new users, with $200 of free credit to use on any Azure service. You will have access to all Azure services during the trial, and can upgrade to a paid plan afterwards
- Go to the Azure website (https://azure.microsoft.com).
- Click on the "Free account" button on the top right corner of the page then click "Start free".
- Sign in to your Microsoft account or create one if you don't have account.
- You'll be prompted to set up your free account. Fill in the required information, such as your name, email, address, country/region, and phone number
- Choose either "Text me" or "Call me" to receive your verification code. Enter the code and click "Verify code".
- Check the "I agree to the customer agreement" then click "Next".
- After verifying your identity, you'll be asked to provide your credit card information. Don't worry, this is just to verify your identity and you won't be charged anything unless you upgrade to a paid account.
- Once you've entered your credit card information, you'll be taken to the Azure dashboard. Congratulations, you've created an Azure account!
Azure
Creating a VM​
- Sign in to your Azure portal.
- Select "Virtual machines" under "Azure services" or search it using the search bar.
- In the "Instance details" section, you'll need to provide some information about your virtual machine, such as the name, region, operating system, and size. For this tutorial, we will use Ubuntu 22.04 LTS instance
- Under "Administrator account", select SSH public key, enter the username and the key pair name.
- Allow the HTTP and HTTPS traffic, under the "Inbound port rules", select "Allow selected ports" then select "HTTP (80)", "HTTPS (443)", and "SSH (22)" from the drop-down.
- Leave the remaining defaults then go to "Review + create" tab.
- If everything looks good, click on the "Create" button to create your virtual machine.
- Azure will start provisioning your virtual machine, which may take a few minutes.
- Don't forget to download the private key when the Generate new key pair window opens
The Ubuntu command will be used in the next steps since this instance is running on Ubuntu. If you are using a different operating system, adjust the command according to your OS. Once your virtual machine is ready, you can connect to it using remote desktop or SSH.
Azure
Connect to VM​
In this tutorial, we will connect to our instance using SSH and generated private key.
Open terminal on your local computer, this tutorial will use Ubuntu as operating system.
Change permission of your private key with this following command.
chmod 400 /path/to/yourkey/myKey.pem
If you don't have SSH client on your computer, install it by running this command.
sudo apt-get install openssh-client
Connect to your instance through SSH.
ssh -i /path/to/youkey/myKey.pem <username>@<publicIP>
The project to be deployed​
Source project: https://github.com/app-generator/deploy-azure-django
If git
isn't installed, run following command to install it.
sudo apt-get install git
Then clone the project by running this command.
git clone https://github.com/app-generator/deploy-azure-django
Azure
Database set up​
You only need to choose one of these databases, either PostgreSQL or MySQL.
PostgreSQL​
Install PostgreSQL in your instance
sudo apt-get install postgresql
Start the service
Once the installation is complete, PostgreSQL will be automatically running. Otherwise, use the following command to start the service.
sudo systemctl start postgresql
Login to PostgreSQL
sudo -u postgres psql
Create database
CREATE DATABASE appseed_db;
Replace
appseed_db
with the name that you want.Create a user and grant permission to the user.
For this demo, we'll grant all permissions to all tables for the created user.
CREATE USER appseed_db_usr WITH PASSWORD 'pass';
GRANT ALL PRIVILEGES ON DATABASE appseed_db TO appseed_db_usr;Replace
appseed_db
with your database name,appseed_db_usr
andpass
with your desired username and password.Exit PostgreSQL
\q
MySQL​
Install MySQL in your instance
sudo apt-get install mysql-server libmysqlclient-dev
Start the service
Once the installation is complete, MySQL will be automatically running. Otherwise, use the following command to start the service.
sudo systemctl start mysql
Login to MySQL
sudo mysql
Create database
CREATE DATABASE appseed_db;
Replace
appseed_db
with the name that you want.Create user and grant permission to the user.
For this demo, we'll grant all permissions to all tables for the created user.
CREATE USER 'appseed_db_usr'@'localhost' IDENTIFIED BY 'pass';
GRANT ALL PRIVILEGES ON appseed_db.* TO 'appseed_db_usr'@'localhost';
FLUSH PRIVILEGES;Replace
appseed_db
with your database name,appseed_db_usr
andpass
with your desired username and password.Exit MySQL
exit
Azure
Environment set up​
We need to set up the instance for Python environment. To find the right version to use, you need to check the Django version. If the version is not set in requirements.txt
, it means the latest version. In Django documentation, we can found the compatible Python version.
Django 4.2 supports Python 3.8, 3.9, 3.10, and 3.11.
Setting up Python​
First, we need to install Python, pip, and virtualenv if they aren't already installed. Use the following commands to install them.
sudo apt-get update
sudo apt-get install python3 python3-pip python3-virtualenv
Verify the installation with the following command.
python3 --version
pip --version
Setting up virtual environment and dependencies​
Enter the project directory, create a virtual environment to isolate the project dependencies, and activate it using the following commands.
cd deploy-azure-django
virtualenv env
source env/bin/activate
Now you can install packages or dependencies without affecting other projects.
For installing the dependencies, you need to follow these steps:
Open
requirements.txt
file using a text editor, such as nano or vim.Uncomment the package following the database you used by deleting the
#
in front of it.psycopg2-binary
: for PostgreSQLmysqlclient
: for MySQL
Save and exit, then install the packages.
pip install -r requirements.txt
Setting up environment variables​
Before moving on to the next steps, we need to set up environment variables so that the app can connect to the database and complete the setup.
Copy the
env.sample
file to.env
.cp env.sample .env
Open
.env
file using a text editor, such as nano or vim.Remove comment from database variables and fill it by following your database configuration.
For PostgreSQL:
DB_ENGINE=postgresql
DB_PORT=5432For MySQL:
DB_ENGINE=mysql
DB_PORT=3306
Fill out the other variables as needed, then save the file.
Setting up database​
Run the following commands to set up your database.
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
Running the app​
You're all set up! You can now run the app using the following command.
python3 manage.py runserver
Note that your project is currently only accessible locally.
Azure
Domain settings​
This demo use a domain that already been connected to Cloudflare.
Adding DNS record in Cloudflare​
First, we need to add a DNS record in Cloudflare to connect the domain to our Azure instance. Follow these steps:
- Add an
A
record. - In the
Name
field, enter@
for the domain or anything else if you want to use a subdomain. - In the
IPv4 address
field, enter the Public IP address of the instance. You can find this in Azure > Services > Compute > EC2 > Instances (running). - Turn the
Proxy status
on. - Click 'Save'.
Setting up reverse proxy using NGINX​
Next, we need to set up a reverse proxy to point the domain to our app.
Install NGINX in your instance.
sudo apt-get update
sudo apt-get install nginxStart the NGINX service using the following command.
sudo systemctl start nginx
Set up a server block by creating a new configuration file
sudo nano /etc/nginx/sites-available/your_domain
Replace
your_domain
with your domain name.Paste this following config into the file. Don't forget to replace
your_domain
with your domain name.server {
listen 80;
listen [::]:80;
server_name your_domain;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Enable the new config by creating a symbolic link to the
site-enabled
directory.sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled
Remove the default config by unlinking it.
sudo unlink /etc/nginx/sites-enabled/default
Check your configuration by running this command.
sudo nginx -t
If there are no problems or errors, restart NGINX to apply the new config.
sudo systemctl restart nginx
Allowing access to your domain​
Finally, we need to allow access to our domain in our Django settings. Follow these steps:
Open the
core/settings.py
file in your project.Search
ALLOWED_HOSTS
and add your domain and Public IP address into it. For example:ALLOWED_HOSTS = ['your_domain', 'Public IP address']
Save the file.
Azure
SSL certificates​
This demo will use SSL certificate from Cloudflare. You can also use certbot as an alternative.
Creating an SSL certificate in Cloudflare​
- Navigate to SSL/TLS > Origin Server in Cloudflare.
- Click on "Create Certificate".
- Keep the default config then click on "Create".
- You will have the Origin Certificate and Private Key. We will use this in the server.
Editing the NGINX config​
Create a new file in
/etc/ssl/certs
, paste the Origin Certificate into it, and save.sudo nano /etc/ssl/certs/cloudflare.pem
Create a new file in
/etc/ssl/private
, paste the Private Key into it, and save.sudo nano /etc/ssl/private/cloudflare.key
Edit the NGINX config.
sudo nano /etc/nginx/sites-available/your_domain
Add this config below the existing config. Replace
your_domain
with your domain name.server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/certs/cloudflare.pem;
ssl_certificate_key /etc/ssl/private/cloudflare.pem;
server_name your_domain;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Check your configuration by running this command.
sudo nginx -t
If there are no problems, restart NGINX to apply this config.
sudo systemctl restart nginx
Changing the SSL/TLS encryption mode in Cloudflare​
Finally, change the SSL/TLS encryption mode to Full (strict) in the Overview tab of the SSL/TLS section in the Cloudflare dashboard. This tells Cloudflare to always encrypt the communication between Cloudflare and the NGINX server at your source.
Azure
CI/CD from Github​
Continuous Integration and Continuous Deployment (CI/CD) is a process that automates the building, testing, and deployment of software. In this tutorial, we will set up CI/CD from Github to our Azure instance.
Setting up SSH Keys and Secrets​
Before creating Github Action, we need to set up the Github secrets. The key pair generated in the instance creation process will be used.
- Open your repository on Github. Go to Settings > Secrets and variables > Actions.
- Click on "New repository secret" to add a secret. You need to add these following secrets:
- HOST: Public IP address of instance
- USERNAME: instance username
- KEY: private key, from
-----BEGIN
until end of the key. Find your key pair with the.pem
extension. - PORT: 22
Setting up CI/CD with Github Action​
After setting up the SSH keys and secrets, we can move the next steps:
- Create a new folder
.github/workflows
in the project. - Create a new file
cd.yml
in the.github/workflows
, paste this following code.
name: Django CI/CD Workflow
on:
push:
branches:
- main
jobs:
deployment:
runs-on: ubuntu-latest # according to your os
steps:
- name: executing remote ssh commands using ssh key
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
script: |
cd ~/deploy-azure-django
git pull origin main
env/bin/pip install -r requirements.txt
env/bin/python manage.py migrate
- Save the file then commit and push it to the repository.
Now, every time you push code to the main
branch on Github, it will automatically deploy to your Azure instance.
Azure
LIVE Service monitoring​
We'll use UptimeRobot to monitor the app. UptimeRobot is a popular online service that monitors the uptime and performance of websites, servers, and other online services.
Go to UptimeRobot create an account or sign in if you already have account. Create monitor by clicking "Add New Monitor" button then follow this config:
- Monitor Type: HTTP(s)
- Friendly Name: django-Azure (or anything else you want)
- URL (or IP): your_domain
Keep the rest of the config as default and click "Create Monitor"
UptimeRobot will now start monitoring your app. You can view the monitor status and performance data on the UptimeRobot dashboard
✅ Resources​
- 👉 Access AppSeed and start your next project
- 👉 Deploy Projects on Aws, Azure and Digital Ocean via DeployPRO
- 👉 Create an amazing landing page with Simpllo, an open-source site builder
- 👉 Django App Generator - A 2nd generation App Builder