Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Deploying Django

Updated on 2024-12-09 GMT+08:00

Introduction

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This tutorial describes how you can use Nginx and uWSGI to deploy the Django project in HCE 2.0.

Preparations

  • Prepare an ECS and assign a public IP address or EIP to the ECS.
  • Ensure that inbound security group rules allow traffic to flow to the ECS over ports 80, 8001, and 8002.

Procedure

  1. Install Nginx.

    1. Run the following command to install Nginx:
      dnf install nginx
    2. Run the following command to start Nginx:
      systemctl start nginx
    3. Run the following command to check the Nginx status:
      systemctl status nginx

      If active (running) is displayed, Nginx is started.

  2. Install uWSGI.

    1. Run the following command to install the uWSGI dependency:
      dnf install python3-devel gcc
    2. Run the following command to install uWSGI:
      pip install uwsgi
    3. Run the following command to check the uWSGI version:
      uwsgi --version
    4. Edit the hello.py file and enter the following information:
      def application(env, reply):
          reply('200 ok',[('Content-Type','text/html')])
          return [b"Hello!"]
    5. Run the following command to start uWSGI:
      uwsgi --http :8001 --wsgi-file hello.py
    6. Enter http://<Public IP address>:8001 in the address box of the browser to access uWSGI.

  3. Install the Django environment.

    1. Run the following command to install Django:
      pip install Django
    2. Run the following command to initialize the project:
      python -m django startproject django_project
    3. Go to the project directory, edit the django_project/settings.py configuration file, and change the value of ALLOWED_HOSTS to the following:
      ALLOWED_HOSTS = ["*"]
    4. Run the following command to start Django:
      python manage.py runserver 0.0.0.0:8002
    5. Enter http://<Public IP address>:8002 in the address box of the browser to access Django.

  4. Configure the environment.

    1. Edit the django_project/settings.py file.
      1. Enter the following statement at the beginning of the file to import the OS library:
        import os
      2. Add the following parameter to the end of the file:
        STATIC_ROOT = os.path.join(BASE_DIR, "static/")
      3. Run the following command to collect all static files:
        sudo python manage.py collectstatic

        Information similar to the following is displayed:

        [root@hce2 django_project]# sudo python manage.py collectstatic
        
        125 static files copied to '/root/django_project/static'.

        A static directory is added to the project directory. The project structure is as follows:

        [root@hce2 django_project]# ls
        db.sqlite3  django_project  manage.py  static
    2. Edit the /etc/nginx/nginx.conf file to configure Nginx.
      1. Find the http attribute and add the following content:
        upstream django {
            server 127.0.0.1:8001;
        }
      2. Find the server attribute under http and change it to the following:
        server {
            listen       80;
            server_name  django_project;
         
            charset      utf-8;
            location  /static {
                autoindex on;
                alias /root/django_project/static;
            }   
            location / {
                uwsgi_pass 127.0.0.1:8001;
                include uwsgi_params;
                include /etc/nginx/uwsgi_params;
                uwsgi_param UWSGI_SCRIPT iCourse.wsgi;
                uwsgi_param UWSGI_CHDIR /iCourse;
                index index.html index.htm;
                client_max_body_size 35m;
                index index.html index.htm;
        }

        The following is displayed.

    3. Create the uwsgi_config.ini file in the project directory and enter the following:
      [uwsgi]
      socket = 127.0.0.1:8001 # Port 8001 must be the same as the uwsgi_pass port defined in the Nginx configuration file.
      chdir = /root/django_project/ # Specify the project directory. In this example, the directory is /root/django_project/. Modify it based on the project.
      wsgi-file = django_project/wsgi.py # Specify the Django's wsgi file and modify it based on the project.
      processes = 4 # Maximum number of working processes
      threads = 2 # Number of threads started after each working process is started
      vacuum = true # Automatic cleanup upon environment exit
      buffer-size = 65536 # Set the size of the internal buffer for parsing uwsgi packets to 64 KB. The default value is 4 KB.
      CAUTION:

      In the actual environment, delete the logs in the configuration file.

  5. Verify Django.

    1. Run the following command to restart Nginx:
      systemctl restart nginx
    2. Run the following command in the project directory to start uWSGI:
      uwsgi --ini uwsgi_config.ini
    3. Enter http://<Public IP address> in the address box of the browser to access the Django page.

CAUTION:

The preceding configuration is used only for tests. Exercise caution when using the configuration in the service environment.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback