How to deploy a Django application on Alibaba Cloud

This article describes how to launch and deploy a ** Django ** application on Alibaba Cloud.

This blog is a translation from the English version. You can check the original from here. We use some machine translation. We would appreciate it if you could point out any translation errors.

Prerequisites

As a first prerequisite, virtualenv and virtualenv wrapper for Python-based applications You need to install .readthedocs.io/en/latest/?spm=a2c65.11461447.0.0.28a16cabOLprE3) to create a separate environment for your Python project. To do this, follow the steps below.

--Install pip

sudo apt-get install python-pip

--Installing virtualenv

sudo pip install virtualenv

--Create a dir to store virtualenvs.

mkdir ~/.virtualenvs

--Installing virtualenvwrapper

sudo pip install virtualenvwrapper

--Set WORKON_HOME in the virtualenv directory

export WORKON_HOME=~/.virtualenvs

--Add virtualenvwrapper.sh to .bashrc. Add this line to the end of ~ / .bashrc so that the virtualenvwrapper command is loaded.

/usr/local/bin/virtualenvwrapper.sh

Exit the shell and reopen it, or reload .bashrc with the .bashrc or source ~ / .bashrc command and you're ready to go.

  1. Install git.
apt-get install git
  1. Install Nginx as a web server so that you can run your applications behind it.
Sudo apt-get install nginx

procedure

Now let's get started with setting up and launching an Alibaba Cloud ECS instance.

Launch the first Linux instance

