https://github.com/lannyMa/django-uwsgi-nginx.gitcss
- 克隆代碼進入項目 git clone https://github.com/lannyMa/django-blog-tutorial.git cd django-blog-tutorial - 建立並進入虛擬環境 pip install virtualenv virtualenv blogproject_env - 若是須要mysql-devel yum install -y python-devel mysql-devel pip install MySQL-python - 安裝項目依賴 pip install -r requirements.txt - 同步數據庫,建立超級管理員 python manage.py migrate python manage.py createsuperuser - 運行 python manage.py runserver - 訪問 http://127.0.0.1:8000 http://127.0.0.1:8000/admin
代碼沒問題後,考慮部署到生產html
本身打docker image,容器化部署python
dockerfile有py3和py3的環境, supervisor啓動nginx+uwsgimysql
docekr pull lanny/blog-uwsgi-py3-django1.10 docker run -d -p 8080:80 lanny/blog-uwsgi-py3-django1.10 http://x:8080 來訪問,已測過沒問題
- nginx啓動 /usr/sbin/nginx 看下nginx.conf配置 # Django media location /media { alias /home/docker/code/app/blog/media; # your Django project's media files - amend as required } location /static { alias /home/docker/code/app/blog/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /home/docker/code/uwsgi_params; # the uwsgi_params file you installed } - 原來nginx把歸py處理的uwsgi_pass發給了django的uwsgi,uwsgi用uwsgi_params解析 uwsgi.ini配置文件 [base] chdir = %dapp/ module=blogproject.wsgi:application chmod-socket=666 - uwsgi啓動 /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini #啓動後會監聽端口供與nginx通訊 - 文末附錄有nginx與wsgi通訊原理介紹.
搞清楚了這些後,更近一步着手製做uwsgi+django的docker鏡像.nginx
參考地址:這裏有python2 和python3版本的環境.能夠知足平常需求了.
https://hub.docker.com/r/dockerfiles/django-uwsgi-nginx/
https://github.com/dockerfiles/django-uwsgi-nginxgit
完整的dockerfile和相關配置的關鍵部分以下github
- dockerfile FROM ubuntu:16.04 MAINTAINER Dockerfiles # Install required packages and remove the apt packages cache when done. RUN apt-get update && \ apt-get upgrade -y && \ apt-get install -y \ git \ python3 \ python3-dev \ python3-setuptools \ python3-pip \ python3-dev \ libmysqlclient-dev \ nginx \ supervisor \ sqlite3 && \ pip3 install -U pip setuptools && \ rm -rf /var/lib/apt/lists/* # install uwsgi now because it takes a little while RUN pip3 install uwsgi # setup all the configfiles RUN echo "daemon off;" >> /etc/nginx/nginx.conf COPY nginx-app.conf /etc/nginx/sites-available/default COPY supervisor-app.conf /etc/supervisor/conf.d/ # COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism # to prevent re-installing (all your) dependencies when you made a change a line or two in your app. COPY app/requirements.txt /home/docker/code/app/ RUN pip3 install -r /home/docker/code/app/requirements.txt # add (the rest of) our code COPY . /home/docker/code/ RUN git clone https://github.com/lannyMa/django-blog-tutorial-deploy.git tmp && \ mv tmp/* /home/docker/code/app/ && \ rm -rf tmp # install django, normally you would remove this step because your project would already # be installed in the code/app/ directory #RUN django-admin.py startproject website /home/docker/code/app/ EXPOSE 80 CMD ["supervisord", "-n"] - supervisor配置 [program:app-uwsgi] command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini [program:nginx-app] command = /usr/sbin/nginx uwsgi.ini配置文件 [base] chdir = %dapp/ module=blogproject.wsgi:application chmod-socket=666
Dockerfile RUN git clone https://github.com/lannyMa/django-blog-tutorial-deploy.git tmp && \ mv tmp/* /home/docker/code/app/ && \ rm -rf tmp
settings.py ALLOWED_HOSTS = ['*']
nginx.conf location /media { alias /home/docker/persistent/media; # your Django project's media files - amend as required } location /static { alias /home/docker/volatile/static; # your Django project's static files - amend as required }
等這一切修改完成後,開始構建鏡像web
docker build -t webapp . docker run -d -p 80:80 webapp - 運行後訪問便可 docker run -d -p 8080:80 webapp
解決: 修改nginx配置爲本身項目static的地址sql
location /media { alias /home/docker/persistent/media; # your Django project's media files - amend as required } location /static { alias /home/docker/volatile/static; # your Django project's static files - amend as required }
解決: 修改settings.pydocker
ALLOWED_HOSTS = ['寫本身服務器的ip便可']
修改uwsgi.ini
修改成本身項目的地址,
module=blogproject.wsgi:application
這裏uwsgi調用的是項目的這個
nginx-app.conf
# nginx-app.conf # the upstream component nginx needs to connect to upstream django { server unix:/home/docker/code/app.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, default_server indicates that this server block # is the block to use if no blocks match the server_name listen 80 default_server; # the domain name it will serve for server_name .example.com; # 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/docker/code/app/blog/media; # your Django project's media files - amend as required } location /static { alias /home/docker/code/app/blog/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /home/docker/code/uwsgi_params; # the uwsgi_params file you installed } }
uwsgi.ini
[uwsgi] # this config will be loaded if nothing specific is specified # load base config from below ini = :base # %d is the dir this configuration file is in socket = %dapp.sock master = true processes = 4 [dev] ini = :base # socket (uwsgi) is not the same as http, nor http-socket socket = :8001 [local] ini = :base http = :8000 # set the virtual env to use home=/Users/you/envs/env [base] chdir = %dapp/ module=blogproject.wsgi:application chmod-socket=666
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django gunicorn vs uwsgi是兩種不一樣的appserver gunicorn vs uwsgi: http://fatelei.github.io/2015/07/05/Gunicorn-vs-uwsgi/ uwsgi在高併發下比gunicorn有更好的吞吐量和更少的錯誤數