Deploy Django Application on Alibaba Cloud

Overview & Objectives

This post explains how you can launch and deploy your Django application on Alibaba Cloud (with the OS being Ubuntu). A detailed discussion of the steps on the two processes stated hereunder follows:

● Launching your first Linux instance using the Alibaba Cloud Management Console

● Installing and deploying your Django application on this instance


About Django

Django is an open-source, Python-based web framework that helps developers launch web applications just a few hours after conception. It follows the Model View Template (MVT) to build applications. It reduces the complexity of Web development, enabling developers to focus on writing applications. It offers provisions for site maps, content administration, user authentication, RSS feeds, and other tasks — right out of the box. Some heavy traffic sites use Django due to its ability to quickly and flexibly scale to meet the peaks in fluctuating traffic.

Prerequisites

1. Install virtualenv and virtualenv wrapper for Python application to create isolated environments for Python projects.

● Install pip

sudo apt-get install python-pip

● Install virtualenv

sudo pip install virtualenv

● Create a dir to store your virtualenvs

mkdir ~/.virtualenvs

● Install virtualenvwrapper

sudo pip install virtualenvwrapper

● Set WORKON_HOME to your virtualenv dir

export WORKON_HOME=~/.virtualenvs

● Set WORKON_HOME to your virtualenv dir

export WORKON_HOME=~/.virtualenvs

● Add virtualenvwrapper.sh to .bashrc
Add this line to the end of ~/.bashrc so that the virtualenvwrapper commands are loaded.

/usr/local/bin/virtualenvwrapper.sh

Exit and re-open your shell, or reload .bashrc with the command .bashrc or source ~/.bashrc and you are ready to go.

2. Install git.

apt-get install git

3. Install Nginx as a web server so that you can run your application behind it.

Sudo apt-get install nginx

Launching your first Linux instance

Let us look at the steps involved in running an ECS instance using the Alibaba Cloud Management Console.

1. Log in to your account and navigate to “Elastic Compute Service” under the “Product & Services” section. Click on Overview in the sidebar menu. It displays the list of instances already running. Click on Buy Instance to purchase an instance from any of the regions or go on to the next step to create a new instance.

2. Click on Instances in the sidebar menu. Select the required region in the Instance List and click on Create Instance in the top right bar.

3. You will be redirected to the Product Purchase console where you need to select your preferred package- either the Starter Package that offers a special pricing on ECS instances and data transfer, or Advanced Purchase that offers two pricing models. You can opt for either Subscription (monthly or yearly) or Pay-As-You-Go as per your requirements. In this case, we have selected Pay-As-You-Go.

4. In this window, select the Datacenter Region and Availability Zone where you wish to launch the ECS instance. If you select the Region but do not select the Availability Zone, then the system will place the instance randomly in any zone.

5. Now you need to choose the Instance Type that you wish to create. Select the type of instance Generation from the Generation tabs based on your requirement. Generation types represent different instance types based on the configuration and computing power used.

6. Next, select Network Type to launch your ECS instance. It can be either Classic Network or VPC depending on your needs. In a Classic network, Alibaba Cloud assigns the IP address in a distributed way. It is suitable for users requiring simple and fast use of ECS. VPC is more of a logically isolated private network that supports dedicated connections. It is suitable for users comfortable with network management. After choosing the appropriate Network Type, select your Network Billing Type that can be either Data Transfer or Fixed Bandwidth. In both cases, you need to set the Network Bandwidth Peak (maximum data transfer rate).

7. Now, you need to choose the Operating System. Under each option, a list of different OS versions is available. Select the Ubuntu.

8. Choose the type of System Disk from the drop-down menu as per your needs. You can also add more disks to this list by clicking on “Add a disk.”

9. In the Security Setup section, you can create a password for enhanced security or skip this step at this stage and perform it later from the management console.

10. In the Purchase Plan section, type the name of your Instance and set the number of instances you want to launch.

11. Check the configuration details and total price in the Overview section and click on Buy Now.

12. Click on Activate to confirm your order and launch your instance.

13. Once your instance is launched, you can view it in the console, under the Instances tab.

Installing and Deploying Django Application

Now that you have created and launched your ECS instance using the Alibaba Cloud Management Console let us look at how you can install and deploy a Django application.

1. Login to your server using ssh command

2. Enter the password

3. Set up the environment for deploying Django application by creating a new virtualenv:


mkvirtualenv DjangoApp 

To exit your new virtualenv, use deactivate.
You can switch between environments with workon. To load or switch between virtualenvs, use the workon command:


workon DjangoApp 

4. Install Django on your current environment


    pip install Django

5. Create Sample Project by using django-admin command and change directory to the project folder.


django-admin startproject todoApp

cd todoApp/

6. Migrate or bootstrap your database.


python manage.py migrate 

7. Create a superuser to access admin panel.


python manage.py createsuperuser

8. Once you setup your user, test your application by running the runserver command which is handled by manage.py


python manage.py runserver 0.0.0.0:8000

You will see the following running on port 8000


Go to /admin, which is your admin panel, where you can manage the application.

Now put your application behind the web server by using Nginx.

9. Create Database Schema

Activate python environment.

● Change directory to Django Project directory

● Run the following command

  python manage.py migrate

10. Collect all the static files (css, js, etc.)

● Run the following command to collect all the static files at any particular location

Python manage.py collectstatic --noinput

● The developer bears the responsibility of setting the STATIC_URL path to the location where all the static files are to be collected.

● These variables are defined in setting.py inside Project directory.

1. STATIC_URL

2. STATICFILES_DIRS

3. STATIC_ROOT

11. Install the uwsgi library and start the server using uwsgi server. I


   pip install uWSG

● Create ini file which is used to deploy your django app


vim uwsgi.ini

● Save it to uwsgi.ini on the application dir. For a better understanding of how to write ini file, refer to Quickstart for Python/WSGI applications.Run this command to start your application.

uwsgi uwsgi.ini (your ini file)

12. Change the nginx config file to serve the application.

server   {       listen 80 default_server;        		listen [::]:80 default_server ipv6only=on;        		server_name localhost;  location /static/   {        			include uwsgi_params;        			alias /root/todoApp/public/;  } location  /    {      			  include uwsgi_params;      			  uwsgi_pass unix:/tmp/uwsgi.sock; } } 

13. Restart nginx and your application is up and running behind nginx at port 80.

Conclusion

In this post, we discussed how you could deploy a Django application on Alibaba Cloud. To summarize, the first step is running an ECS instance (with OS being Ubuntu). Then you can install and deploy the Django application on this instance. Keep in mind the prerequisites, so that you are in a position to complete the deployment.

Related Alibaba Cloud Products

ECS

Alibaba CloudMonitor

Web Application Firewall (WAF)

Alibaba Cloud DNS

ApsaraDB for RDS