Let's take a quick look at the steps to run an ECS instance using Alibaba Cloud Management Console.

  1. Log in to your account and go to the "Elastic Compute Service" section of the ** Product & Services ** section (https://account.alibabacloud.com/login/login.htm?oauth_callback=https%3A%2F%2Fecs.console. Go to aliyun.com% 2F% 3Fspm% 3Da2c65.11461447.0.0.28a16cabOLprE3 & lang = ja). Click ** Overview ** in the sidebar menu. A list of instances that are already running is displayed. Click ** Buy Instance ** to buy an instance from any region or proceed to the next step to create a new instance.

image.png

  1. Click ** Instances ** on the sidebar menu. Select the required region in the instance list and click Create Instance (https://www.alibabacloud.com/help/ja/doc-detail/92990.html) in the upper right bar.

image.png

  1. ** Product Purchase ** You will be redirected to the console so you need to select the package you want, but ** Quick Launch ** option (offers special price for ECS instance and data transfer), Or you can choose one of the ** Custom Launch ** options (which have two different pricing models). Specifically, you can choose to pay monthly or yearly, or pay-as-you-go, depending on your needs. In this tutorial, we chose ** Pay-As-You-Go ** as the billing method.

image.png

  1. In this window, select ** Datacenter Region ** and ** Zone ** to launch the ECS instance. If you select ** Region ** and not ** Zone **, the instance will be placed in a random zone.

image.png

  1. Now you need to select the ** instance type ** you want to create. Based on your requirements, select the type of generation for your instance from the ** Generation ** tab. Generation types represent different instance types based on the configuration and computing power used.

  2. Then select ** Network Type ** to launch your ECS instance. Depending on your needs, it can be either ** Classic Network ** or VPC (https://www.alibabacloud.com/en/product/vpc). In ** Classic Network **, Alibaba Cloud assigns IP addresses in a decentralized manner. Suitable for users who need simple and fast ECS. ** VPC ** is a logically isolated private network that supports dedicated connections. Suitable for users who are accustomed to more complex network management processes.

image.png

  1. Now you need to select ** Operating System **. Below each option is a list of different OS versions. Select the ** Ubuntu ** option.

image.png

  1. Select the ** System Disk ** type from the drop-down menu as needed. You can also click ** Add a disk ** to add a disk to this list.

  2. In the Security Settings section, you can create a password for added security.

image.png

  1. In the ** Purchase Plan ** section, enter a name for your instance and set the number of instances to launch.

image.png

  1. Review the configuration details and total price in the ** Overview ** section and click ** Buy Now **.

  2. Click ** Activate ** to confirm your order and launch your instance.

  3. Once the instance is launched, you can see it on the ** Instances ** tab of the console.

image.png

Install and deploy Django applications

[Alibaba Cloud Management Console](https://account.alibabacloud.com/login/login.htm?oauth_callback=https%3A%2F%2Fhome-intl.console.aliyun.com%2F%3Fspm%3Da2c65.11461447.0.0.28 Now that we have created and launched an ECS instance using a16cabOLprE3 & lang = ja), let's see how to install and deploy the Django application.

  1. Log in to the server using the SSH command.

image.png

  1. Enter your password.

image.png

  1. Create a new virtualenv and set up the environment for deploying your Django application.
mkvirtualenv DjangoApp 

Use deactivate to terminate the new virtualenv. You can now switch environments with workon. Use the workon command to load and switch virtualenv.

workon DjangoApp 
  1. Install Django in your current environment.
pip install Django
  1. Create a ** Sample Project ** with the django-admin command and change the directory to the project folder.
django-admin startproject todoApp
cd todoApp/
  1. Migrate or bootstrap the database.
python manage.py migrate 
  1. Create a superuser to access the admin panel.
python manage.py createsuperuser
  1. After configuring the user, test the application by running the runerver command, which is processed by manage.py.
python manage.py runserver 0.0.0.0:8000

You can see that it is executed on port 8000 as follows.

image.png

You can manage your application by going to your admin panel, ** / admin **.

image.png

Now use Nginx (https://www.nginx.com/?spm=a2c65.11461447.0.0.28a16cabOLprE3) to place the application behind the web server.

  1. Create a database schema and enable the Python environment. You can do this by following the steps below.

--Change directory to Django Project directory --Execute the following command

  python manage.py migrate
  1. Collect all static files, including CSS and JS files. You can do this by following the steps below. image.png

--Run the following command to collect all static files in a particular location.

Python manage.py collectstatic --noinput

--The developer is responsible for setting the STATIC_URL path to the location where all static files are collected. --These variables are defined in setting.py in the Project directory.

1、STATIC_URL 2、STATICFILES_DIRS 3、STATIC_ROOT

image.png

  1. Install the ** uwsgi ** library and use ** uwsgi server ** to start the server. You can do this by following the steps below.
 pip install uWSG

Create a ** ini ** file to use to deploy your django application.

 vim uwsgi.ini

image.png

--Save in ʻuwsgi.ini of ʻapplication dir. See ** Python / WSGI Application Quick Start ** for how to write an ini file. Execute the following command to start the application.

uwsgi uwsgi.ini (your ini file)
  1. Modify the nginx configuration file that provides 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; } } 
  1. After restarting nginx, the application is running behind nginx on port 80.

image.png

Conclusion

This article described how to deploy a Django application on Alibaba Cloud. In summary, the first step involved in doing this was to launch and run an ECS instance with Ubuntu as the OS, and then install and deploy the Django application on this instance. As part of the prerequisites for this tutorial, keep in mind that you must have completed the deployment and have a valid Alibaba Cloud account.

Below is a list of products related to this tutorial that will help you deploy your Django application in production scenarios.

Recommended Posts

How to deploy a Django application on Alibaba Cloud
How to deploy a web application on Alibaba Cloud as a freelancer
How to deploy a Django app on heroku in just 5 minutes
How to deploy a Streamlit application to GCP (GAE)
Deploy a Django application on Google App Engine (Python3)
How to build a Django (python) environment on docker
How to deploy a Go application to an ECS instance
How to run Django on IIS on a Windows server
How to run a Django application on a Docker container (development and production environment)
Deploy a Django application with Docker
Deploy your Django application on Heroku
How to deploy django-compressor on Windows
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
How to test on a Django-authenticated page
How to build an application from the cloud using the Django web framework
How to develop a cart app with Django
How to live a decent life on 2017 Windows
How to create a Rest Api in Django
A record of the time it took to deploy mysql on Cloud9 + Rails
How to install Fast.ai on Alibaba Cloud GPU and run it on Jupyter notebook
Deploy a Python 3.6 / Django / Postgres web app on Azure
(Python) Try to develop a web application using Django
How to install Linux on a 32bit UEFI PC
A memorandum on how to use keras.preprocessing.image in Keras
Try running a Django application on an nginx unit
How to use Django on Google App Engine / Python
Deploy a Django app made with PTVS on Azure
How to reference static files in a Django project
How to build a Python environment on amazon linux 2
How to set up a VPN gateway to establish a connection between Alibaba Cloud and AWS
How to call a function
How to register on pypi
How to hack a terminal
Deploy django project to heroku
How to use GitHub on a multi-person server without a password
How to deploy a web app made with Flask to Heroku
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
A memo on how to easily prepare a Linux exercise environment
How to run a trained transformer model locally on CloudTPU
How to build a new python virtual environment on Ubuntu
How to convert an array to a dictionary with Python [Application]
Don't lose to Ruby! How to run Python (Django) on Heroku
Encrypt ACME on Alibaba Cloud: Concepts Related to SSL Certificates
How to make a multiplayer online action game on Slack
How to deploy the easiest python textbook pybot on Heroku
How to mount a Windows 10 directory on Ubuntu-Server 20.04 on VMware Workstation 15
Setting up a CentOS 7 server hosted on Alibaba Cloud ECS
A note on how to load a virtual environment in PyCharm
How to generate a query using the IN operator in Django
How to register a package on PyPI (as of September 2017)
To deploy Java application on VPS (Apache / Tomcat installation / linkage)
How to connect to Cloud SQL PostgreSQL on Google Cloud Platform from a local environment with Java
[Code Pattern] How to deploy a sample currency app to OpenShift on IBM Cloud using Red Hat Universal Base Image (UBI)
How to make a Japanese-English translation
How to install OpenCV on Cloud9 and run it in Python
[Django] How to test Form [TDD]
How to put a symbolic link
A note on how to check the connection to the license server port
How to install mysql-connector-python on mac
Steps to create a Django project
How to use Dataiku on Windows