Tutorial on deploying nginx+uwsgi in Django project under Centos8

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation

1. Install virtualenv

pip3 install virtualenv

2. Create a directory and transfer the project files

mkdir My
cd My

3. Create an independent operating environment-naming

virtualenv --no-site-packages --python=python3 venv1 #Create an independent environment and specify the interpreter as python3

4. Enter the virtual environment

source venv1/bin/activate #Enter the virtual environment (venv1)

5. Install third-party libraries in the virtual environment and import the required environment (export command: pip3 freeze > packages.txt)

pip3 install django==2.11 #At this time, all pip3 packages will be installed in the venv1 environment, which is created for Myproject
pip3 install -r packages.txt

6. Exit venv1 environment

deactivate

7. How does virtualenv create an “independent” Python runtime environment? The principle is very simple, just copy the system Python to the virtualenv environment,
When you use the command source venv/bin/activate to enter a virtualenv environment, virtualenv will modify the relevant environment variables so that the commands python and pip both point to the current virtualenv environment.

2. Django configuration

1.settings.py

DEBUG = False #debug changed to false

ALLOWED_HOSTS = ['*'] # Change the access address to "*" to indicate all STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static")
 #The directory accessed by nginx is placed in the previous static directory. You can customize the absolute path STATIC_URL = '/static/' 
STATICFILES_DIRS = [os.path.join(BASE_DIR,"static"),]

MEDIA_URL = '/archive/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'archive') 
#Static files uploaded by users, such as avatars

After the configuration is complete, run python manage.py collectstatic to load static files to the STATIC_ROOT directory.

2.urls.py

from django.urls import path, re_path
from django.conf import settings
from django.views.static import serve
 
urlpatterns = [
   re_path(r'^archive/(?P<path>.*)$', serve, 
{'document_root': settings.MEDIA_ROOT}, name='archive'), #path of the file uploaded by the user('favicon.ico', serve,{'path': 'img/favicon.ico','document_root':settings.STATIC_ROOT}),
 #Global favicon.ico icon]

3. Install and configure uwsgi

1. Enter the virtual environment venv1 and install uwsgi (it is best to install it outside the virtual environment)

(venv1) [root@localhost ~]# pip3 install uwsgi

2. Configure the startup file (you can put it in any directory, I put it in venv1)
uwsgi supports multiple configuration methods such as ini and xml. This article takes ini as an example. Create a new uwsgi.ini in the /etc/ directory and add the following configuration:

 #Add configuration options [uwsgi]
 #Configure the socket connection to nginx socket=127.0.0.1:8000
 #http=0.0.0.0:8000 #http connection #Configure the project path, the directory where the project is located chdir = /opt/My/Myproject

 #Configure the wsgi interface module file path, that is, the directory name where the wsgi.py file is located wsgi-file = Myproject/wsgi.py
 #Configure the number of processes started processes=4
 #Configure the number of threads for each process threads=2
 #Configure the startup management master process master=True
 #Virtual environment directory home=/opt/My/venv1
 #Configure the process number file for storing the main process (I commented it out, it is said to conflict with the supervisor log)
 #pidfile=uwsgi.pid

 #Configure dump logging (same as above)
 #daemonize=uwsgi.log

3. Start by specifying a configuration file

uwsgi --ini /opt/My/venv1/uwsgi.ini

4. Install and configure nginx

1. Install nginx on centos8 (direct yum installation)

yum install -y nginx

2. Configure nginx.conf

 user nginx;
 worker_processes 2; #Number of processes error_log /var/log/nginx/error.log;
 pid /run/nginx.pid;

 # 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 2048;

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

 # Load modular configuration files from the /etc/nginx/conf.d directory.
 # See http://nginx.org/en/docs/ngx_core_module.html#include
 # for more information.
 #include /etc/nginx/conf.d/*.conf;

 server {
 listen 80;#Listen port#listen [::]:80 default_server;
 server_name 192.168.3.119;# Domain name or IP
 #root /usr/share/nginx/html;

 # Load configuration files for the default server block.
 #include /etc/nginx/default.d/*.conf;
 charset utf-8;

 location /static {
 alias /opt/My/static; #Static file address (STATIC_ROOT)

 }

 location / {
 include uwsgi_params;
 uwsgi_pass 0.0.0.0:8000; #project port number uwsgi_param UWSGI_SCRIPT Myproject.wsgi; #project wsgi.py directory uwsgi_param UWSGI_CHDIR /opt/My/Myproject; #project directory}

 }

 }

3. Start nginx

/usr/sbin/nginx

5. Install and configure supervisor

1. Install supervisor

pip3 install supervisor # Previously, you needed a python2 environment to install it. Now you can install it directly with pip3

2. Generate configuration files to the etc directory through commands (can be customized)

echo_supervisord_conf > /etc/supervisord.conf

3. Add the following code at the end of the configuration file

[program:myname] #Task name command=/opt/my/venv1/bin/uwsgi --ini /opt/my/venv1/uwsgi.ini
 #The command executed runs uwsgi. uwsgi is in the virtual environment [program:nginx] 
 command=/usr/sbin/nginx #Run nginx

4. Start supervisor

 supervisord -c /etc/supervisord.conf #Start supervisor
 supervisorctl -c /etc/supervisord.conf #Enter the supervisor interactive interface

5. Supervisor Command

 start myname #Start\
 stop myname #Stop>> You can write the task name or all to indicate all restart myname #Restart/

This is the end of this article about the tutorial on how to deploy nginx+uwsgi in Django project under Centos8. For more relevant content about deploying nginx+uwsgi in Django project, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the idea of ​​deploying Nginx+Uwsgi+Django project to the server
  • Django project uwsgi+Nginx nanny-level deployment tutorial implementation
  • Example of deploying Django project with uwsgi+nginx
  • Django 8.5 project deployment - Nginx

<<:  JavaScript MouseEvent Case Study

>>:  Problems encountered when installing mysql8.0.15 winx64 on Win10 and connecting to the server

Recommend

Example of how to enable Brotli compression algorithm for Nginx

Brotli is a new data format that can provide a co...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

The 6 Most Effective Ways to Write HTML and CSS

This article shares the 6 most effective methods,...

Graphical explanation of the underlying principle of JavaScript scope chain

Table of contents Preface Scope 1. What is scope?...

Detailed explanation of MySQL InnoDB index extension

Index extension: InnoDB automatically extends eac...

Solve the abnormal error when building vue environment with webpack

Table of contents First, configure package.json T...

CSS beginner tutorial: background image fills the entire screen

If you want the entire interface to have a backgr...

Javascript File and Blob Detailed Explanation

Table of contents File() grammar parameter Exampl...

How to use nginx to access local static resources on Linux server

1. Check whether port 80 is occupied. Generally, ...

harborRestart operation after modifying the configuration file

I won't say much nonsense, let's just loo...

MySQL Series 10 MySQL Transaction Isolation to Implement Concurrency Control

Table of contents 1. Concurrent access control 2....

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

How to implement image mapping with CSS

1. Introduction Image maps allow you to designate...

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...