1. Virtualenv installation
pip3 install virtualenv
mkdir My
cd My
virtualenv –no-site-packages –python=python3 venv1 #Create an independent environment and specify that the interpreter is python3
source venv1/bin/activate #Enter the virtual environment at this time (venv1)
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
deactivate
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
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
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
( venv1) [root@localhost ~]# pip3 install uwsgi
# 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
uwsgi –ini /opt/My/venv1/uwsgi.ini
4. Install and configure nginx
yum install -y nginx
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
}
}
}
/usr/sbin/nginx
5. Install and configure supervisor
pip3 install supervisor # Before you need python2 environment to install, now you can install it directly with pip3
echo_supervisord_conf > /etc/supervisord.conf
[ 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
supervisord -c /etc/supervisord.conf #Start supervisor
supervisorctl -c /etc/supervisord.conf #Enter the supervisor interactive interface
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