Updated on 2025-07-29 GMT+08:00

Deploying Django

Introduction

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This section describes how to use Nginx and uWSGI to deploy a 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.

Prerequisites

A yum repository has been configured. For details about how to configure a yum repository accessed over the Internet, see Configuring Repositories and Installing Software for HCE.

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 uWSGI dependencies:
      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.
      Figure 1 Accessing the uWSGI service

  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 django_project, edit the django_project/settings.py configuration file, and change the value of ALLOWED_HOSTS as follows:
      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.
      Figure 2 Accessing the Django service

  4. Configure the environment.

    1. Edit the django_project/settings.py file.
      1. Add 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

        For example, the command output is as follows:

        [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 django_project. 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. Locate http and add the following content:
        upstream django {
            server 127.0.0.1:8001;
        }
      2. Locate server under http and configure it as follows (the result is shown in Figure 3):
        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;
        }
        Figure 3 Example
    3. Create the uwsgi_config.ini file in the project directory django_project and enter the following content:
      [uwsgi]
      socket = 127.0.0.1:8001
      chdir = /root/django_project/
      wsgi-file = django_project/wsgi.py
      processes = 4
      threads = 2
      vacuum = true
      buffer-size = 65536
      • socket: address and port to be listened on. The port 8001 in the example must be the same as the uwsgi_pass port defined in the Nginx configuration file.
      • chdir: project directory. In this example, the project directory is /root/django_project/. Change it based on the project requirements.
      • wsgi-file: Django's wsgi file. Change it based on project requirements.
      • processes: maximum number of worker processes
      • threads: number of threads started after each worker process is started
      • vacuum: automatically clears the environment when exiting
      • buffer-size: size of the internal buffer for parsing uWSGI packages (value: 64 KB; default: 4 KB)

  5. Verify Django.

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

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