CentOS 6.5中部署django+uwsgi+nginx+mysql项目

发表时间:2015-09-23 17:28 | 分类:其他知识 | 浏览:818 次

因为django 1.8至少需要python 2.7以上的版本,所以在部署django之前还需要确保python版本是否满足要求。上一篇文章介绍了如何在centos 6.5中单独安装python 2.7 ,如果你的python已经是2.7可以略过此步骤。

安装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

使用pip安装django最简单。

(ch)[py27@localhost ~]$ pip install Django
Collecting Django
 Downloading Django-1.8.4-py2.py3-none-any.whl (6.2MB)
 100% |████████████████████████████████| 6.2MB 61kB/s 
Installing collected packages: Django
Successfully installed Django-1.8.4

创建一个django项目测试。

(ch)[py27@localhost ~]$ django-admin.py startproject mysite
(ch)[py27@localhost ~]$ ll
total 12
drwxrwxr-x. 5 py27 py27 4096 Sep 23 16:41 ch
drwxrwxr-x. 3 py27 py27 4096 Sep 23 17:00 mysite
-rw-rw-r--. 1 py27 py27 118 Sep 23 16:49 test.py
(ch)[py27@localhost ~]$ cd mysite/
(ch)[py27@localhost mysite]$ python manage.py runserver 0.0.0.0:8001
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
September 23, 2015 - 09:00:44
Django version 1.8.4, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8001/
Quit the server with CONTROL-C.
[23/Sep/2015 09:00:56] "GET / HTTP/1.1" 200 1767
[23/Sep/2015 09:00:56] "GET /favicon.ico HTTP/1.1" 404 1942

浏览器看到django页面说明django安装成功。

uwsgi和django整合

django的runserver只在测试环境使用,正式环境不推荐使用这种方式。所以需要使用uwsgi调用django程序。django 1.4以上的版本在项目创建的时候会自动生产wsgi模块。

uwsgi支持很多种的配置方式,这里博主使用ini文件。

[uwsgi]
project = mysite
base = /home/py27
chdir = %(base)/%(project)
module = %(project).wsgi:application
;socket = 127.0.0.1:8001
;socket = /home/pyuser/var/%(project).sock
;chmod-socket = 664
http = 0.0.0.0:8001
master = true
processes = 3
vacuum = true
pidfile = /home/py27/uwsgi8001.pid
daemonize = /home/py27/uwsgi8001.log

连接时有socket和http两种方式,socket一般是提供给第三方调用,而http可以接受浏览器的http请求。上面配置文件是为了测试,接下来如果使用nginx时可以使用socket+端口或者socket+sock两种方式。

启动

(ch)[py27@localhost ~]$ uwsgi --ini mysite.ini 
[uWSGI] getting INI configuration from mysite.ini
(ch)[py27@localhost ~]$ tailf uwsgi8001.log 
your mercy for graceful operations on workers is 60 seconds
mapped 291072 bytes (284 KB) for 3 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x14370a0 pid: 16050 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 16050)
spawned uWSGI worker 1 (pid: 16053, cores: 1)
spawned uWSGI worker 2 (pid: 16054, cores: 1)
spawned uWSGI worker 3 (pid: 16055, cores: 1)
spawned uWSGI http 1 (pid: 16056)
[pid: 16055|app: 0|req: 1/1] 192.168.140.1 () {36 vars in 619 bytes} [Wed Sep 23 09:13:44 2015] GET / => generated 1767 bytes in 13 msecs (HTTP/1.1 200) 2 headers in 73 bytes (1 switches on core 0)

出现这个界面且浏览器打开正常说明django和uwsgi整合成功。有关uwsgi的详细配置参考官方文档。

配置nginx

安装完nginx后增加类似如下配置文件,重新加载nginx即可。

server
 {
 listen 80;
 server_name manage.hostunion.net;
location ~ ^/static/admin(/|$) {
 root /home/py27/ch/lib/python2.7/site-packages/django/contrib/admin ;
 }
location ~ ^/static(/|$) {
 root /home/py27/mysite;
 }
location / {
 include uwsgi_params;
 #uwsgi_pass 127.0.0.1:8001;
 uwsgi_pass unix:/home/py27/var/mysite.sock;
 }
access_log /home/wwwlogs/manage.hostunion.net.log access;
 }

不出意外,django项目的部署就完成了。

参考连接:

http://stackoverflow.com/questions/15878176/uwsgi-invalid-request-block-size

http://www.sijitao.net/2050.html

本文标签:,

本文链接:http://www.sijitao.net/2054.html

本文版权虫虫开源所有,欢迎您在本博客中留下评论,如需转载原创文章请注明出处,谢谢!

已经有2个回复
Comment (2)
Trackbacks (0)
  1. 大学问社区  ( 2015.10.8 14:42 ) : #-9

    第一次进来 留个言 博主网站内容很丰富,学习了。

    回复
    • 章郎虫  ( 2015.10.8 14:52 ) :

      谢谢。网站只是记录自己工作学习中的总结,希望可以对别人有点帮助。

      回复
  1. 还没有Trackbacks
icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif 

一键脚本 SSH攻击 IP查询 博客历程 留言本 文章归档 网站地图 谷歌地图
托管于阿里云&七牛云. 已加入博客联盟. 浙ICP备13025236号.
Copyright © 2010-2016 虫虫开源 All Rights Reserved.