nginx+uwsgi部署django

上次有一篇写了centos7上Django的安装与服务器的搭建,但每次运行项目都要执行

python manage.py runserver 0.0.0.0:8000

在开发测试的时候这样是可以的,但是在实际运营中,肯定是不能这样的,所以需要部署一下。

有好几种部署方法,这里就介绍nginx+uwsgi部署的方法。

 

首先确保服务器上装好了python、pip、django,并确保都可以运行。还没装的可以看我上一篇文章

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 mysite.wsgi

mysite.wsgi指向你的项目的wsgi.py文件

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

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

nginx+uwsgi部署django_第1张图片

二、安装 Nginx

wget 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= option.

则需要先安装zlib-devel,然后再重新编译,这样边可以解决了

#yum install -y zlib-devel


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

#/usr/local/nginx-1.5.6/sbin/nginx

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

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

nginx+uwsgi部署django_第2张图片

三、配置uwsgi

uwsgi支持inixml等多种配置方式,下面以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      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,相对于项目的根目录
    }
}

然后还要在项目里新建mediastatic两个文件夹

然后执行

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

如果出现以下情况


说明80端口被占用,nginx已经启动过了,执行

#/usr/local/nginx-1.5.6/sbin/nginx -s reload重新加载


如果出现


说明9090端口被占用,可能是刚才已经执行过这个

#uwsgi --ini /etc/myuwsgi.ini

先查看下端口

#lsof –i :9090


然后杀掉进程

#kill -9 5457 5459 5460

然后再执行以下命令就可以了

#uwsgi --ini /etc/myuwsgi.ini


然后在浏览器输入127.0.0.1:8000,如果出现以下界面,说明部署成功

nginx+uwsgi部署django_第3张图片


uwsgi文档:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#nginx-and-uwsgi-and-test-py


你可能感兴趣的:(python)