# docker-django-nginx-uwsgi-postgres-tutorial
[English version - README.en.md](https://github.com/twtrubiks/docker-django-nginx-uwsgi-postgres-tutorial/blob/master/README.en.md)
Docker + Django + Nginx + uWSGI + Postgres 基本教學 - 從無到有
教你用 [Docker](https://www.docker.com/) 建立 [Django](https://www.djangoproject.com/) + [Nginx](https://nginx.org/en/) + [uWSGI](https://uwsgi-docs.readthedocs.io/en/latest/) + [PostgreSQL](https://www.postgresql.org/) 📝
建議對 [Docker](https://www.docker.com/) 還不熟的朋友,可以先參考我之前寫的
[Docker 基本教學 - 從無到有 Docker-Beginners-Guide 教你用 Docker 建立 Django + PostgreSQL 📝](https://github.com/twtrubiks/docker-tutorial)
* [Youtube Tutorial PART 1 - Docker + Django + Nginx + uWSGI + Postgres - 簡介](https://youtu.be/u4XIMTOsxJk)
* [Youtube Tutorial PART 2 - Docker + Django + Nginx + uWSGI + Postgres - 原理步驟](https://youtu.be/9K4O1UuaXrU)
* [Youtube Tutorial PART 3 - Docker + Django + Nginx + uWSGI + Postgres - 實戰](https://youtu.be/v7Mf9TuROnc)
## 簡介
### [Docker](https://www.docker.com/)

之前介紹過了,這邊就不在介紹 :stuck_out_tongue_closed_eyes:
請參考
[Docker 基本教學 - 從無到有 Docker-Beginners-Guide 教你用 Docker 建立 Django + PostgreSQL 📝](https://github.com/twtrubiks/docker-tutorial)
### [Django](https://github.com/django/django)
請參考
[Django 基本教學 - 從無到有 Django-Beginners-Guide 📝](https://github.com/twtrubiks/django-tutorial)
[Django-REST-framework 基本教學 - 從無到有 DRF-Beginners-Guide 📝](https://github.com/twtrubiks/django-rest-framework-tutorial)
更多 Django 的範例可以參考我的 [Github](https://github.com/twtrubiks?utf8=%E2%9C%93&tab=repositories&q=Django&type=&language=),這裡我就列出比較基本的兩篇就好:relaxed:
### [PostgreSQL](https://www.postgresql.org/)

### [Nginx](https://nginx.org/en/)

Nginx 是一種 Web Server,使用資源少且穩定性高,穩定性高這部分可參考
Nginx 解決了 **C10K** 問題,什麼是 **C10K**? 原文可參考 [The C10K problem](http://www.kegel.com/c10k.html) ,
**C10K** 就是 Client 10000 的問題,在過去,如果同時連接到 Server 的 Client
端數超過 10000 中,可能無法正常提供服務。
Ngnix 本身沒辦法處理動態內容,所以必須另外設定 [uWSGI](https://uwsgi-docs.readthedocs.io/en/latest/) 來處理之間的互動
,參考下方流程 ( 重要 )
:star: the web client <-> the web server ( Nginx ) <-> unix socket <-> uWSGI <-> Django :star:
你可能會問我,uWSGI 這個是什麼 :confused:
uWSGI 是一種通信協議,可以把它想成是一種接口 ( 和 Django 進行溝通 ),
通常 Django 程式會放在 http server( Nginx )上,那當 server 接收到
request 時,該怎麼將這些數據傳遞(轉換)給 Django 呢 ?
這就是 uWSGI 的功能 :wink:
那為什麼還需要 Nginx 呢 :confused:
先了解一個觀念,
Nginx 負責靜態內容(html js css 圖片...... ),uWSGI 負責 Python 的動態內容。
uWSGI 對於靜態內容處理的並不是很好( 效能差 ),所以我們可以透過
Nginx 來處理靜態內容,而且使用 Nginx 還有很多好處,
* Nginx 比起 uWSGI 能更好地處理靜態資源
* Nginx 可以設定 Cache 機制
* Nginx 可以設定 反向代理器
* Nginx 可以進行多台機器的負載平衡( Load balance )
溫馨小提醒:heart:
如果你想更進一步的了解**反向代理器**,可參考 [正向代理器 VS 反向代理器](https://github.com/twtrubiks/docker-django-nginx-uwsgi-postgres-load-balance-tutorial#%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86%E5%99%A8--vs-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E5%99%A8) 的說明 :smile:
看到這邊你可能會問我?
那為什麼我在本機開發時,都不需要有 Nginx 以及 uWSGI 就可以執行呢 :confused:
當你在開發 Django 時,我們通常都是用 `python manage.py runserver`
去執行,當你執行這段指令時,其實他就是幫你建立一個小型的 http server ,
當然,這只是開發方便,正式環境是不會這樣使用的( 更何況效能的部份 :disappointed_relieved: )
疑 :confused: 好像有 Gunicorn 這個東西,之前有講過 Gunicorn ,可參考
[Deploying_Django_To_Heroku_Tutorial](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial)
[Deploying-Flask-To-Heroku](https://github.com/twtrubiks/Deploying-Flask-To-Heroku)
那為什麼不用 Gunicorn,要用 uWSGI 呢?
那時候會使用 Gunicorn,是因為在 Heroku 裡,官方建議使用 Gunicorn 來
啟動 web server,至於 Gunicorn 和 uWSGI 哪個比較好,我覺得要依照自
己的使用情境下去選擇 :wink:
等等,既然都講到了 Nginx,不是還有 [Apache](https://httpd.apache.org/),聽說那個好像很多人在用 :stuck_out_tongue_winking_eye:
可能有人會問,那我要選 [Nginx](https://nginx.org/en/) 還是 [Apache](https://httpd.apache.org/) :confused:
我認為沒有哪個 Server 最好,重點在你的需求下,哪個 Server 最符合你的情境(需求),
你就選他 :smiley:
## 教學
這次我將利用 Docker 建立 3 個容器( Containers ),分別為 Nginx、Django + uWSGI 、Postgres
我主要是參考 [https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html](https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html) 這篇教學,
但些許部分不太一樣 :smirk:
這次的重點會放在 Nginx 以及 Django + uWSGI 設定的部份,
**Nginx 的部份**,可參考 Nginx 資夾中的 [Dockerfile](https://github.com/twtrubiks/docker-django-nginx-uswgi-postgres-tutorial/blob/master/nginx/Dockerfile)
```Dockerfile
FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY my_nginx.conf /etc/nginx/sites-available/
RUN mkdir -p /etc/nginx/sites-enabled/\
&& ln -s /etc/nginx/sites-available/my_nginx.conf /etc/nginx/sites-enabled/
# RUN mkdir -p /etc/nginx/sites-enabled/\
# && ln -s /etc/nginx/sites-available/my_nginx.conf /etc/nginx/sites-enabled/\
# && rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
```
解釋一下裡面的步驟,
第一步
先將 nginx.conf 複製到 `/etc/nginx/nginx.conf` 的路徑,
( 原始的 nginx.conf 可以從 Docker 的 Nginx 容器中取得,在 `/etc/nginx` 路徑下取得 nginx.conf )
我有複製一份原始的出來 [nginx_origin.conf](https://github.com/twtrubiks/docker-django-nginx-uswgi-postgres-tutorial/blob/master/nginx/nginx_origin.conf) :smiley:
nginx.conf 主要是修改兩個部分,
一部分是將 user 從 nginx 修改為 root,
```conf
user root;
```
另一部份是
```conf
# include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*;
```
增加一行 `include /etc/nginx/sites-available/*;`
並且將 `include /etc/nginx/conf.d/*.conf;` 這行註解掉,
這樣在 Nginx 資料夾中的 [Dockerfile](https://github.com/twtrubiks/docker-django-nginx-uswgi-postgres-tutorial/blob/master/nginx/Dockerfile) 就不用再執行刪除 default.conf 的指令,
因為 `include /etc/nginx/conf.d/*.conf;` 它是默認會跑的頁面,
但我們現在要設定自己的 :smirk:
第二步
將 my_nginx.conf 複製到 `/etc/nginx/sites-available/` 裡面,
這邊我們先暫停一下,
如果你是用 `FROM nginx:latest` 的方式安裝 Nginx,你會發現你沒有以下兩個路徑
`/etc/nginx/sites-available/`
`/etc/nginx/sites-enabled/`
但不要擔心,沒有我們就自己建立 ( 也就是 Nginx 資料夾中的 Dockerfile 所執行的指令 ) ,
但為什麼我們沒有這些路徑呢 :confused:
原因是這些默認路徑似乎是要用 `apt-get` 的方式安裝 Nginx 才會有默認的路徑。
第三

程序员进化不脱发!
- 粉丝: 1w+
最新资源
- 本库是个基于python的工具集,用于记录数据到文件。 使用方便,代码简洁, 是一个可靠、省心且实用的工具。 支持多线程同时写入。.zip
- 本科毕业设计,基于python的图像复制粘贴篡改识别软件。.zip
- 本项目是基于计算机视觉的端到端交通路口智能监控系统.采用的设计架构由SRS
- 碧蓝航线ios平台自动脚本,基于python+opencv+facebook_wda实现.zip
- 毕业设计中基于给定微博数据的反作弊识别,用python开发。.zip
- 毕业设计项目,基于深度学习的实时语义分割算法研究,python实现。.zip
- 对基于python的微博爬虫进行重写,重写语言:java.zip
- 此框架是基于Python+Pytest+Requests+Allure+Yaml+Json实现全链路接口自动化测试
- 程序语言课程作业在线评测平台(实现Java、C、Python的选择、填空、代码题在线评测),基于SpringBoot+Layui+MySQL实现.zip
- 非官方的科大讯飞语音合成(用于朗读,配音场景)python API (基于官方demo增加了:超过2000字上限自动分割再合并音频的功能).zip
- 非官方的简易中国铁路列车运行图系统,基于Python + PyQt5
- 超市POS销售与后台管理系统_商品录入收银业务会员管理进货销售库存人员权限断网收银断电保护_实现超市前台POS销售商品扫描条形码输入收银计算找零打印清单会员折扣累计消费以及后台管理.zip
- 俄罗斯方块闯关版,基于Python实现.zip
- 该项目是基于Python和数据库实现的学生信息管理系统.zip
- 该仓库为agv系统调度软件的前后端实现。项目基于fastapi(python后端框架)和vue2实现了RESTful风格的前后端分离.zip
- 该项目是基于Scrapy框架的Python新闻爬虫,能够爬取网易,搜狐,凤凰和澎湃网站上的新闻,将标题,内容,评论,时间等内容整理并保存到本地.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0