231fds 发表于 2016-1-19 08:51:38

初探django-演示charade使用virtualenv来配置项目在centos7下部署

初探django-演示charade使用virtualenv来配置项目在centos7下部署
=======================================
2016/1/18

####charade 是一个猜单词的小游戏。
https://github.com/opera443399/charade

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
prepare
-------
1. pip+virtualenv+django ::

      # yum install python-pip
      # pip install virtualenvwrapper
      # mkdir /opt/.virtualenvs /opt/.pyprojects
      # echo 'export WORKON_HOME=/opt/.virtualenvs' >>~/.bashrc
      # echo 'export PROJECT_HOME=/opt/.pyprojects' >>~/.bashrc
      # echo 'source /usr/bin/virtualenvwrapper.sh' >>~/.bashrc
   
      # source ~/.bashrc
      # mkproject django_web
      (django_web)# pip install django


2. 调整 project setting ::

      (django_web)# django-admin startproject www
      (django_web)# cd www
      (django_web)# vim www/settings.py
      增加app,调整语言和时区,配置文件最后增加static目录的路径
            INSTALLED_APPS = [
                (omitted)
                'charade',
                'polls',
            ]
            LANGUAGE_CODE = 'zh-cn'
            TIME_ZONE = 'Asia/Shanghai'

3. 拷贝上述2个app的代码到project目录下 ::

    (omitted)

4. 调整 project urls ::

      (django_web)# vim www/urls.py
            from django.conf.urls import url, include
            from django.contrib import admin
            from charade import views
   
            urlpatterns = [
                url(r'^$', views.index, name='index'),
                url(r'^charade/', include('charade.urls', namespace='charade')),
                url(r'^polls/', include('polls.urls', namespace='polls')),
                url(r'^admin/', include(admin.site.urls)),

5. 生成数据库 ::

      (django_web)# python manage.py migrate
      (django_web)# python manage.py makemigrations charade
      (django_web)# python manage.py sqlmigrate charade 0001
      (django_web)# python manage.py makemigrations polls
      (django_web)# python manage.py sqlmigrate polls 0001
      (django_web)# python manage.py migrate

6. 试着运行一下 ::

      (django_web)# python manage.py runserver 0.0.0.0:80
    测试基本功能,例如,我们用到了:pytz,需要安装,否则会报错:
   
      Exception Type:    ImproperlyConfigured
      Exception Value:   
      This query requires pytz, but it isn't installed.
      (django_web)# pip install pytz   
   
    确认后台的数据读写无异常后,停止运行,后续将使用uwsgi来管理。


7. admin后台 ::

      (django_web)# python manage.py createsuperuser
      http://0.0.0.0/admin/


8. debug ::

      DEBUG=False,则django不处理静态文件,此时应该配置nginx或apache来处理静态文件。


uwsgi+supervisord+nginx
----------------------
1. 安装 ::

      # yum install nginx python-devel
      # yum groupinstall "development tools"
      # pip install supervisor
      # whereis supervisord
      supervisord: /usr/bin/supervisord /etc/supervisord.conf
         
      (django_web)# pip install uwsgi
      (django_web)# whereis uwsgi
      uwsgi: /opt/.virtualenvs/django_web/bin/uwsgi   

2. 配置 ::

    1) 关闭django项目的 DEBUG 选项,并设置 ALLOWED_HOSTS 和 STATIC_ROOT :
   
      (django_web)# vim www/settings.py
      DEBUG = False
         
      ALLOWED_HOSTS = ['*']
         
      STATIC_ROOT = os.path.join(BASE_DIR,'static')
   
    2) 收集django项目的static文件:
   
      (django_web)# python manage.py collectstatic
   
    3) 使用supervisor来管理uwsgi服务,用uwsgi来运行django:
   
      # # echo_supervisord_conf > /etc/supervisord.conf \
      && mkdir /etc/supervisor.d \
      && echo -e '\nfiles=/etc/supervisor.d/*.ini' >>/etc/supervisord.conf \
      && grep ^[^\;] /etc/supervisord.conf
         
      # whereis supervisord
   
    4) 启动 supervisord 服务:
   
      # /usr/bin/supervisord -c /etc/supervisord.conf
      # echo '/usr/bin/supervisord -c /etc/supervisord.conf' >>/etc/rc.local
   
    5) 配置uwsgi服务:
   
      # cat /etc/supervisor.d/uwsgi.ini
      
      command=/opt/.virtualenvs/django_web/bin/uwsgi --socket 127.0.0.1:8090 --chdir /opt/.pyprojects/django_web/www --module www.wsgi
         
    6)启动 uwsgi 服务:
   
      # supervisorctl reload
      Restarted supervisord
      # supervisorctl status
      uwsgi                            RUNNING   pid 22023, uptime 0:00:05
   
      说明:
      uwsgi 使用 --socket 方式,表示:通过socket来访问,因此后续可以用 nginx uwsgi 模块来访问。
      uwsgi 使用 --http 方式,表示:可以直接通过 http访问,因此后续可以用 nginx proxy 来访问。
   
   
    7) 使用nginx来处理静态文件和转发请求到后端的uwsgi服务
   
      a)nginx uwsgi
      # cat /etc/nginx/conf.d/www.conf
      server {
            listen 80 default;
            server_name www.test.com;
            charset utf-8;
         
            location /static {
                alias /opt/.pyprojects/django_web/www/static;
            }
         
            location / {
                uwsgi_pass 127.0.0.1:8090;
                include uwsgi_params;
            }
      }
         
      b)nginx proxy
      # cat /etc/nginx/conf.d/www.conf
      upstream backend {
            server 127.0.0.1:8090;
      }
         
      server {
            listen 80 default;
            server_name www.test.com;
            charset utf-8;
            
            location /static {
                alias /opt/.pyprojects/django_web/www/static;
            }
         
            location / {
                proxy_pass http://backend;
            }
      }
         
      (centos7)
      # systemctl start nginx.service
      # systemctl enable nginx.service



页: [1]
查看完整版本: 初探django-演示charade使用virtualenv来配置项目在centos7下部署