Django + nginx + uwsgi配置和环境搭建(ubuntu)

来源:互联网 发布:文职将军知乎 编辑:程序博客网 时间:2024/05/29 02:04

Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。
在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求。nginx把所有静态请求自己来处理(这是NGINX的强项)。然后,NGINX将所有非静态请求通过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。

uwsgi官网:http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
我就不逐条翻译了,按照官网流程下来,一直不成功(django中的conf文件没有refer成功,nginx并不会加载它,导致配置的nginx路径和端口一直没有起来),中文网站多数也是翻译的官网内容,没什么实际的意义
最后的解决办法:直接在/etc/nginx/site-available/default中配置server信息,然后重启nginx: sudo service nginx restart,搞定

首先按照官网步骤,保证每个模块都能正常运行,相关配置如下:

假定项目网站的名字叫做:domain_seo_tool

uwsgi_socket.xml配置(网站根目录下)

<uwsgi>    <!-- <socket>:8001</socket> -->    <socket>domain_seo_tool.sock</socket>    <chdir>/var/www/domain_seo_tool</chdir>    <module>domain_seo_tool.wsgi</module>    <processes>4</processes> <!-- 进程数 -->    <daemonize>uwsgi.log</daemonize></uwsgi>

使用下列代码运行,带起uwsgi服务(也可以配置成开机启动的方式)

uwsgi -x domain_seo_tool.xml

nginx的配置信息(/etc/nginx/site-available/default):

upstream django {    server unix:///var/www/domain_seo_tool/domain_seo_tool.sock; # for a file socket    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)}# configuration of the serverserver {    # the port your site will be served on    listen      8000;    # the domain name it will serve for    server_name {name}; # substitute your machine's IP address or FQDN    charset     utf-8;    # max upload size    client_max_body_size 75M;   # adjust to taste    # Django media    location /media  {        alias /var/www/domain_seo_tool/media;  # your Django project's media files - amend as required    }    location /static {        alias /var/www/domain_seo_tool/static; # your Django project's static files - amend as required    }    # Finally, send all non-media requests to the Django server.    location / {        uwsgi_pass  django;        include     /var/www/domain_seo_tool/uwsgi_params; # the uwsgi_params file you installed    }}
1 0
原创粉丝点击