nginx + uwsgi 搭建 Django web 服务

4 篇文章 0 订阅
1 篇文章 0 订阅

可以直接参考 uwsgi中文文档
以下内容也是我参考文档,自己实践的一些总结。

  1. 创建 虚拟环境 不熟悉的可以参考 这篇文章

  2. 在虚拟环境内安装 uwsgi

    pip install uwsgi
    

    pip install uwsgi 之后,运行uwsgi 报错:

    [uwsgi: command not found]
    

    解决方案:建立软链接 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

在这一步 可以进行uwsgi的测试,查看是否连接成功,我在这就不做了。
需要的移步上面的文档。

  1. 在虚拟环境内 ,安装你的项目所需要的依赖包

    pip install -r requirements.txt
    
  2. 使用 python3 manage.py runserver 0.0.0.0:8000 看项目是否可以正常启动

    1. 云服务器用户 记得添加安全组 开放自定义端口8000
    2. Invalid HTTP_HOST header: 'xxx.xx.xxx.xxx:8000'. You may need to add 'xxx.xx' to ALLOWED_HOSTS
      如果出现以上错误,修改 django项目的 setting文件 ALLOWED_HOSTS = []改为ALLOWED_HOSTS = ['*']
    3. 再次运行 项目应该是可以启动了。但此时 网页的静态文件并没有正常访问到
  3. 在 4 正常启动的情况下,我们就可以使用 uwsgi启动项目了
    请在事先 准备好的虚拟环境中运行!!!

    uwsgi --chdir /your/project/ --home /your/virtualenv/ --http :8000 --module mysite.wsgi:application
    

    此时 静态文件还是没有正常访问到, 稍等解决。

到这里 这个栈 the web client <-> uWSGI <-> Django 工作正常

=======================================================================

  1. 接下来 要安装 nginx ,不熟悉的同学可以参考 这里

  2. 启动 nginx ,浏览器访问出现下面的画面。
    在这里插入图片描述

  3. 在nginx下创建 sites-available 和 sites-enabled 目录

  4. 在/nginx/nginx.conf配置文件中的http区块添加如下行:

    include ....../nginx/sites-enabled/*;
    
  5. 所有的虚拟主机配置文件放在sites-available目录,然后你可以在sites-enabled目录创建软链接指向sites-available里的配置文件
    ln -s ...nginx/sites-available/xxx.conf .../nginx/sites-available/xxx.conf

    使用软连接 要使用 绝对路径

  6. 在sites-available 中创建 mysite_nginx.conf 文件,内容如下:

    #mysite_nginx.conf
    
    #the upstream component nginx needs to connect to
    #upstream django {
        # server unix:///path/to/your/mysite/mysite.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      80;
        # the domain name it will serve for
        server_name mxonline; # 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 /your/project/media;  # your Django project's media files - amend as required
        }
    
        location /static {
            alias /your/project/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:8001; **这是nginx与uwsgi通信的端口**
            include     /your/project/uwsgi_params; # the uwsgi_params file you installed
        }
    }
    
  7. 由于每次启动 uwsgi 都需要在命令行 指定参数 很麻烦,所以我们可以通过从 ini 文件读取配置启动 uwsgi。
    在这里需要在 系统环境 安装 uwsgi 添加软连接 到 /usr/bin/ 上面讲过了。
    我在我的项目根目录下 新建了 mysite_uwsgi.ini
    内容如下:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /your/project
# Django's wsgi file
module          = ProjectName.wsgi:application
# the virtualenv (full path)
home            = /your/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 4
max-requests    = 5000
harakiri        = 60
# the socket (use the full path to be safe
#socket          = /your/project/ProjectName.sock
socket          = 127.0.0.1:8001
uid             = root
gid             = root
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

pidfile         = /your/project/master.pid
daemonize       = /your/project/xxx.log
  1. 启动 uwsgi uwsgi --ini /your/project/mysite_uwsgi.ini
    15.

  2. 启动nginx , 启动之前可以通过 -t 查看 nginx 状态
    在这里插入图片描述

  3. 到这里 nginx 和 uwsgi 全部启动了 . 在浏览器尝试 输入 ip:80 访问

可能出现的问题

  1. 403 错误
    在这里插入图片描述
    查看 nginx errors.log 日志 发现
    在这里插入图片描述
    调查
    在这里插入图片描述
  2. 发现启动 nginx的用户是nobody
  3. 修改 nginx.conf 文件 将用户 设置为 root
    在这里插入图片描述
  4. 重启 nginx ./nginx -s reload
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值