Django is an open-source, Python-based web framework that can be helpful to developers that want to launch web applications in just a few hours. The framework follows the Model View Template (MVT) to build applications. It reduces the complexity of Web development, enabling developers to focus on the task of writing applications. It offers the necessary provisions for site maps, content administration, user authentication, and RSS feeds, among other things. DjangoOne interesting thing to note is that some major, heavy traffic websites use Django because of its ability to quickly and flexibly scale to meet traffic peaks.
This blog post goes over how you can launch and deploy a Django application on Alibaba Cloud. In particular, you will set up and launch an Alibaba Cloud Elastic Compute Service (ECS) instance installed with Linux, then you will install and deploy a Django application on this instance.
As your first prerequisite, you'll need to have virtualenv and virtualenv wrapper installed for your Python-based application to create isolated environments for your Python projects. For this, you can do so with the following steps:
pip
sudo apt-get install python-pip
virtualenv
sudo pip install virtualenv
dir
to store your virtualenvsmkdir ~/.virtualenvs
virtualenvwrapper
sudo pip install virtualenvwrapper
WORKON_HOME
to your virtualenv direxport WORKON_HOME=~/.virtualenvs
WORKON_HOME
to your virtualenv direxport WORKON_HOME=~/.virtualenvs
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.
1. Install git.
apt-get install git
2. Install Nginx as a web server so that you can run your application behind it.
Sudo apt-get install nginx
Now, it's time to get on to setting up and launching your Alibaba Cloud ECS instance.
Let's take a quick 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, which can either be the Quick Launch option, which happens to offer a special pricing on ECS instances and data transfer, or the Custom Launch option, which features two different pricing models. More specifically, you can opt for either subscription billing, which is paid on a monthly or yearly basis, or pay-as-you-go billing based on your needs. In this tutorial, we have selected Pay-As-You-Go as the billing method.
4. In this window, select the Datacenter Region and Zone where you wish to launch the ECS instance. If you select the Region but do not select the Zone, then the system will place the instance in a random 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. This is suitable for users requiring simple and fast use of ECS. VPC is a logically isolated private network that supports dedicated connections. It is suitable for users comfortable with a more involved network management process.
7. Now, you need to choose the Operating System. Under each option, a list of different OS versions is available. Select the Ubuntu option.
8. Choose the type of System Disk from the drop-down menu based on 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.
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 the 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.
Now that you have created and launched your ECS instance using the Alibaba Cloud Management Console, let's look at how you can install and deploy a Django application.
1. Log in to your server using a SSH command:
2. Enter the password.
3. Set up the environment for deploying the Django application by creating a new virtualenv:
mkvirtualenv DjangoApp
To exit your new virtualenv, use deactivate. Now, 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 a 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 have set up 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 the Database Schema, and activate the Python environment. You can do so with the following steps:
python manage.py migrate
10. Collect all the static files, including the CSS and JS files. You can do so with the following steps:
Python manage.py collectstatic --noinput
STATIC_URL
path to the location where all the static files are to be collected.These variables are defined in setting.py
inside the Project directory.
STATIC_URL
STATICFILES_DIRS
STATIC_ROOT
11. Install the uwsgi
library and start the server using uwsgi
server. You can do so by implementing the following steps:
pip install uWSG
ini
file, which will ne used to deploy your django application. vim uwsgi.ini
uwsgi.ini
on the application dir
. For a better understanding of how to write an 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.
In this post, we discussed how you can deploy a Django application on Alibaba Cloud. To summarize things, the first step involved with this was launching and running an ECS instance with Ubuntu as the operating system. Next was installing and deploying the Django application on this instance. Keep in mind that, as part of the prerequisites to this tutorial, you should be in a position to complete the deployment and have a valid Alibaba Cloud account.
Below is a list of products that are related to this tutorial and would be helpful if you were to deploy a Django application in a real-world production scenario.
Getting Started with Alibaba Cloud ApsaraDB for Memcache SDK with NodeJS
2,599 posts | 762 followers
FollowAlibaba Clouder - April 28, 2021
Alibaba Clouder - March 22, 2019
Alibaba Clouder - April 12, 2018
Alibaba Clouder - March 22, 2019
Alibaba Clouder - August 26, 2020
Alibaba Clouder - December 11, 2018
2,599 posts | 762 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreAn encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world
Learn MoreA virtual private cloud service that provides an isolated cloud network to operate resources in a secure environment.
Learn MoreMore Posts by Alibaba Clouder