django生产环境部署

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
安装python3.6.7
1、安装python
访问https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz 下载python源码包
解压
tar xf Python-3.6.7.tar.xz
cd Python-3.6.7
./configure --prefix=/usr/local/python3.6.7 --enable-optimizations
make && make install

为方便调用创建软连接
ln -sf /usr/local/python3.6.7/bin/python3.6 /usr/bin/python3
ln -sf /usr/local/python3.6.7/bin/pip3.6 /usr/bin/pip3

更换pip源
mkdir ~/.pip
cat ~/.pip/pip.conf
[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/

安装python虚拟环境virtualenv
pip3 install virtualenv

到本地项目路径导出依赖库
pip freeze > ./requirements.txt

建议网站目录
mkdir -p /home/wwwroot

创建python虚拟环境
cd /home/wwwroot/ormpro
virtualenv --python=/usr/bin/python3 .py3env
cd .py3env/bin
进入python虚拟环境
source activate
退出python虚拟环境
deactivate

安装django和uwsgi
pip3 install django==2.1.3
pip3 install uwsgi

批量安装依赖库
cd /home/wwwroot/ormpro
pip3 install -r ./requirements.txt

cat uwsgi.ini
[uwsgi]
chdir = /home/wwwroot/mydjango/ormpro //项目路径
module = ormpro.wsgi:application //指定wsgi模块
socket = 127.0.0.1:8000
master = true //主进程
vhost = true //多站模式
workers = 4 #子进程数
vacuum = true //退出重启时清理文件
#no-site = true
#reload-mercy = 10
#max-requests = 1000
#limit-as = 512
#buffer-size = 30000
pidfile = /home/wwwroot/mydjango/ormpro/uwsgi8000.pid
daemonize = /home/wwwroot/mydjango/ormpro/run.log
disable-logging = true
uid = www
gid = www

启动uwsgi
source /home/wwwroot/mydjango/.py3env/bin/activate
uwsgi --ini /home/wwwroot/mydjango/ormpro/uwsgi.ini
deactivate

重载uwsgi
source /home/wwwroot/mydjango/.py3env/bin/activate
uwsgi --reload /home/wwwroot/mydjango/ormpro/uwsgi.pid
deactivate

停止
souruwsgice /home/wwwroot/mydjango/.py3env/bin/activate
uwsgi --stop /home/wwwroot/mydjango/ormpro/uwsgi.pid
deactivate

nginx安装脚本


nginx配置文件
cat ormpro.conf
server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/_ropon.top.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/_ropon.top.key;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name django.ropon.top;
if ($ssl_protocol = "") { return 301 https://$server_name$request_uri; }
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
location /static {
alias /home/wwwroot/mydjango/ormpro/static;
}
}