Django + nginx + uwsgi 服务器部署

这个教训告诉我们,不要随便相信别人博客里的东西,文档还是官方的靠谱 ___

1. 安装Django + nginx + uwsgi

八仙过海-各显神通,安装的版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-!- » python --version                                
Python 2.7.5
-!- ~ » uwsgi --version
2.0.15
!- ~ » nginx -v
nginx version: nginx/1.10.2
-!- Server/HelloWorld » python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 11, 3, u'final', 0)
>>>

2. 创建第一个项目

1
django-admin.py startproject HelloWorld

修改HelloWorld/settings.py,修改参数ALLOWED_HOSTS

1
ALLOWED_HOSTS = ['*']

3. 在工程目录文件中,创建两个配置文件 uwsgi.xmldjango_wsgi.py

uwsgi.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<uwsgi>
<socket>0.0.0.0:8000</socket>
<listen>20</listen>
<master>true</master>
<pidfile>/etc/nginx/uwsgi.pid</pidfile>
<processes>2</processes>
<module>django_wsgi</module> #这个文件下面要建立
<pythonpath>/root/Server/HelloWorld</pythonpath> #刚才建立项目的路径
<profiler>true</profiler>
<memory-report>true</memory-report>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<limit-as>6048</limit-as>
</uwsgi>

django_wsgi.py [版本不一样,配置不一样,这个地方有遇到很多的坑]

1
2
3
4
5
import os
import sys
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'HelloWorld.settings'
application = get_wsgi_application()

4. 服务器搭建好之后,css之类的文件 会丢失,这个时候需要修改HelloWorld/settings.py,建立本地的静态文件,添加STATIC_ROOT = ‘static’

1
2
3
4
#Static files (CSS, JavaScript, Images)
#https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_ROOT = 'static'
STATIC_URL = '/static/'

执行python manage.py collectstatic

1
python manage.py collectstatic

5. 修改nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.baidu.com;#域名或者IP
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ~/static/ {
root /root/Server/HelloWorld;
break;
}
location / {
root /root/Server/HelloWorld;
uwsgi_pass 127.0.0.1:8000;
include uwsgi_params;
access_log off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

6. 开启nginx uwsgi

1
2
/sbin/nginx -s reload
uwsgi -x /root/Server/HelloWorld/uwsgi.xml &

7. OK

注意,要打开网络端口

1
2
3
4
5
#开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=8000/tcp --permanent
#重启防火墙
firewall-cmd --reload

工程树(以前不会看,后来发现,这个树还是挺有用的)

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
-!- Server/HelloWorld » tree                                              
.
├── db.sqlite3
├── django_wsgi.py
├── django_wsgi.pyc
├── HelloWorld
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── 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
└── uwsgi.xml

12 directories, 74 files