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
- Install Nginx.
- Run the following command to install Nginx:
dnf install nginx
- Run the following command to start Nginx:
systemctl start nginx
- Run the following command to check the Nginx status:
systemctl status nginx
If active (running) is displayed, Nginx is started.
- Run the following command to install Nginx:
- Install uWSGI.
- Run the following command to install uWSGI dependencies:
dnf install python3-devel gcc
- Run the following command to install uWSGI:
pip install uwsgi
- Run the following command to check the uWSGI version:
uwsgi --version
- Edit the hello.py file and enter the following information:
def application(env, reply): reply('200 ok',[('Content-Type','text/html')]) return [b"Hello!"]
- Run the following command to start uWSGI:
uwsgi --http :8001 --wsgi-file hello.py
- Enter http://<Public IP address>:8001 in the address box of the browser to access uWSGI.
Figure 1 Accessing the uWSGI service
- Run the following command to install uWSGI dependencies:
- Install the Django environment.
- Run the following command to install Django:
pip install Django
- Run the following command to initialize the project:
python -m django startproject django_project
- 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 = ["*"]
- Run the following command to start Django:
python manage.py runserver 0.0.0.0:8002
- Enter http://<Public IP address>:8002 in the address box of the browser to access Django.
Figure 2 Accessing the Django service
- Run the following command to install Django:
- Configure the environment.
- Edit the django_project/settings.py file.
- Add the following statement at the beginning of the file to import the OS library:
import os
- Add the following parameter to the end of the file:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
- 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
- Add the following statement at the beginning of the file to import the OS library:
- Edit the /etc/nginx/nginx.conf file to configure Nginx.
- Locate http and add the following content:
upstream django { server 127.0.0.1:8001; }
- 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; }
- Locate http and add the following content:
- 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)
- Edit the django_project/settings.py file.
- Verify Django.
- Run the following command to restart Nginx:
systemctl restart nginx
- Run the following command in the project directory django_project to start uWSGI:
uwsgi --ini uwsgi_config.ini
- Enter http://<Public IP address> in the address box of the browser to access Django.
Figure 4 Django page
- Run the following command to restart Nginx:

The preceding configuration is used only for tests. Exercise caution when using the configuration in the service environment.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot