Deploy code to Django-based servers (Tencent Cloud)

Posted by Atomic Taco on Tue, 21 Sep 2021 18:23:49 +0200

Note: This article is my own note, no logic, just for myself

Essential information:

_System: Cent OS 7
_Server: Tencent Cloud
Django Official Website

One: Some linux commands used

  • Use django-admin to create a Django project
django-admin startproject mysite
#This mysite is the project name

  • Start Server
python runserver 8000

Note that you want to execute it in the MySite directory, for example, cd mysite, and execute the above statement

  • Enter mysql database
mysql -u root -p
  • See
#Used to display tcp, udp ports and processes, etc.
netstat -tunlp
-t (tcp)Show Only tcp Related Options
-u (udp)Show Only udp Related Options
-n Refuse to display aliases, convert all numbers to numbers
-l Only listed are Listen(Monitor)Service Status
-p Show the name of the program that made the relevant link
-a Lists all service states, connected by default
#View process port number and running programs
netstat -atunp

#View process id from port number port(8000)
netstat -anp |grep 8000

  • Stop it
#Kill the specified process according to PID (process id): 
kill pid
#Force kill specified process according to PID (process id): 
kill -9 pid

  • View nginx processes in the system
ps -ef|grep nginx

2. Common problems and Solutions

  • nginx: [error] invalid PID number "" in "/run/"

Execution of nginx-t is OK after server restart, but error occurred while executing nginx-s reload

nginx: [error] invalid PID number "" in "/run/"


Need to execute first

nginx -c /etc/nginx/nginx.conf

The path to the nginx.conf file can be found in the return of nginx -t.

nginx -s reload

3. File Path

  • Related file directories

Project path: /root/mysite/

[root@VM-8-3-centos ~]# cd /root/mysite
[root@VM-8-3-centos mysite]# tree
├── db.sqlite3
├── mysite
│   ├──
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├──
│   ├──
│   └──
└── mysite.ini

_Nginx path: /etc/nginx

4. Configuration and Code

1. [uwsgi] Configuration

Create a mysite.ini configuration file in the root directory of the project mysite, with a customizable file name
Path to mysite.ini file:
Execute statement:

uwsgi --ini mysite.ini
# Django-related settings
socket= :8000

# the base directory (full path)

# Django s wsgi file

# process-related setttings
# master


# maximum number of worker processes

# ... with appropriate permissions - may be needed
# chmod-socket  = 664
# clear environment on exit

  • Other
    When you do this, you've done it before completing your uWSGI server detection
    Detect if uWSCI is functioning properly:
    After completing the code configuration (mainly modifying the database, etc.), enter the uwsgi command in the CentOS7 system to test whether the uWSGI server can function properly, as follows:
# /root/mysite is the absolute address of the project MySite
# mysite.wsgi is the file for the project mysite
uwsgi --http :8000 --chdir /root/mysite -w mysite.wsgi

After the command runs, you can enter the IP address of the virtual system + port 8000 in the browser of the local system to view the test results.htttp://, we can see the information on the first page of the project mysite. (Note: is the server's public ip)

2.Nginx Deployment Project

Edit/etc/nginx/nginx.conf file

# For more information on configuration, see:
#   * Official English Documentation:
#   * Official Russian Documentation:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # set up the Nginx server configuration of the project mysite
    server {
	listen 80;
	charset utf-8;
	client_max_body_size	75M;
	# configure media resoure files
	location /mysite {
		expires 30d;
		autoindex on;
		add_header Cache-Control private;
		alias /root/mysite/mysite/;
	# configure static resource files
	location /static {
		expires 30d;
		autoindex on;
		add_header Cache-Control private;
		alias /root/mysite/static/;
	# configure uWSGI server
	location / {
		include uwsgi_params;
		uwsgi_read_timeout 2;



# Settings for a TLS enabled server.
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }


  • Then execute under / etc/nginx/directory:
nginx -t

Test configuration for syntax issues

  • If the syntax is okay, you can restart the nginx server:
nginx -s reload

5. Deployment process

With no problem with source code data changes (mainly database configurations, etc.) for sites such as, deploying the site to a server requires only uwsgi and nginx.
With the code and configuration of uwsgi and nginx written, execute the following command to start the service:

# ->Nginx section
# Reread Configuration File
nginx -t
# If nginx: [error] invalid PID number "" in "/run/" appears
# Execute nginx-c/etc/nginx/nginx.conf
nginx -s reload

# ->uWSGI section
# Start uWSGI server
cd /root/mysite/
uwsgi --ini mysite.ini

Topics: Python Django Nginx