nginx+gunicorn+django部署,nginxgunicorn
nginx+gunicorn+django部署,nginxgunicorn
部署Django的操作:
0.检查项目需求的安装包。
1.修改setting文件。
1.1,修改ALLOWED_HOSTS。字段。
什么机器可以访问gunicorn。添加反向代理的ip地址。
1.2TIME-ZONE,修改为Asia/Shanghai
1.3 DEBUG =False
。
1.4最重要的是分发Django的静态文件。django自己并不负责任何静态文件的处理。gunicorn也不。所以静态文件处理就落在nginx头上。
步骤:1.设定setting文件中的setting中的
STATIC_URL = '/static/'
#SATIC_ROOT = os.path.join(BASE_DIR, 'static') #这句作用和下一句是一样的。
STATIC_ROOT = '/var/www/static/' #指定静态文件应该去哪里。
1.5在django项目目录上执行。python manage/py collectstatic
。发送开发中的静态文件到指定目录。
2.配置nginx反向代理,以及指定静态文件位置。指定后端gunicorn服务器。
server {
listen 80;
client_max_body_size 4G;
server_name WWW.EXAMPLE.COM
keepalive_timeout 5;
location /static/ {
alias /var/www/xiangstatic/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS
# proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://127.0.0.1:8000;
}
配置systemd启动gunicorn脚本:
vim /lib/systemd/system/gunicorn.service
注: 这里使用的ubuntu 16.10所以unit文件在这里进行注册使用。
参照gunicorn 部署;
[Unit]
Description= Gunicorn A high performance python server
#Requires=gunicorn.socket
After=network.target
[Service]
#Type=forking
Group =root
User=root
PIDFile=/run/gunicorn.pid
#RuntimeDirectory =gunicorn
RuntimeDirectory=gunicorn
WorkingDirectory=/root/XIANGCAOBLOG/
ExecStart=/usr/local/bin/gunicorn --pid /run/gunicorn.pid --workers 3\
--bind 0.0.0.0:8000 LV3.wsgi
# unix:run/gunicorn/socket applicationname.wsgi
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
TimeoutStopSec=5
#KillMode=mixed
[Install]
WantedBy=multi-user.target
如果配置完成后发现访问页面出现bad request(400)。则一定是你的setting文件的配置问题。请仔细修改。
可以启动服务查看业务了。(完)
评论暂时关闭