nginx+uwsgi部署django

Posted 前进的卒子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+uwsgi部署django相关的知识,希望对你有一定的参考价值。

一、安装uwsgi

直接使用pip安装就可以了

#pip install uwsgi    

安装过程中如果出现

Exception: you need a C compiler to builduWSGI

是因为服务器上没有c编译器,先安装

#yum install -y gcc gcc-c++  

安装完成测试是否安装正常

在你django项目下执行

#uwsgi --http :8000 --module django_project.wsgi  
 django_project.wsgi  指向你的项目的wsgi.py文件

比如我的目录结构为django_project ->django_project ->wsgi.py,则那条语句在第一个django_project 下执行

然后在浏览器输入127.0.0.1:8000,如果出现以下界面,则说明安装正常

 

二、安装 nginx

# cd downloadfile
# wget http://nginx.org/download/nginx-1.10.3.tar.gz  
# tar xf nginx-1.10.3.tar.gz  
# cd nginx-1.10.3  
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module  
#make && make install 

 

可能在./configure编译这一步会出现一些错误:

1、./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,然后再重新编译,这样边可以解决了

yum install -y zlib-devel 

 

2、./configure:  error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题

yum -y install pcre-devel

3、错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决办法:

yum  -y install openssl openssl-devel

安装完成测试是否正常,执行

#/usr/local/nginx/sbin/nginx  

不一定是这样的命令,具体看安装的路径,按我这样装一般就是这条命令

然后在浏览器打开127.0.0.1:80如果出现以下界面,说明安装正常

 

三、配置uwsgi

uwsgi支持ini、xml等多种配置方式,下面以ini为例,在/etc/目录下新建myuwsgi.ini,并添加以下配置:

[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配置

server {     
# the port your site will be served on     
listen 80;      
# the domain name it will serve for 
   
server_name 127.0.0.1,bradball.xin; #这里是填你的域名或ip,然后在浏览器通过这个访问  

charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /home/brad/blog_project; # your Django project\'s media files - amend as required } location /static/ { alias /home/brad/blog_project; # 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 /home/brad/blog_project; #你的项目的路径,最好用完整路径
uwsgi_param UWSGI_SCRIPT blog_project.wsgi; #指向wsgi.py,相对于项目的根目录
    }
}

 然后执行:

sudo uwsgi --ini /etc/myuwsgi.ini & /usr/local/nginx/sbin/nginx

 重新加载 nginx

#/usr/local/nginx/sbin/nginx -s reload

 

以上是关于nginx+uwsgi部署django的主要内容,如果未能解决你的问题,请参考以下文章

nginx + uwsgi 部署 Django+Vue项目

debian完整部署 Nginx + uWSGI + Django

Django + Uwsgi + Nginx 实现生产环境部署

centos7下部署Django(nginx+uWSGI+Python3+Django)

django+nginx+uwsgi的生产环境部署(Ubuntu16.04)

Django部署——uwsgi+Nginx(超详细)