Publish CRM You will use the following software
- nginx
- uWSGI
- CentOS7
- CRM Project File
- virtualenv
- supervisor
WSGI,uWSGI
Pthon web server development uses WSGI protocol (Web Server Gateway Interface)
The python web project generates a wsgi.py file by default, identifying the application module.
The production environment uses uWSGI, implements all WSGI interfaces, is written in C language, and is a highly efficient web server.
UWSGI is a full-featured HTTP server that implements WSGI protocol, uwsgi protocol, http protocol, etc.All it has to do is convert the HTTP protocol into a language-supported network protocol.For example, convert the HTTP protocol to WSGI so that Python can use it directly.
Nginx
nginx is used for its reverse proxy function, and the project is deployed online through Django+uWSGI+Nginx.
CentOS
1. Package project CRM folders, compress files
2. Upload files to Centos server via xftp, scp, lrzsz, etc.
Linux tips
1. Use software like xshell or iTerm to operate your linxu in multiple terminals, so that when debugging uwsgi, nginx, and project code, you can avoid switching directories back and forth to provide efficiency.
2. Note that the configuration file of the linux software has been modified, and the service must be restarted before it takes effect.
Virtualenv
It is recommended to build a clean, isolated python interpreter environment to prevent software dependency, conflicts, etc.
Supervisor
Supervisor (http://supervisord.org/) is a client/server service developed with Python, a process management tool under Linux/Unix system, and does not support Windows system.It can easily monitor, start, stop, and restart one or more processes.Supervisor manages processes that, when a process is accidentally killed, supervisort automatically pulls it up again when it hears that the process is dead. It is very convenient to achieve the function of automatic process recovery, and no longer needs to write a shell script to control it.
Project Deployment
Activate virtual python environment
#Create python3-based virtual interpreter environment venv virtualenv --no-site-packages --python=python3 venv #Activate python3 virtual environment [root@yugo /data 11:11:30]#source venv/bin/activate (venv) [root@yugo /data 11:11:35]#
Install uwsgi
(venv) [root@yugo /data 11:13:23]#pip3 install uwsgi
Configure the boot uwsgi.ini to start with this profile when uwsgi is started
(venv) [root@yugo /data 11:14:25]#cat uwsgi.ini [uwsgi] #Use when using nginx connections socket=0.0.0.0:8000 #Use directly as a web server without nginx #http=0.0.0.0:9000 #Absolute path to project directory chdir=/data/Ace_crm
#wsgi file path, under Project wsgi-file=Ace_crm/wsgi.py #Specify interpreter directory home=/data/venv processes=4 threads=2 master=True pidfile=uwsgi.pid daemonize=uwsgi.log
Configure nginx
Configure nginx.conf to drop requests to django processing through the nginx reverse proxy
(venv) [root@yugo /data 11:20:32]#cat /opt/nginx1-12/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; 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 logs/access.log main; sendfile on; keepalive_timeout 65; #Define a load balancing pool, called django, in which the socket address of the uwsgi Django is written upstream django { server 0.0.0.0:8000; } server { listen 80; server_name pythonav.cn; #When accessing the root path of nginx, forward the request to port 8000 of uwsgi, as written by uwsgi.ini location / { include /opt/nginx1-12/conf/uwsgi_params;
#Request forwarding to uwsgi program in upstream address pool uwsgi_pass django; } location /static/ { alias /opt/nginx1-12/html/static/; } } }
Hot-load nginx service, read nginx.conf content
(venv) [root@yugo /data 11:24:24]#/opt/nginx1-12/sbin/nginx -t nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful (venv) [root@yugo /data 11:26:07]#/opt/nginx1-12/sbin/nginx -s reload
Start uwsgi, start django
(venv) [root@yugo /data 11:26:54]#uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini (venv) [root@yugo /data 11:27:10]#ps -ef|grep uwsgi root 15540 1 0 11:27 ? 00:00:00 uwsgi uwsgi.ini root 15543 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini root 15544 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini root 15545 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini root 15546 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini root 15590 11958 0 11:27 pts/0 00:00:00 grep --color=auto uwsgi
#If you need to stop uwsgi have access to ps -ef|grep uwsgi,find pid Kill
#A better way to kill uwsgi
killall -9 uwsgi
Visit port 80 of nginx to see if the request is forwarded to django
http://pythonav.cn/login/ //Or 10.0.0.10/login
Configure static resources for nginx
Why configure static resources?
Configuring the static resource directory is because static resources can be returned directly through nginx without requiring uwsgi, which means that uwsgi only processes the end logic, does not process static resources, and optimizes performance
Configure static resources, django and nginx
#Create a static resource store directory [root@yugo /opt/nginx1-12/html 11:39:51]#mkdir -vp /opt/nginx1-12/html/static mkdir: created directory '/opt/nginx1-12/html/static'
#Add permissions to directories
[root@yugo /opt/nginx1-12/html 11:40:57]#chmod 755 /opt/nginx1-12/html/static/
Configure settings.py for django
DEBUG = False ALLOWED_HOSTS = ['*']
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT= '/opt/nginx1-12/html/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
Split Line--
Collect django static files
python3 manage.py collectstatic
This statement copies all static files previously placed in static under app to the STATIC_ROOT folder set in settings.py
Requesting a static resource then goes to location/static {alias/opt/nginx1-12/html/static/} of the nginx configuration to find it
After the above steps are completed, access the server host address and port. If port 80 is configured in nginx.conf, the address bar does not need to enter a port, because the browser request port is also defaulted to port 80, and non-80 ports need to be added after ip