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

阿里云上CentOS6.5部署Django+nginx+uwsgi

2016-05-25 17:51 911 查看
首先centos6.5要升级python版本和安装django版本上两篇中已经提到!

----------------------------------------------------------------------------------------------------------

安装uwsgi (ch)[py27@localhost
~]$ pip install uwsgi
Collecting uwsgi
Downloading uwsgi-2.0.11.1.tar.gz (782kB)
100% |████████████████████████████████| 782kB 144kB/s 
Building wheels for collected packages: uwsgi
Running setup.py bdist_wheel for uwsgi
Stored in directory: /home/py27/.cache/pip/wheels/65/00/6e/34678eb0a21a697c1646c71ccfabd1cb3c310c20797c60ad01
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.11.1

安装直接使用pip ,迁移是用的python2.7版本的pip。安装完成之后简单测试下uwsgi

新建一个test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"

输入“uwsgi --http :8001 --wsgi-file test.py”启动uwsgi,可以看到类似如下的内容。
(ch)[py27@localhost ~]$ uwsgi
--http :8001 --wsgi-file test.py
*** Starting uWSGI 2.0.11.1 (64bit) on [Wed Sep 23 16:53:25 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-16) on 23 September 2015 16:41:18
os: linux-2.6.32-431.el6.x86_64
#1 SMP Fri Nov 22 03:15:09 UTC 2013
nodename: localhost.localdomain
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/py27
detected binary path: /home/py27/ch/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8001 fd 4
spawned uWSGI http 1 (pid: 15856)
uwsgi socket 0 bound to TCP address 127.0.0.1:38968 (port auto-assigned) fd 3
Python version: 2.7.10 (default, Sep 23 2015, 16:33:09) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x203ccb0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x203ccb0 pid: 15855 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 15855, cores: 1)

浏览器打开ip:8001这个地址应该可以看到“hello world”页面。

创建一个django项目测试。
(ch)[py27@localhost ~]$ django-admin.py
startproject mysite
---------------------------------------------------

安装ngnix

yum -y install gcc gcc-g++ autoconf automake

  yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

  cd ~

 wget http://nginx.org/download/nginx-1.6.0.tar.gz
 ls

  tar -xzvf nginx-1.6.0.tar.gz

 cd nginx-1.6.0

 ./configure

 make

  make install

  ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

-------------------------------------------------------------------------------

.配置uwsgi与nginx支持django:

uwsgi和nginx都可以单独工作,我们要把这两者联系起来,用来支持django项目。

首先我们打开项目所在目录,在根目录,也就是manage.py所在的目录新建一个django_uwsgi.py的文件,这个文件是要django以uwsgi的方式来运行,文件内容如下:代码中注释的那两行是manage.py运行django的方式,可以看出有什么不同。
"""
WSGI config for fengzhengBlog project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/ """

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

#from django.core.wsgi import get_wsgi_application
#application = get_wsgi_application()    版本升级之后此处会报错  详情看最后
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()


为了实现Nginx与uWSGI的连接,两者之间将采用soket来通讯方式,还需要在项目根目录,即和上面的django_uwsgi.py同一目录新建一个文件来实现,文件格式可以是xml,命名为django_socket.xml,内容如下:
<uwsgi>
<socket>:8077</socket>
<chdir></chdir>
<module>django_uwsgi</module><!-- 指定模块 即上面创建的django_uwsgi.py的名称 -->
<processes>4</processes> <!-- 进程数 -->
<daemonize>uwsgi.log</daemonize>
</uwsgi>


或者是ini格式,命名为django_socket.ini,内容如下:
[uwsgi]
vhost = false
socket = 127.0.0.1:8077      ;通信端口
master = true
enable-threads = true
workers = 4
wsgi-file = django_uwsgi.py   ;指定模块 即上面创建的django_uwsgi.py
-------------------------------------------------------------------------------------------------------------------------------
配置nginx 的nginx.conf
我的目录在 cd /usr/local/nginx/conf/nginx.conf
配置如下:我的项目在root/dajangtotest的下的mysite

    server {

        listen       8002;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        access_log  /root/djangotest/mysite/logs/access.log;

        error_log   /root/djangotest/mysite/logs/error.log;

        location / {

           # root   html;

           # index  index.html index.htm;

            include  uwsgi_params;

            uwsgi_pass  127.0.0.1:8077;

            #uwsgi_param UWSGI_SCRIPT mysite.wsgi;

            #uwsgi_param UWSGI_CHDIR /root/djangotest/mysite;

            #index  index.html index.htm;

            #client_max_body_size 35m;

        }

-------------------------------------------------------------------------------------------------------------------------------


uwsgi+django报错django.core.exceptions.AppRegistryNotReady  

参照网上用uwsgi+nginx配置python环境时,报了这么个错,google发现是WSGI application的问题,好象是因为django升级,配置有所变化。

原来:

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

改成:

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

重启一下uwsgi。
----------------------------------------------------------------------------------------------------------

启动网站:

配置完成后,重启nginx :  nginx -s reload

启动uwsgi服务:

进入项目根目录,即前面创建的django_uwsgi.py所在的目录。

运行如下命令,使用django_socket.xml配置:
uwsgi -x django_socket.xml


如果系统不支持-x命令,可以运行下面的命令启动django_socket.ini配置:
uwsgi --ini django_socket.ini


启动成功后,会得到如下信息:

----------------------------------------------------------------------------------------------------------

参考文章:
http://www.it165.net/pro/html/201407/17777.html http://blog.163.com/rihui_7/blog/static/21228514320151353149688/ http://www.codesec.net/view/194213.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: