centos7下采用Nginx+uwsgi来部署django

1.安装uWSGI
下载最新的2.0版本,地址:http://projects.unbit.it/downloads/uwsgi-2.0.15.tar.gz
解压、安装
tar zxf uwsgi-2.0.1.tar.gz
cd uwsgi-2.0.1
python3 uwsgiconfig.py –build
python3 uwsgiconfig.py –clean
cd ..
cp -R ./uwsgi-2.0.1 /usr/local/uwsgi
ln -s /usr/local/uwsgi/uwsgi /usr/bin/uwsgi

注意:在任意目录中执行uwsgi时,其默认工作目录为当前目录
1.2测试uWSGI是否安装成功
1 # foobar.py
import os
import sys
def application(env, start_response):
start_response(‘200 OK’, [(‘Content-Type’,’text/html’)])
return “Hello World”
[root@localhost ~]# uwsgi –http :8001 –wsgi-file foobar.py
采用浏览器访问主机或者采用postman模拟get操作,如果出现”Hello World”即测试成功
1.3、测试django工程(这里是我的工程,你可以自己用django建一个简单的网站来测试)
uwsgi –http :8001 –chdir /usr/local/django-certificate-0.1 –module certificate.wsgi
并在django工程的settings里边添加主机的ip地址,采用浏览器访问主机或者采用postman模拟get操作,http://10.10.14.128:8001/ocr/idcardfrontview/,如果页面出现,即测试成功
2.3.安装Nginx
yum install nginx*
测试nginx是否安装成功
curl 127.0.0.1:8000
如果出现welcome to nginx页面则测试通过
3、写uwsgi.ini配置文件
然后运行命令:
1 mkdir -p /etc/uwsgi/ini
2 mv /usr/local/django-certificate-0.1/uwsgi.ini /etc/uwsgi/ini/

#uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /usr/local/django-certificate-0.1
# Django's wsgi file
module          = certificate.wsgi:application
# the virtualenv (full path)
#home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 3
# the socket (use the full path to be safe)
#socket          = /home/myself/myself.sock
socket = 10.10.14.128:8000
# ... with appropriate permissions - may be needed
chmod-socket    = 666
chown-socket = nginx:nginx
# clear environment on exit
vacuum          = true
enable-threads = true
daemonize = /var/log/uwsgi/uwsgi.log

4、修改nginx配置文件
cp /etc/nginx/default.conf /etc/nginx/nginx.conf.bak
vi /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;
    error_log /var/log/nginx/error.log;

    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 10.10.14.128:8000; # 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/ {
            alias /usr/local/django-certificate-0.1/ocr/static;                                  ----project地址
        }

        location /media/ {
            alias /usr/local/django-certificate-0.1/ocr/media;                                  ----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 {
#        }
#    }

}

5、采用Systemd管理uwsgi
vi /etc/systemd/system/uwsgi.service

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

记得配置好后要重启下nginx,以便使之生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值