Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简单使用 Gunicorn 托管 Django #156

Open
Qingquan-Li opened this issue Sep 3, 2020 · 0 comments
Open

简单使用 Gunicorn 托管 Django #156

Qingquan-Li opened this issue Sep 3, 2020 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

一、把 Django 当作普通 WSGI 应用在 Gunicorn 中运行

参考:https://docs.djangoproject.com/zh-hans/2.2/howto/deployment/wsgi/gunicorn/

https://docs.gunicorn.org/en/latest/run.html#django

安装 Gunicorn 之后,可以使用 gunicorn 命令启动 Gunicorn 服务进程。最简模式下,只需要把包含了 WSGI 应用对象的 application 模块位置告诉 gunicorn,就可以启动了。因此对于典型的 Django 项目,像这样来调用 gunicorn:

# 在 manage.py 文件所在的目录(项目根目录)中运行命令:
$ gunicorn myproject.wsgi

这样会创建一个进程,包含了一个监听在 127.0.0.1:8000 的线程。


开发时提供静态文件服务

参考:https://docs.djangoproject.com/zh-hans/2.2/howto/static-files/#serving-static-files-during-development

使用以上命令启动 Gunicorn 并不能加载 CSS 等静态文件,需要执行以下操作才能加载静态文件(注意:这不适合生产环境,生产环境请使用 Nginx 等代理服务器提供静态文件服务):

例如,若 STATIC_URL/static/,你能通过添加以下代码片段至 urls.py 完成目的:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

二、Gunicorn 常用参数

参考:https://docs.gunicorn.org/en/latest/run.html#commonly-used-arguments

实例:绑定 IP 地址:

$ gunicorn --bind 192.168.x.xx:8000 myproject.wsgi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant