Mysql配置
在项目同名文件的init.py文件中配置pymyql的连接
1 2 3
| import pymysql pymysql.install_as_MySQLdb()
|
在settings中配置数据库
1 2 3 4 5 6 7 8 9 10
| 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测试
1 2 3 4 5 6 7 8
| 新建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
1
| 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
|
静态文件的收集
在配置文件中添加:
1
| STATIC_ROOT = os.path.join(BASE_DIR,'col_static')
|
执行python3 manage.py collectstatic,每次更新静态文件后都要执行,然后把关于静态文件的配置文件注释.按如下:
制作成配置文件
在任意位置添加配置文件uwsgi_http.ini
1 2 3 4 5 6 7
| [uwsgi] http = 0.0.0.0:9005 chdir = /Django_Online/data/deploy wsgi-file = deploy/wsgi.py 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| ======================= nginx.conf =================== 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; include /etc/nginx/mime.types; default_type application/octet-stream; upstream django { server 127.0.0.1:8001; } server { listen 80; charset utf-8; client_max_body_size 75M; location /static { alias /Django_Online/data/deploy/col_static; } location / { uwsgi_pass django; include uwsgi_params; } } }
|
Nginx通过socket连接uwsgi
建立uwsgi_socket.ini文件
1 2 3 4 5 6 7
| [uwsgi] socket = 127.0.0.1:8001 chdir = /Django_Online/data/deploy wsgi-file = deploy/wsgi.py static-map = /static=/Django_Online/data/deploy/col_static
|
最后执行:uwsgi uwsgi_socket.ini
请求流程

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