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
- 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 the uWSGI dependency:
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.
- Run the following command to install the uWSGI dependency:
- 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, edit the django_project/settings.py configuration file, and change the value of ALLOWED_HOSTS to the following:
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.
- Run the following command to install Django:
- Configure the environment.
- Edit the django_project/settings.py file.
- Enter 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
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
- Enter the following statement at the beginning of the file to import the OS library:
- Edit the /etc/nginx/nginx.conf file to configure Nginx.
- Find the http attribute and add the following content:
upstream django { server 127.0.0.1:8001; }
- 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.
- Find the http attribute and add the following content:
- 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.
- 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 to start uWSGI:
uwsgi --ini uwsgi_config.ini
- Enter http://<Public IP address> in the address box of the browser to access the 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