Django builds SERVER background

This paper takes the alicloud lightweight application server of Ubuntu 18.04 as an example Stacklens' open source project

Remote connection server

Use MobaXterm SSH to connect to Alibaba cloud server. Enter the account and password according to the prompt. After entering successfully, you can see Alibaba cloud's welcome interface.

After deployment to the server, you can't use the background server provided by Django, but choose to use the network services provided by Nginx and Gunicorn.

  • The client sends an http request, and Nginx analyzes the http request as a direct external server interface;
  • If it is a static resource request, it is handled by Nginx itself (very efficient);
  • If it is a dynamic resource request, it is forwarded to Gunicorn for preprocessing, and then forwarded to Django to finally complete the return of resources.

Installing anaconda environment

First, install the anaconda environment (using virtualenv is also OK)

bash -bfp /usr/anaconda

After installation, use the conda command to check whether the installation is successful
If conda command not found appears
Use the following command

vim /etc/profile

Press the i key to enter the edit mode
Add the following content at the bottom of the file, where / usr/anaconda/bin is the location where anaconda is installed


Then press Ctrl + c and enter: wq Save Profile
Enter the following command to reload the configuration file

source /etc/profile
conda create -name venv python=3.8
source activate venv

Install nginx and git

apt-get install nginx
apt-get install git

After all are successful, create and enter the project directory

mkdir -p /home/sites/myblog
cd /home/sites/myblog

Next, download the project from Github

git clone

After downloading, enter the project directory, install the dependency library, collect static resources and migrate the database:

cd django-vue-tutorial
pip3 install -r requirements.txt
python3 collectstatic
python3 migrate
python3 runserver

Finally, start nginx

service nginx start

Next, enter your public ip in the browser and see the welcome interface of Nginx, which is half the success.

Configure nginx

Open profile

vim /etc/nginx/sites-available/myblog

Write configuration information with vim:

server {
  charset utf-8;
  listen 80;
  server_name XXX.XXX.XXX.XXX;  # Change to your IP

  # Define the root path of the server
  # Modify the path to your project
  root /home/sites/myblog/django-vue-tutorial;

  # The following four items are configuring forwarding paths for static resources
  # Note that the path name must be correct
  # Especially the horizontal line - and underline_ Don't mix it up
  location /static {
    alias /home/sites/myblog/django-vue-tutorial/collected_static;

  location /media {
    alias /home/sites/myblog/django-vue-tutorial/media;

  location /js {
    alias /home/sites/myblog/django-vue-tutorial/collected_static/js;

  location /css {
    alias /home/sites/myblog/django-vue-tutorial/collected_static/css;

  # Forward the interface and background request to Gunicorn
  location ~ (^/api|^/admin)  {
    proxy_set_header Host $host;
    proxy_pass http://unix:/tmp/XXX.XXX.XXX.XXX.socket;  #  Change to your IP

  # All other requests directly request Vue's packaged html file
  location / {
    try_files /collected_static/index.html =404;

Link profile to available profiles

ln -s /etc/nginx/sites-available/myblog /etc/nginx/sites-enabled

Configure Gunicorn

pip3 install gunicorn
# Restart nginx
service nginx restart
# Change XXX.XXX.XXX.XXX to your own public ip address
gunicorn --bind unix:/tmp/XXX.XXX.XXX.XXX.socket drf_vue_blog.wsgi:application

Enter the public ip address of your server in the browser to see:

Tags: Django server

Posted on Fri, 26 Nov 2021 01:29:54 -0500 by gazolinia