Django 部署(Nginx)
来源:互联网 发布:怎么改淘宝网店名称 编辑:程序博客网 时间:2024/05/23 15:35
1. 运行开发服务器测试
cd
zqxt
# 进入项目 zqxt 目录
python manage.py runserver
运行开发服务器测试,确保开发服务器下能正常打开网站。
2. 安装 nginx 和 需要的包
2.1 安装 nginx 等软件
ubuntu / Linux Mint 等,下面简写为 (ubuntu):
sudo
apt-get
install
python-dev nginx
centos / Fedora/ redhat 等,下面简写为 (centos)
sudo
yum
install
epel-release
sudo
yum
install
python-devel nginx
sudo
pip
install
supervisor
Ubuntu用户 请直接看 3,以下是CentOS 注意事项:
CentOS下,如果不是非常懂 SELinux 和 iptables 的话,为了方便调试,可以先临时关闭它们,如果发现部署了之后出不来结果,可以临时关闭测试一下,这样就知道是不是 SELinux 和 iptables 的问题
CentOS 7 iptables如何使用:http://stackoverflow.com/questions/24756240/how-can-i-use-iptables-on-centos-7
将 SELinux 设置为宽容模式,方便调试:
sudo
setenforce 0
防火墙相关的设置:
可以选择临时关闭防火墙
sudo
service iptables stop
或者开放一些需要的端口,比如 80
sudo
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
上面的两条命令,如果是 CentOS 7 用
临时关闭防火墙
sudo
systemctl stop firewalld
或者 开放需要的端口
sudo
firewall-cmd --zone=public --add-port=80
/tcp
--permanent
sudo
firewall-cmd --reload
备注:由于我还没有用 最新版本的 Fedora ,需要用 dnf 来安装包,有需求的同学自测,可以参考这里。
3. 使用 gunicorn / uwsgi 来部署 (二选一)
注意:以下为二选一,不需要两个都用
3.1 使用 gunicorn(纯Python实现的包):
sudo
pip
install
gunicorn
在项目目录下运行下面的命令进行测试:
gunicorn -w4 -b0.0.0.0:8001 zqxt.wsgi
-w 表示开启多少个worker,-b 表示要使用的ip和port,我们这里用的是 8001,0.0.0.0代表监控电脑的所有 ip。
如果使用了 virtualenv 可以这样
/path/to/env/bin/gunicorn --chdir /path/to/project --pythonpath /path/to/env/ -w4 -b0.0.0.0:8017 project.wsgi:application
用 --pythonpath 指定依赖包路径,多个的时候用逗号,隔开,如:'/path/to/lib,/home/tu/lib'
3.2 使用 uwsgi(纯C语言实现的包):
安装 uwsgi
sudo
pip
install
uwsgi
使用 uwsgi 运行项目
uwsgi --http :8001 --chdir
/path/to/project
--home=
/path/to/env
--module project.wsgi
这样就可以跑了,--home 指定virtualenv 路径,如果没有可以去掉。project.wsgi 指 project/wsgi.py 文件
如果提示端口已经被占用:
probably another instance of uWSGI is running on the same address (:8002).
bind(): Address already in use [core/socket.c line 764]
这时可以把相关的进程 kill 掉:
按照端口进行查询:
lsof
-i :8002
可以查出:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
uwsgi 2208 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
uwsgi 2209 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
sudo
kill
-9 2208 2209
按照程序名称查询:
ps
aux |
grep
uwsgi
4. 使用supervisor来管理进程
安装 supervisor 软件包
sudo pip install supervisor生成 supervisor 默认配置文件,比如我们放在 /etc/supervisord.conf 路径中:
sudo
echo_supervisord_conf >
/etc/supervisord
.conf
打开 supervisor.conf 在最底部添加:
[program:zqxt]
command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi
directory=/path/to/zqxt
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
supervisord -c
/etc/supervisord
.conf
重启 zqxt 程序(项目):
supervisorctl -c
/etc/supervisord
.conf restart zqxt
启动,停止,或重启 supervisor 管理的某个程序 或 所有程序:
supervisorctl -c
/etc/supervisord
.conf [start|stop|restart] [program-name|all]
以 uwsgi 为例,上面这样使用一行命令太长了,我们使用 ini 配置文件来搞定,比如项目在 /home/tu/zqxt 这个位置,
在其中新建一个 uwsgi.ini 全路径为 /home/tu/zqxt/uwsgi.ini
[uwsgi]
socket =
/tmp/zqxt
.sock
chdir=
/home/tu/zqxt
wsgi-
file
= zqxt
/wsgi
.py
processes = 2
threads = 4
chmod
-socket = 664
chown
-socket=tu:www-data
注意上面的 /tmp/zqxt.sock ,一会儿我们把它和 nginx 关联起来。
修改 supervisor 配置文件中的 command 一行:
[program:zqxt]
command=/path/to/uwsgi --ini /home/tu/zqxt/uwsgi.ini
directory=/path/to/zqxt
startsecs=0
supervisorctl -c
/etc/supervisord
.conf restart zqxt
或者
supervisorctl -c
/etc/supervisord
.conf restart all
5. 配置 Nginx
新建一个网站 zqxt
sudo
vim
/etc/nginx/sites-available/zqxt
.conf
写入以下内容:
server {
listen 80;
server_name www.ziqiangxuetang.com;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /path/to/project/media;
}
location /static {
alias /path/to/project/static;
}
location / {
uwsgi_pass unix:/tmp/zqxt.sock;
include /etc/nginx/uwsgi_params;
}
}
sudo
ln
-s
/etc/nginx/sites-available/zqxt
.conf
/etc/nginx/sites-enabled/zqxt
.conf
测试配置语法问题
sudo
service nginx configtest
重启 nginx 服务器:
sudo
service nginx reload 或者
sudo
service nginx restart
一些有用的参考教程:
Django 官网部署教程:
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/gunicorn/
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/uwsgi/
一些博客相关教程:
http://www.ituring.com.cn/article/201045
http://www.jianshu.com/p/be9dd421fb8d
http://blog.csdn.net/tengzhaorong/article/details/12833157
nginx 与 socket
http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#configure-nginx-for-your-site
防火墙:
iptables: https://www.digitalocean.com/community/tutorials/how-to-setup-a-basic-ip-tables-configuration-on-centos-6
centos 7 FireWalld: http://stackoverflow.com/questions/24756240/how-can-i-use-iptables-on-centos-7
ubuntu ufw 防火墙:http://wiki.ubuntu.org.cn/Ufw%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97
uwsgi ini 配置文件:http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#configuring-uwsgi-to-run-with-a-ini-file
- nginx部署django
- Django+Nginx+uwsgi部署
- nginx uwsgi django部署
- nginx uwsgi 部署django
- django nginx+uwsgi部署
- Django+uWsgi+nginx部署
- Django 部署(Nginx)
- Nginx+uWSGI+Django部署
- nginx+uwsgi部署django
- nginx+uwsgi部署django
- nginx 部署django程序
- Django部署(Nginx)
- nginx+uwsgi 部署django
- Django nginx uwsgi部署
- nginx+fastCGI 部署Django
- uwsgi+django+nginx部署
- django+nginx+flup部署
- django + gunicorn + nginx 部署
- 如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】
- 【Leetcode】Rotate Image
- OC - 第五章 集合 ( 数组、字典、集合 )
- HDU 5592 ZYB's Premutation 线段树
- static用法总结
- Django 部署(Nginx)
- 模块度与Louvain社区发现算法
- K-SVD算法
- 标签的语法
- [LeetCode]007-Reverse Integer
- zoj 3349 Special Subsequence 【离散化二分 + 线段树优化dp】
- 新手学socket
- 一起学libcef--libcef的基本类和方法介绍(如何产生一个你自己的浏览器)
- Spring MVC集成Tiles框架