CentOS 6.5中部署django uwsgi nginx mysql项目

因为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

CentOS 6.5安装Python 2.7实例

关于西部数码

成都西维数码科技有限公司成立于2002年,注册资本1000万元,总部坐落于“天府之国”——成都,旗下品牌西部数码 (www.west.cn) ,深耕IDC行业十多年,已拥有北京、广东、郑州、成都、绵阳、香港等中国多个云计算IDC安全数据中心和美国等海外数据中心。我们先后自主研发的虚拟主机弹性云服务器、西数企业云邮箱等产品都广受用户欢迎。我们始终坚持用户体验至上的价值导向,深入挖掘用户需求,目前,有超过一百万用户通过我们注册并管理了超过一千万个域名,累计有超过50万网站在我们的自主研发的云主机平台上运行,服务的用户有:宝贝回家寻子网、四川大学、链家网(北京)科技有限公司、四川省互联网协会、沱牌集团、谭木匠、中铁二局、四川省中国青年旅行社、富森美家居网上商城等

我们始终坚持“以人为本,客户为尊,永续创新”的核心价值观,抢抓各种发展机遇,不断创新发展理念,不断转变发展方式,不断破解发展难题,随着企业的发展,我们经营的业务也不断发展为以云计算为基础的云主机业务、域名注册、域名交易等其他相关业务,公司从最初只有几个员工发展到如今拥有近二百人的精英团队,并在中国IDC、云计算行业中占有一席之地,位列国内三强。成为拥有多项自主知识产权的国家高新技术企业,ICANN和CNNIC双认证的国际顶级域名注册服务机构,首批获得国家工信部颁发的全国云服务牌照企业之一。

赞(0)
声明:本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-62778877-8306;邮箱:fanjiao@west.cn。本站原创内容未经允许不得转载,或转载时需注明出处:西部数码知识库 » CentOS 6.5中部署django uwsgi nginx mysql项目

登录

找回密码

注册