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

Deploying Django

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.

      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.

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