阿里云上CentOS6.5部署Django+nginx+uwsgi
来源:互联网 发布:天猫和淘宝价格对比 编辑:程序博客网 时间:2024/04/30 08:15
首先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, seehttps://docs.djangoproject.com/en/dev/howto/deployment/wsgi/"""import osos.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 WSGIHandlerapplication = 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 = falsesocket = 127.0.0.1:8077 ;通信端口master = trueenable-threads = trueworkers = 4wsgi-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
- 阿里云上CentOS6.5部署Django+nginx+uwsgi
- CentOS6.5+Django+Nginx+MySQL+uwsgi 阿里云环境部署
- 阿里云Ubuntu上通过nginx+uwsgi部署Django
- 在阿里云ECS上部署 Django+MySQL+uWSGI+Nginx 项目的基本流程
- 阿里云上部署 django+uwsgi+nginx,admin静态文件问题
- 阿里云 Contos 6.5 + nginx + uwsgi + django环境部署
- centos6.5+Django+mysql+nginx+uwsgi
- centos6.5下配置django+uwsgi+nginx
- centos6.5安装nginx+python+uwsgi+django
- Django+Nginx+uwsgi部署
- nginx uwsgi django部署
- nginx uwsgi 部署django
- django nginx+uwsgi部署
- Django+uWsgi+nginx部署
- Nginx+uWSGI+Django部署
- nginx+uwsgi部署django
- nginx+uwsgi部署django
- nginx+uwsgi 部署django
- C#中重新定义Console实现自动保存输出到文件
- Android Studio 2.1.1 更换主题 代码样式
- HDU 1075.What Are You Talking About【Map水题(字典树)】【5月25】
- Android 6.0 ConfirmCredential
- 实时人脸检测 (Real-Time Face Detection)
- 阿里云上CentOS6.5部署Django+nginx+uwsgi
- android 蓝牙编程重点---如何发送和接收16进制数据
- 网站锚点执行平滑滚动
- 用次世代2.3训练自己的字符库cds小demo
- 最全整理浏览器兼容性问题与解决方案
- Android技巧之封装Log工具类
- iOS开发网络篇—数据缓存
- android蓝牙开发---与蓝牙模块进行通信
- PhalGo-介绍 PhalGo-介绍