您的位置:首页 > 运维架构 > Nginx

阿里云 Contos 6.5 + nginx + uwsgi + django环境部署

2015-10-19 21:55 831 查看
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式,以下分5步介绍其部署过程:

1.安装uwsgi

pip安装:pip install uwsgi

如果尚未安装pip可先行安装pip,安装pip的步骤:

1.1先安装setuptools

1.2easy_install pip 或者 pypi.python.org下载pip压缩包解压后python setup.py install

测试uwsgi是否安装成功:编写uwsgi.py文件,内容如下:

def application(env, start_response):

start_response('200 OK', [('Content-Type','text/html')])

return "OK"

运行脚本: uwsgi --http :8001 --wsgi-file uwsgi.py

查看结果: http://127.0.0.1:8001/
查看到页面显示:OK

2.配置django编写django_wsgi.py文件,将其放在与文件manage.py同一个目录下,内容如下:

# -*-coding: utf-8 -*-

import os

import sys

reload(sys)sys.setdefaultencoding('utf8')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Yangtai.settings")

# 如果下面找不到APP就用下面这个注释的试试

#from django.core.handlers.wsgi import WSGIHandler

#application = WSGIHandler()from django.core.wsgi

import get_wsgi_application

application = get_wsgi_application()

测试一下:uwsgi --http :8000 --chdir /home/mydjango/ --module django_wsgi

访问:127.0.0.1:8000即可看到django的It worked界面

3.配置uWSGI为了实现Nginx与uWSGI的连接,两者之间将采用soket来通讯方式:新建djangochina_socket.xml,放置在django项目目录下,即和django的manage.py同一目录,内容如下:

<uwsgi>

<socket>:9090</socket>

<chdir>/home/mydjango</chdir>

<module>django_wsgi</module>

<processes>4</processes>

<daemonize>/root/log/uwsgi.log</daemonize>

</uwsgi>

4.配置Nginx查找到nginx的nginx_conf文件,并编辑它,查找方式可以:find / -name nginx_conf向其中的http请求中添加内容大致如下:

server {

listen 80;

server_name 120.24.239.22;

#server_name可为域名也可为IP

access_log /home/logs/access.log;

error_log /home/logs/error.log;

#charset koi8-r;

#access_log logs/host.access.log

main;

location / {

include uwsgi_params;

uwsgi_pass 127.0.0.1:9090;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

# error_page 500 502 503 504 /50x.html;

location = /50x.html { root html; }

location /static/ {

alias /home/testdjango/collectedstatic/;

index index.html index.htm;

}

location /media/ {

alias /home/testdjango/public/media/;

}

}

5.Nginx+uWSGI+Django的实现方式

5.1启动uWSGI服务器uwsgi -x djangochina_socket.xml

查看是否开启成功:lsof -i:9090

查看到开启4个进程运行即开启成功

5.2重启nginx服务器重启方式:

1.nginx -s reload

2.service nginx restart

5.3查看设置的域名或者IP即可查看django的It worked页面更新代码后,重启服务方式:

1.查找uwsgi进行id查找方式: lsof -i:9090杀掉进程: kill -9 进程ID

2.重启uwsgi & nginx 服务器

PS:第一次写CSDN博客,虽然这篇文章百度出很多类似的,自己也是在django china学习的,前期搭建遇见很多问题,最后还是一一解决了,希望看到这篇文章的童鞋们,如果遇见了问题不要泄气,百度或者Google查查就好

本人扣扣:407708323

本人博客:http://xuyangting.sinaapp.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: