如何搭配 Gunicorn 使用 Django

Gunicorn(「綠色獨角獸」)是一個純 Python 的 UNIX WSGI 伺服器。它沒有任何相依性,可以使用 pip 安裝。

安裝 Gunicorn

執行 python -m pip install gunicorn 安裝 gunicorn。如需更多詳細資訊,請參閱 gunicorn 文件

在 Gunicorn 中將 Django 作為通用 WSGI 應用程式執行

安裝 Gunicorn 後,可以使用 gunicorn 命令啟動 Gunicorn 伺服器進程。Gunicorn 最簡單的調用方式是傳遞一個包含名為 application 的 WSGI 應用程式物件的模組位置,對於典型的 Django 專案而言,它看起來像這樣

gunicorn myproject.wsgi

這將啟動一個進程,運行一個監聽 127.0.0.1:8000 的執行緒。它要求你的專案位於 Python 路徑中;最簡單的確保方式是從與你的 manage.py 檔案相同的目錄中執行此命令。

有關其他提示,請參閱 Gunicorn 的部署文件

返回頂部