Tutorial for deploying nginx+uwsgi in django project under Centos8

1. Virtualenv installation

  1. Install virtualenv

pip3 install virtualenv

  1. Create a directory and transfer the project files

mkdir My
cd My

  1. Create a standalone environment-naming

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

  1. Enter the virtual environment

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

  1. 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, pip3 packages will be installed in the venv1 environment, and venv1 is created for Myproject
pip3 install -r packages.txt

  1. Exit venv1 environment

deactivate

  1. How does virtualenv create a "standalone" Python runtime environment? The principle is very simple, that is, copy the system Python to the virtualenv environment,
    When using 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 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 "*"Means 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 to write
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

  1. urls.py
from django.urls import path,re_path
from django.conf import settings
from django.views.staticimport serve
 
urlpatterns =[   re_path(r'^archive/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}, name='archive'), #User uploaded files
 path('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, install uwsgi (it is best to install it outside the virtual environment)

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

  1. Configure the startup file (you can put it in any directory, I put it under venv1)
    uwsgi supports multiple configuration methods such as ini, xml, etc. 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 wsgi interface module file path,Which is wsgi.py the name of the directory where the file is located
 wsgi-file = Myproject/wsgi.py
 # Configure the number of processes started
 processes=4
 # Configure the number of threads per process
 threads=2
 # Configuration start management main process
 master=True
 # Virtual environment directory
 home=/opt/My/venv1
 # Configure the process number file of the main process (I commented, it is said to conflict with the supervisor log)
 # pidfile=uwsgi.pid

 # Configure dump logging (same as above)
 # daemonize=uwsgi.log 
  1. Specify the configuration file to start

uwsgi –ini /opt/My/venv1/uwsgi.ini

4. Install and configure nginx

  1. Install nginx on centos8 (direct yum installation)

yum install -y nginx

  1. 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;#Listening 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
 }

 }

 }
  1. Start nginx

/usr/sbin/nginx

5. Install and configure supervisor

  1. Install supervisor

pip3 install supervisor # Before you need python2 environment to install, now you can install it directly with pip3

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

echo_supervisord_conf > /etc/supervisord.conf

  1. 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 executed command runs uwsgi. uwsgi is in the virtual environment

 [ program:nginx] 
 command=/usr/sbin/nginx #Run nginx
  1. Start supervisor
 supervisord -c /etc/supervisord.conf #Start supervisor
 supervisorctl -c /etc/supervisord.conf #Enter the supervisor interactive interface
  1. supervisor command
 start myname #start up\
 stop myname #stop>>You can write the task name or all means all
 restart myname #Reboot/

So far, this article about the tutorial on django project deployment nginx+uwsgi under Centos8 is introduced. For more related django project deployment nginx+uwsgi content, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope everyone Support ZaLou.Cn a lot in the future!

Recommended Posts

Tutorial for deploying nginx+uwsgi in django project under Centos8
Tutorial diagram for installing zabbix2.4 under centos6.5
Graphical tutorial for installing JDK1.8 under CentOS7.4
Install Mesos tutorial under CentOS7
Detailed steps for installing Django under Python 3.6 in Ubuntu 16.04 environment
Minimal install JDK 1.8 tutorial in CentOS 7
Centos8 uses Docker to deploy a detailed tutorial for Django projects
Use NTP for time synchronization in CentOS 7
Deploy django application in ubuntu through nginx+uwsgi
Jenkins installation and deployment tutorial under CentOS 7
Set static IP for CentOS in VMware
Complete deployment record for LDAP under Centos7.2
Detailed tutorial on installing MySQL 8 in CentOS 7
Graphical tutorial for installing Pycharm 2020.1 in Ubuntu 20.04
Install Docker CE in yum under CentOS 7
The most complete centos installation tutorial in history
Install nginx in centos8 custom directory (detailed tutorial)
Install MySQL 5.7 under CentOS 7 for middle class children!
CentOS8 detailed tutorial for configuring local yum source
Build Discuz Forum in LNMP Environment under CentOS7
Build LEMP (Linux+Nginx+MySQL+PHP) environment under CentOS 8.1 (detailed tutorial)
Build Dedecms website in LNMP environment under CentOS7