阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ubuntu上通过Nginx部署Django笔记

183次阅读
没有评论

共计 3308 个字符,预计需要花费 9 分钟才能阅读完成。

Django 的部署可以有很多方式,采用 nginx+uwsgi 的方式是其中比较常见的一种方式。今天在 Ubuntu 上使用 Nginx 部署 Django 服务,虽然不是第一次搞这个了,但是发现还是跳进了好多坑,Google 了好久才搞定。想想还是把这个过程记录下来,免得下次再来踩同样的坑。

安装 Nginx

apt-get install nginx

ubantu 安装完 Nginx 后,文件结构大致为:
所有的配置文件都在 /etc/nginx 下;
启动程序文件在 /usr/sbin/nginx 下;
日志文件在 /var/log/nginx/ 下,分别是 access.log 和 error.log;
并且在 /etc/init.d 下创建了启动脚本 nginx。

sudo /etc/init.d/nginx start    # 启动
sudo /etc/init.d/nginx stop     # 停止
sudo /etc/init.d/nginx restart  # 重启

安装 uwsgi

apt-get install Python-dev
pip install uwsgi

至于为什么要使用 uwsgi, 可以参见这边博客:快速部署 Python 应用:Nginx+uWSGI 配置详解。
这样大体的流程是:nginx 作为服务器最前端,负责接收 client 的所有请求,统一管理。静态请求由 Nginx 自己处理。非静态请求通过 uwsgi 传递给 Django,由 Django 来进行处理,从而完成一次 WEB 请求。
通信原理是:
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django

测试 uwsgi

在 Django 项目下新建 test.py 文件,

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"] # python2
    #return [b"Hello World"] # python3

然后执行 shell 命令:

uwsgi --http :8001 --plugin python --wsgi-file test.py

加上 –plugin python 是告诉 uWSGI 在使用 python 插件,不然很有可能会出现类似这样的错误:

uwsgi: unrecognized option '--wsgi-file'
getopt_long() error

执行成功在浏览器中打开:http://localhost:8001 显示 Hello World 说明 uwsgi 正常运行。

测试 Django

首先得保证 Django 项目没有问题

python manage.py runserver 0.0.0.0:8001

访问 http://localhost:8001, 项目运行正常。
然后链接 Django 和 uwsgi,实现简单的 web 服务器,到 Django 项目目录下执行 shell:

uwsgi --http :8001 --plugin python --module blog.wsgi

blog 为你的项目名。访问 http://localhost:8001,项目正常。注意这时项目的静态文件是不会被加载的,需要用 nginx 做静态文件代理。

配置 uwsgi

uwsgi 支持通过配置文件的方式启动,可以接受更多的参数,高度可定制。我们在 Django 项目目录下新建 uwsgi.ini

# Django-related settings

socket = :8001

# the base directory (full path)
chdir           = /home/ubuntu/blog

# Django s wsgi file
module          = blog.wsgi

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 4

# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

在 shell 中执行:

sudo uwsgi --ini uwsgi.ini 

ps: 如果实在不想配置 nginx 的话,单 uwsgi 就已经能完成部署了(把 socket 换成 http),你可以把 Django 中的静态文件放到云平台中如七牛等等,这样你的 Web 也能被正常访问。

配置 nginx

nginx 默认会读取 /etc/nginx/sites-enabled/default 文件中的配置,修改其配置如下:

server {# the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name 127.0.0.1; # 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 /home/ubuntu/blog/media;  # your Django project's media files - amend as required
    }

    location /static {alias /home/ubuntu/blog/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {include     uwsgi_params; # the uwsgi_params file you installed
        uwsgi_pass 127.0.0.1:8001;
    }
}

收集 Django 静态文件

把 Django 自带的静态文件收集到同一个 static 中,不然访问 Django 的 admin 页面会找不到静态文件。在 django 的 setting 文件中,添加下面一行内容:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后到项目目录下执行:

python manage.py collectstatic

运行

一切配置好后直接重启 nginx 即可。更加详细的说明请参见官方文档

可能遇到的问题

如果监听 80 端口,部署后访问 localhost 自动跳转到 Nginx 默认的欢迎界面

uwsgi: option‘–http‘is ambiguous

Ubuntu 14.04 下 Django+MySQL 安装部署全过程 http://www.linuxidc.com/Linux/2016-02/128714.htm

Django1.8 返回 json 字符串和接收 post 的 json 字符串内容  http://www.linuxidc.com/Linux/2015-07/120226.htm

如何使用 Docker 组件开发 Django 项目?http://www.linuxidc.com/Linux/2015-07/119961.htm

Ubuntu Server 12.04 安装 Nginx+uWSGI+Django 环境 http://www.linuxidc.com/Linux/2012-05/60639.htm 

Django+Nginx+uWSGI 部署 http://www.linuxidc.com/Linux/2013-02/79862.htm 

Django 实战教程 http://www.linuxidc.com/Linux/2013-09/90277.htm 

Django Python MySQL Linux 开发环境搭建 http://www.linuxidc.com/Linux/2013-09/90638.htm 

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/137831.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3308字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中