centos7下采用Nginx+uwsgi来部署django

时间:2024-01-08 19:01:20

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

1、安装uwsgi以及Nginx

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

2、测试

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

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

3、测试django工程

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

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

4、写uwsgi.ini配置文件

 #uwsgi.ini file
[uwsgi] # Django-related settings
# the base directory (full path)
chdir = /home/myself
# Django's wsgi file
module = myself.wsgi:application
# the virtualenv (full path)
#home = /path/to/virtualenv # process-related settings
# master
master = true
# maximum number of worker processes
processes =
# the socket (use the full path to be safe)
#socket = /home/myself/myself.sock
socket = 127.0.0.1:
# ... with appropriate permissions - may be needed
chmod-socket =
chown-socket = nginx:nginx
# clear environment on exit
vacuum = true
enable-threads = true

5、修改nginx配置文件

 vim /etc/nginx/nginx/conf
 # For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/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; upstream django {
#server unix:/home/myself/myself.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first) ---socket
} server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location /static/ {
root /home/myself; ----project地址
} location / {
include uwsgi_params;
uwsgi_pass django; ----上面修改socket的位置
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} # Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# } }

6、采用Systemd管理uwsgi

 mkdir -p /etc/uwsgi/ini
mv /home/myself/uwsgi.ini /etc/uwsgi/ini/
 vim /etc/systemd/system/uwsgi.service

 [Unit]
Description=uWSGI Emperor
After=syslog.target [Service]
ExecStart=/root/uwsgi/uwsgi --emperor /etc/uwsgi/ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all [Install]
WantedBy=multi-user.target