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

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 server
server {
    # 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值