Django+Nginx+uwsgi部署

Mysql配置

在项目同名文件的__init__.py文件中配置pymyql的连接

import pymysql

pymysql.install_as_MySQLdb()

在settings中配置数据库

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'baoleiji',
        'USER': 'abc',
        'PASSWORD': '123',
        'HOST': 'localhost',
        'PORT': 3306,
    }
}

settings中配置allowed host

ALLOWED_HOSTS = ['*']或是要部署的主机的IP地址

uwsgi的下载安装

安装:pip3 install uwsgi

uwsgi测试

新建app.py,在浏览器端进行访问的时候,返回Hello World
    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"]
        
uwsgi --http :9001 --wsgi-file app.py # 在终端中执行

uwsgi --http :9002 --wsgi-file foobar.py --master --processes 4 --threads 2 # 开启多线程和多进程,根据机器的性能几核开几个进程

Django开启uwsgi测试

  • chdir:项目路径
  • wsgi-file:Django的项目同名文件中的wsgi文件路径
  • static-map:静态文件的前缀是static
uwsgi --http :9004 --chdir /Django_Online/data/deploy --wsgi-file deploy/wsgi.py  --static-map /static=/Django_Online/data/deploy/col_static --master --processes 4 --threads 2 

静态文件的收集

在配置文件中添加:

STATIC_ROOT = os.path.join(BASE_DIR,'col_static') # 可以设置绝对路径

执行python3 manage.py collectstatic,每次更新静态文件后都要执行,然后把关于静态文件的配置文件注释.按如下:

# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR,'static'),
# )
# STATIC_ROOT = os.path.join(BASE_DIR,'col_static')

制作成配置文件

在任意位置添加配置文件uwsgi_http.ini

[uwsgi]
http = 0.0.0.0:9005
chdir = /Django_Online/data/deploy
wsgi-file = deploy/wsgi.py
#processes = 4
#threads = 2
static-map = /static=/Django_Online/data/deploy/col_static

执行:uwsgi uwsgi_http.ini

Nginx的配置

上面的静态文件是通过uwsgi进行配置的,不如Nginx对静态文件的配置效率高。

安装nginx:yum install nginx
启动Nginx:systemctl start nginx
配置Nginx:vim /etc/nginx/nginx.conf

Nginx配置文件

  • upstream django:Nginx通过socket连接8001端口,连接Django
  • location /static 通过正则匹配找到静态文件
  • uwsgi_pass django:就是指向upstream
======================= 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 root;
worker_processes 4;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

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;

    upstream django {
        # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
        server 127.0.0.1:8001; # for a web port socket (we'll use this first)
    }
    server {
        listen      80;

        #server_name ;
        charset     utf-8;


        # max upload size
        client_max_body_size 75M;   # adjust to taste

        location /static {
            alias  /Django_Online/data/deploy/col_static; # your Django project's static files - amend as required
        }

        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  django;
            include     uwsgi_params; # the uwsgi_params file you installed
        }
    }
}

Nginx通过socket连接uwsgi

建立uwsgi_socket.ini文件

[uwsgi]
socket = 127.0.0.1:8001
chdir = /Django_Online/data/deploy
wsgi-file = deploy/wsgi.py
#processes = 4
#threads = 2
static-map = /static=/Django_Online/data/deploy/col_static

最后执行:uwsgi uwsgi_socket.ini

请求流程

注意

如果遇到配置文件的权限问题,需要修selinux的配置
vim /etc/selinux/configuration 修改成disable

posted @ 2017-09-23 18:18  hzxPeter  阅读(250)  评论(0)    收藏  举报