码迷,mamicode.com
首页 > 其他好文 > 详细

uwsgi+django+nginx

时间:2016-11-27 00:20:04      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:nginx   django   uwsgi   python2.7   

    首先安装nginx和uwsgi,nginx用源码安装,uwsgi用pip install uwsgi安装,环境为centos6.8,python为2.7。开始前,确保项目在python manage.py runserver下能正常运行。

    uwsgi支持ini、xml等多种配置方式,本文简单介绍两种方式:ini和xml。

  1. ini方式

    

[root@work ~]# cat /etc/uwsgi9000.ini 
[uwsgi]
socket = 127.0.0.1:9000
master = true
chdir = /django_web/web
wsgi-file = web/wsgi.py
processes = 4
daemonize = /django_web/django_web.log
pidfile = /tmp/uwsgi.pid
vacuum = true
log-maxsize = 50000000
disable-logging = true

创建一个ini配置文件,找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:

server {
        listen       80;
        server_name  192.168.137.224;
        access_log /django_web/access_log;
        error_log /django_web/error_log;


        location / {            
            include  uwsgi_params;  
            # uwsgi_pass 对应uwsgi配置中的socket
            uwsgi_pass  127.0.0.1:9000;  
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /static/ {
            alias  /django_web/web/collected_static/;
            index  index.html index.htm;
        }

        location /media/ {
            alias /django_web/web/media/;
        }
}

这里提一下:/static/目录需要修改django项目下的settings.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
 
STATIC_URL = ‘/static/‘
 
# 当运行 python manage.py collectstatic 的时候
# STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用apache等部署的时候更方便
STATIC_ROOT = os.path.join(BASE_DIR, ‘collected_static‘)
 
# 其它 存放静态文件的文件夹,可以用来存放项目中公用的静态文件,里面不能包含 STATIC_ROOT
# 如果不想用 STATICFILES_DIRS 可以不用,都放在 app 里的 static 中也可以
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "common_static"),
    ‘/path/to/others/static/‘,  # 用不到的时候可以不写这一行
)
 
# 这个是默认设置,Django 默认会在 STATICFILES_DIRS中的文件夹 和 各app下的static文件夹中找文件
# 注意有先后顺序,找到了就不再继续找了
STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

目录类似为:

[root@work web]# tree
.
├── collected_static
│   └── admin
│       ├── css
│       │   ├── base.css
│       │   ├── changelists.css
│       │   ├── dashboard.css
│       │   ├── fonts.css
│       │   ├── forms.css
│       │   ├── login.css
│       │   ├── rtl.css
│       │   └── widgets.css
│       ├── fonts
│       │   ├── LICENSE.txt
│       │   ├── README.txt
│       │   ├── Roboto-Bold-webfont.woff
│       │   ├── Roboto-Light-webfont.woff
│       │   └── Roboto-Regular-webfont.woff
│       ├── img
│       │   ├── calendar-icons.svg
│       │   ├── gis
│       │   │   ├── move_vertex_off.svg
│       │   │   └── move_vertex_on.svg
│       │   ├── icon-addlink.svg
│       │   ├── icon-alert.svg
│       │   ├── icon-calendar.svg
│       │   ├── icon-changelink.svg
│       │   ├── icon-clock.svg
│       │   ├── icon-deletelink.svg
│       │   ├── icon-no.svg
│       │   ├── icon-unknown-alt.svg
│       │   ├── icon-unknown.svg
│       │   ├── icon-yes.svg
│       │   ├── inline-delete.svg
│       │   ├── LICENSE
│       │   ├── README.txt
│       │   ├── search.svg
│       │   ├── selector-icons.svg
│       │   ├── sorting-icons.svg
│       │   ├── tooltag-add.svg
│       │   └── tooltag-arrowright.svg
│       └── js
│           ├── actions.js
│           ├── actions.min.js
│           ├── admin
│           │   ├── DateTimeShortcuts.js
│           │   └── RelatedObjectLookups.js
│           ├── calendar.js
│           ├── cancel.js
│           ├── change_form.js
│           ├── collapse.js
│           ├── collapse.min.js
│           ├── core.js
│           ├── inlines.js
│           ├── inlines.min.js
│           ├── jquery.init.js
│           ├── popup_response.js
│           ├── prepopulate_init.js
│           ├── prepopulate.js
│           ├── prepopulate.min.js
│           ├── SelectBox.js
│           ├── SelectFilter2.js
│           ├── timeparse.js
│           ├── urlify.js
│           └── vendor
│               ├── jquery
│               │   ├── jquery.js
│               │   ├── jquery.min.js
│               │   └── LICENSE-JQUERY.txt
│               └── xregexp
│                   ├── LICENSE-XREGEXP.txt
│                   ├── xregexp.js
│                   └── xregexp.min.js
├── common_static
├── Demo
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_contact_tag.py
│   │   ├── 0002_contact_tag.pyc
│   │   ├── 0003_host.py
│   │   ├── 0003_host.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   ├── tests.py
│   └── views.py
├── django_wsgi.py
├── django_wsgi.pyc
├── manage.py
├── templates
│   ├── hello.html
│   ├── post.html
│   └── search_form.html
├── test.py
├── web
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── search2.py
│   ├── search2.pyc
│   ├── search.py
│   ├── search.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── testdb_delete.py
│   ├── testdb_delete.pyc
│   ├── testdb.py
│   ├── testdb.pyc
│   ├── testdb_select.py
│   ├── testdb_select.pyc
│   ├── testdb_update.py
│   ├── testdb_update.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── view.py
│   ├── view.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── web_socket.xml

静态文件放在对应的 app 下的 static 文件夹中 或者 STATICFILES_DIRS 中的文件夹中。

当 DEBUG = True 时,Django 就能自动找到放在里面的静态文件。(Django 通过 STATICFILES_FINDERS 中的“查找器”,找到符合的就停下来,寻找的过程 类似于 Python 中使用 import xxx 时,找 xxx 这个包的过程)。

当 settings.py 中的 DEBUG = True 时,打开开发服务器 python manage.py runserver 直接访问 /static/zqxt.png 就可以找到这个静态文件。

也可以在 settings.py 中指定所有 app 共用的静态文件,比如 jquery.js 等

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "common_static"),
)

然后,运行

python manage.py collectstatic

会将所有静态文件收集到collected_static目录下。

运行 

uwsgi -i /etc/uwsgi9000.ini &
/usr/local/nginx/sbin/nginx

访问127.0.0.1就相当于访问django项目。

2.xml方式

nginx配置一样,不同ini的是要在django项目根目录下建一个.xml文件,如上文目录视图。

[root@work web]# cat web_socket.xml 
<uwsgi>
    <socket>:9000</socket>
    <chdir>/django_web/web</chdir>
    <module>django_wsgi</module>
    <processes>4</processes> <!-- 进程数 --> 
    <daemonize>uwsgi.log</daemonize>
</uwsgi>

然后运行

uwsgi -x /django_web/web/web_socket.xml &
/usr/local/nginx/sbin/nginx

个人还是习惯用ini方式,简洁明了。

本文出自 “echo xiayun” 博客,请务必保留此出处http://linuxerxy.blog.51cto.com/10707334/1876921

uwsgi+django+nginx

标签:nginx   django   uwsgi   python2.7   

原文地址:http://linuxerxy.blog.51cto.com/10707334/1876921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!