centos7下采用Nginx+uwsgi来部署django

时间:2021-07-10 21:58:32

之前写过采用Apache和mod_wsgi部署django,因为项目需要,并且想比较一下Nginx和Apache的性能,尝试采用Nginx+uwsgi的模式来部署django。

1、安装uwsgi以及Nginx

1 pip install uwsgi                          --目前的版本为2.0.15
2 yum install epel-release
3 yum install nginx* --目前的版本为1.10.2

2、测试

1 # test.py
2 def application(env, start_response):
3 start_response('200 OK', [('Content-Type','text/html')])
4 return "Hello World"
1 uwsgi --http :80 --wsgi-file test.py

采用浏览器访问主机或者采用postman模拟get操作,如果出现"Hello World"即测试成功

3、测试django工程

1 django-admin startproject myself
2 uwsgi --http :80 --chdir /home/myself --module myself.wsgi

并在django工程的settings里边添加主机的ip地址,采用浏览器访问主机或者采用postman模拟get操作,如果出现django的欢迎页面,“It works!”即测试成功

4、写uwsgi.ini配置文件

 1 #uwsgi.ini file
2 [uwsgi]
3
4 # Django-related settings
5 # the base directory (full path)
6 chdir = /home/myself
7 # Django's wsgi file
8 module = myself.wsgi:application
9 # the virtualenv (full path)
10 #home = /path/to/virtualenv
11
12 # process-related settings
13 # master
14 master = true
15 # maximum number of worker processes
16 processes = 3
17 # the socket (use the full path to be safe)
18 #socket = /home/myself/myself.sock
19 socket = 127.0.0.1:8001
20 # ... with appropriate permissions - may be needed
21 chmod-socket = 666
22 chown-socket = nginx:nginx
23 # clear environment on exit
24 vacuum = true
25 enable-threads = true

5、修改nginx配置文件

1 vim /etc/nginx/nginx/conf
  1 # For more information on configuration, see:
2 # * Official English Documentation: http://nginx.org/en/docs/
3 # * Official Russian Documentation: http://nginx.org/ru/docs/
4
5 user nginx;
6 worker_processes auto;
7 error_log /var/log/nginx/error.log;
8 pid /run/nginx.pid;
9
10 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
11 include /usr/share/nginx/modules/*.conf;
12
13 events {
14 worker_connections 1024;
15 }
16
17 http {
18 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19 '$status $body_bytes_sent "$http_referer" '
20 '"$http_user_agent" "$http_x_forwarded_for"';
21
22 access_log /var/log/nginx/access.log main;
23
24 sendfile on;
25 tcp_nopush on;
26 tcp_nodelay on;
27 keepalive_timeout 65;
28 types_hash_max_size 2048;
29
30 include /etc/nginx/mime.types;
31 default_type application/octet-stream;
32
33 # Load modular configuration files from the /etc/nginx/conf.d directory.
34 # See http://nginx.org/en/docs/ngx_core_module.html#include
35 # for more information.
36 include /etc/nginx/conf.d/*.conf;
37
38 upstream django {
39 #server unix:/home/myself/myself.sock; # for a file socket
40 server 127.0.0.1:8001; # for a web port socket (we'll use this first) ---socket
41 }
42
43 server {
44 listen 80 default_server;
45 listen [::]:80 default_server;
46 server_name _;
47 root /usr/share/nginx/html;
48
49 # Load configuration files for the default server block.
50 include /etc/nginx/default.d/*.conf;
51
52 location /static/ {
53 root /home/myself; ----project地址
54 }
55
56 location / {
57 include uwsgi_params;
58 uwsgi_pass django; ----上面修改socket的位置
59 }
60
61 error_page 404 /404.html;
62 location = /40x.html {
63 }
64
65 error_page 500 502 503 504 /50x.html;
66 location = /50x.html {
67 }
68 }
69
70 # Settings for a TLS enabled server.
71 #
72 # server {
73 # listen 443 ssl http2 default_server;
74 # listen [::]:443 ssl http2 default_server;
75 # server_name _;
76 # root /usr/share/nginx/html;
77 #
78 # ssl_certificate "/etc/pki/nginx/server.crt";
79 # ssl_certificate_key "/etc/pki/nginx/private/server.key";
80 # ssl_session_cache shared:SSL:1m;
81 # ssl_session_timeout 10m;
82 # ssl_ciphers HIGH:!aNULL:!MD5;
83 # ssl_prefer_server_ciphers on;
84 #
85 # # Load configuration files for the default server block.
86 # include /etc/nginx/default.d/*.conf;
87 #
88 # location / {
89 # }
90 #
91 # error_page 404 /404.html;
92 # location = /40x.html {
93 # }
94 #
95 # error_page 500 502 503 504 /50x.html;
96 # location = /50x.html {
97 # }
98 # }
99
100 }

6、采用Systemd管理uwsgi

1 mkdir -p /etc/uwsgi/ini
2 mv /home/myself/uwsgi.ini /etc/uwsgi/ini/
 1 vim /etc/systemd/system/uwsgi.service
2
3
4 [Unit]
5 Description=uWSGI Emperor
6 After=syslog.target
7
8 [Service]
9 ExecStart=/root/uwsgi/uwsgi --emperor /etc/uwsgi/ini
10 Restart=always
11 KillSignal=SIGQUIT
12 Type=notify
13 StandardError=syslog
14 NotifyAccess=all
15
16 [Install]
17 WantedBy=multi-user.target