6. Django --- 用nginx, supervisor
2018-01-06 本文已影响73人
HelloJames
注意: 本次使用的环境是Centos 6 64位, Python2.7
所有安装操作与环境为本人亲测, 综合网上各种教程实现! 其中过程也是五味zzz
nginx
sudo yum install nginx
启动
service nginx start
停止
service nginx stop
重启
service nginx restart
关于nginx的更多细节, 大家可以自行Google.
配置nginx
Copy uwsgi_params至Django项目根目录
若使用yum安装, 默认uwsgi_params
在/etc/nginx
目录下, 将其copy一份至django项目根目录.
配置nginx.conf
server {
listen 90;
server_name localhost;
location /static {
alias /var/www/djangoSite/static;
}
location /media {
alias /var/www/djangoSite/media;
}
location / {
proxy_pass http://127.0.0.1:9090;
include /var/www/djangoSite/uwsgi_params;
}
}
注意:
此处配置了静态文件和media上传文件的目录, 在准备发布django项目时, 注意收集static, 命令:python manager.py collectstatic
uWSGI 安装
pip install uwsgi
安装好之后, 将uwsgi执行程序配置到环境变量中, 或是创建一个软链接至
/usr/bin
.
配置之后, 在命令行执行uwsgi, 确认是否可用
安装supervisor
pip install supervisor
supervisor的安装与配置详细过程, 可以参考一文章: 第二篇: 搭建环境 -- Centos7(64位)云主机部署Python Flask项目实战系列
基于uWSGI, nginx和supervisor部署Django
原理
web客户端 <-> nginx <-> socket <-> uwsgi <-> django
```
### 简单测试
在任一目录新建一个文件 : `test.py`, 添加以下内容:
```
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ["Hello World"]
```
此时, 运行uwsgi
`uwsgi --http :9090 --wsgi-file test.py`
参数含义:
* http: 9090, 使用http协议, 9090端口
* wsgi-file test.py: 加载指定文件`test.py`
在nginx中配置好后, 此时访问浏览器:
`http://demo.com:90`
此时浏览器上应该显示`hello world`
如果显示正确, 说明下面3个环节是通畅的
`web客户端 <-> nginx <-> socket <-> uwsgi <-> python`
### Django项目测试.
> 发布项目时, 记得修改settings.py中的以下两项如下:
```
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']
```
#### django项目目录结构如下:
```
djangoSite
- djangoSite
settings.py
urls.py
wsgi.py
__init__.py
- polls
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
```
#### 确认django项目本身是否可运行
`python manage.py runserver 127.0.0.1:9090`
再次在浏览器访问: `http://demo.com:90`
如果没有问题, 使用uwsgi把项目串起来
***在项目根目录下执行以下命令:***
`uwsgi --http :9090 --module djangoSite.wsgi`
再次在浏览器访问: `http://demo.com:90`, 如果没有问题, 说明所有链路应该都是通的.
`web客户端 <-> nginx <-> socket <-> uwsgi <-> django`
### 配置supervisor
在supervisor.conf文件中, 添加以下配置:
```
[program:djangoSite]
directory= /var/www/djangoSite
command = uwsgi --http :9090 --module djangoSite.wsgi
user = daemon
autostart = true
autorestart = true
stopsignal = QUIT
redirect_stderr = true
loglevel = error
stdout_logfile = /usr/uwsgi/logs/uwsgi_out.log
stderr_logfile = /usr/uwsgi/logs/uwsgi_err.log
logfile_maxbytes = 1M
```
> supervisor的安装与配置详细过程, 可以参考一文章: [第二篇: 搭建环境 -- Centos7(64位)云主机部署Python Flask项目实战系列](https://segmentfault.com/a/1190000010629720#articleHeader10)
配置好supervisor后, 重启supervisor中所有应用:
```
supervisor> reload
Really restart the remote supervisord process y/N? y
Restarted supervisord
supervisor> status
djangoSite STARTING
supervisor> status
djangoSite RUNNING pid 12561, uptime 0:00:02
supervisor>
```
最后, 再次在浏览器访问: `http://demo.com:90`, 如果没有问题, 就OK了!
作者:JamesZhang
链接:https://segmentfault.com/u/jamezhang
來源:Segmentfault
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。