上次有一篇写了centos7上Django的安装与服务器的搭建,但每次运行项目都要执行
1python manage.py runserver 0.0.0.0:8000
在开发测试的时候这样是可以的,但是在实际运营中,肯定是不能这样的,所以需要部署一下。
有好几种部署方法,这里就介绍nginx+uwsgi部署的方法。
首先确保服务器上装好了python、pip、django,并确保都可以运行。还没装的可以看我上一篇文章
Django的安装与服务器的搭建
一、安装uwsgi
直接使用pip安装就可以了
1#pip install uwsgi
安装过程中如果出现
Exception: you need a C compiler to builduWSGI
是因为服务器上没有c编译器,先安装
1#yum install -y gcc gcc-c++
安装完成测试是否安装正常
在你django项目下执行
1#uwsgi --http :8000 --module mysite.wsgi
mysite.wsgi指向你的项目的wsgi.py文件
比如我的目录结构为mysite->mysite->wsgi.py,则那条语句在第一个mysite下执行
然后在浏览器输入127.0.0.1:8000,如果出现以下界面,则说明安装正常
二、安装 Nginx
1
2
3
4
5
6wget http://nginx.org/download/nginx-1.5.6.tar.gz tar xf nginx-1.5.6.tar.gz cd nginx-1.5.6 ./configure --prefix=/usr/local/nginx-1.5.6 --with-http_stub_status_module --with-http_gzip_static_module make && make install
可能在./configure编译这一步会出现一个错误:
./configure: error:the HTTP gzip module requires the zlib library.
You can either disable the module by using–without-http_gzip_module
option, or install the zlib library into thesystem, or build the zlib
library
statically from the source with nginx by using–with-zlib=<path> option.
则需要先安装zlib-devel,然后再重新编译,这样边可以解决了
1#yum install -y zlib-devel
安装完成测试是否正常,执行
1#/usr/local/nginx-1.5.6/sbin/nginx
不一定是这样的命令,具体看安装的路径,按我这样装一般就是这条命令
然后在浏览器打开127.0.0.1:80如果出现以下界面,说明安装正常
三、配置uwsgi
uwsgi支持ini、xml等多种配置方式,下面以ini为例,在/etc/目录下新建myuwsgi.ini,并添加以下配置:
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[uwsgi] # Django-related settings # the base directory (full path) # chdir = /path/to/your/project # Django's wsgi file # module = project.wsgi # the virtualenv (full path) # home = /path/to/virtualenv # process-related settings # master master = true # maximum number of worker processes processes = 2 # the socket (use the full path to be safe socket = 127.0.0.1:9090 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
四、配置nginx
找到nginx的安装目录(如:/usr/local/nginx-1.5.6/),打开conf/nginx.conf文件,修改server配置或添加server配置:
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
31server { # the port your site will be served on listen 8000; # the domain name it will serve for server_name 127.0.0.1; # substitute your machine's IP address or FQDN,#这里是填你的域名或ip,然后在浏览器通过这个访问 charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /smile/code/wx_smile/media; # your Django project's media files - amend as required } location /static { alias /smile/code/wx_smile/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass 127.0.0.1:9090; #一定要跟uwsgi配置的一样 include uwsgi_params; # the uwsgi_params file you installed uwsgi_param UWSGI_CHDIR /smile/code/wx_smile; #你的项目的路径,最好用完整路径 uwsgi_param UWSGI_SCRIPT wx_smile.wsgi; #指向wsgi.py,相对于项目的根目录 } }
然后还要在项目里新建media、static两个文件夹
然后执行
1#uwsgi --ini /etc/myuwsgi.ini & /usr/local/nginx-1.5.6/sbin/nginx
如果出现以下情况
说明80端口被占用,nginx已经启动过了,执行
1#/usr/local/nginx-1.5.6/sbin/nginx -s reload重新加载
如果出现
说明9090端口被占用,可能是刚才已经执行过这个
1#uwsgi --ini /etc/myuwsgi.ini
先查看下端口
1#lsof –i :9090
然后杀掉进程
1#kill -9 5457 5459 5460
然后再执行以下命令就可以了
1#uwsgi --ini /etc/myuwsgi.ini
然后在浏览器输入127.0.0.1:8000,如果出现以下界面,说明部署成功
uwsgi文档:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#nginx-and-uwsgi-and-test-py
最后
以上就是可爱大碗最近收集整理的关于nginx+uwsgi部署django的全部内容,更多相关nginx+uwsgi部署django内容请搜索靠谱客的其他文章。
发表评论 取消回复