https://github.com/lannyMa/django-uwsgi-nginx.git

单机调试启动-确保项目代码没问题

  1. - 克隆代码进入项目
  2. git clone https://github.com/lannyMa/django-blog-tutorial.git
  3. cd django-blog-tutorial
  4. - 创建并进入虚拟环境
  5. pip install virtualenv
  6. virtualenv blogproject_env
  7. - 如果需要mysql-devel
  8. yum install -y python-devel mysql-devel
  9. pip install MySQL-python
  10. - 安装项目依赖
  11. pip install -r requirements.txt
  12. - 同步数据库,创建超级管理员
  13. python manage.py migrate
  14. python manage.py createsuperuser
  15. - 运行
  16. python manage.py runserver
  17. - 访问
  18. http://127.0.0.1:8000
  19. http://127.0.0.1:8000/admin



代码没问题后,考虑部署到生产

生产部署-nginx+uwsgi+django10

部署详情说明

docker镜像(baseimage+code)代码我放到镜像里了,直接run镜像就可以跑起来直接访问了. 先快速跑一下吧.

  1. docekr pull lanny/blog-uwsgi-py3-django1.10
  2. docker run -d -p 8080:80 lanny/blog-uwsgi-py3-django1.10
  3. http://x:8080 来访问,已测过没问题

探究下怎么制作docker镜像

  • 先搞清楚nginx+uwsgi+django物理机上是怎么配合工作的,确保手动一步步启动完成没问题,然后再做镜像
  1. - nginx启动 /usr/sbin/nginx
  2. 看下nginx.conf配置
  3. # Django media
  4. location /media {
  5. alias /home/docker/code/app/blog/media; # your Django project's media files - amend as required
  6. }
  7. location /static {
  8. alias /home/docker/code/app/blog/static; # your Django project's static files - amend as required
  9. }
  10. # Finally, send all non-media requests to the Django server.
  11. location / {
  12. uwsgi_pass django;
  13. include /home/docker/code/uwsgi_params; # the uwsgi_params file you installed
  14. }
  15. - 原来nginx把归py处理的uwsgi_pass发给了djangouwsgi,uwsgiuwsgi_params解析
  16. uwsgi.ini配置文件
  17. [base]
  18. chdir = %dapp/
  19. module=blogproject.wsgi:application
  20. chmod-socket=666
  21. - uwsgi启动
  22. /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini #启动后会监听端口供与nginx通信
  23. - 文末附录有nginxwsgi通信原理介绍.

搞清楚了这些后,更近一步着手制作uwsgi+django的docker镜像.

定制属于自己项目的docker镜像

参考地址:这里有python2 和python3版本的环境.可以满足日常需求了.

https://hub.docker.com/r/dockerfiles/django-uwsgi-nginx/

https://github.com/dockerfiles/django-uwsgi-nginx

完整的dockerfile和相关配置的关键部分如下

  1. - dockerfile
  2. FROM ubuntu:16.04
  3. MAINTAINER Dockerfiles
  4. # Install required packages and remove the apt packages cache when done.
  5. RUN apt-get update && \
  6. apt-get upgrade -y && \
  7. apt-get install -y \
  8. git \
  9. python3 \
  10. python3-dev \
  11. python3-setuptools \
  12. python3-pip \
  13. python3-dev \
  14. libmysqlclient-dev \
  15. nginx \
  16. supervisor \
  17. sqlite3 && \
  18. pip3 install -U pip setuptools && \
  19. rm -rf /var/lib/apt/lists/*
  20. # install uwsgi now because it takes a little while
  21. RUN pip3 install uwsgi
  22. # setup all the configfiles
  23. RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  24. COPY nginx-app.conf /etc/nginx/sites-available/default
  25. COPY supervisor-app.conf /etc/supervisor/conf.d/
  26. # COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
  27. # to prevent re-installing (all your) dependencies when you made a change a line or two in your app.
  28. COPY app/requirements.txt /home/docker/code/app/
  29. RUN pip3 install -r /home/docker/code/app/requirements.txt
  30. # add (the rest of) our code
  31. COPY . /home/docker/code/
  32. RUN git clone https://github.com/lannyMa/django-blog-tutorial-deploy.git tmp && \
  33. mv tmp/* /home/docker/code/app/ && \
  34. rm -rf tmp
  35. # install django, normally you would remove this step because your project would already
  36. # be installed in the code/app/ directory
  37. #RUN django-admin.py startproject website /home/docker/code/app/
  38. EXPOSE 80
  39. CMD ["supervisord", "-n"]
  40. - supervisor配置
  41. [program:app-uwsgi]
  42. command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini
  43. [program:nginx-app]
  44. command = /usr/sbin/nginx
  45. uwsgi.ini配置文件
  46. [base]
  47. chdir = %dapp/
  48. module=blogproject.wsgi:application
  49. chmod-socket=666
  • 修改上面docekrfile的这部分为自己的git项目地址



    即可修改:
  1. Dockerfile
  2. RUN git clone https://github.com/lannyMa/django-blog-tutorial-deploy.git tmp && \
  3. mv tmp/* /home/docker/code/app/ && \
  4. rm -rf tmp
  • 修改项目settings.py为

  1. settings.py
  2. ALLOWED_HOSTS = ['*']
  • 修改nginx-app.conf的(django项目)static路径.

  1. nginx.conf
  2. location /media {
  3. alias /home/docker/persistent/media; # your Django project's media files - amend as required
  4. }
  5. location /static {
  6. alias /home/docker/volatile/static; # your Django project's static files - amend as required
  7. }

等这一切修改完成后,开始构建镜像

  • 构建docker镜像并运行
  1. docker build -t webapp .
  2. docker run -d -p 80:80 webapp
  3. - 运行后访问即可
  4. docker run -d -p 8080:80 webapp

遇到的问题

界面乱码,css未加载成功



解决: 修改nginx配置为自己项目static的地址

  1. location /media {
  2. alias /home/docker/persistent/media; # your Django project's media files - amend as required
  3. }
  4. location /static {
  5. alias /home/docker/volatile/static; # your Django project's static files - amend as required
  6. }

不允许访问,权限拒绝

解决: 修改settings.py

  1. ALLOWED_HOSTS = ['写自己服务器的ip即可']

uwsgi启动失败,扫描不到项目

修改uwsgi.ini

修改为自己项目的地址,

  1. module=blogproject.wsgi:application

这里uwsgi调用的是项目的这个

nginx调用uwsgi.ini



nginx-app.conf

  1. # nginx-app.conf
  2. # the upstream component nginx needs to connect to
  3. upstream django {
  4. server unix:/home/docker/code/app.sock; # for a file socket
  5. # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
  6. }
  7. # configuration of the server
  8. server {
  9. # the port your site will be served on, default_server indicates that this server block
  10. # is the block to use if no blocks match the server_name
  11. listen 80 default_server;
  12. # the domain name it will serve for
  13. server_name .example.com; # substitute your machine's IP address or FQDN
  14. charset utf-8;
  15. # max upload size
  16. client_max_body_size 75M; # adjust to taste
  17. # Django media
  18. location /media {
  19. alias /home/docker/code/app/blog/media; # your Django project's media files - amend as required
  20. }
  21. location /static {
  22. alias /home/docker/code/app/blog/static; # your Django project's static files - amend as required
  23. }
  24. # Finally, send all non-media requests to the Django server.
  25. location / {
  26. uwsgi_pass django;
  27. include /home/docker/code/uwsgi_params; # the uwsgi_params file you installed
  28. }
  29. }

uwsgi.ini

  1. [uwsgi]
  2. # this config will be loaded if nothing specific is specified
  3. # load base config from below
  4. ini = :base
  5. # %d is the dir this configuration file is in
  6. socket = %dapp.sock
  7. master = true
  8. processes = 4
  9. [dev]
  10. ini = :base
  11. # socket (uwsgi) is not the same as http, nor http-socket
  12. socket = :8001
  13. [local]
  14. ini = :base
  15. http = :8000
  16. # set the virtual env to use
  17. home=/Users/you/envs/env
  18. [base]
  19. chdir = %dapp/
  20. module=blogproject.wsgi:application
  21. chmod-socket=666

附: 什么是wsgi,和django/flask有什么关系

图解CGI、FastCGI和PHP-FPM关系图解

  1. the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
  2. gunicorn vs uwsgi是两种不同的appserver
  3. gunicorn vs uwsgi: http://fatelei.github.io/2015/07/05/Gunicorn-vs-uwsgi/
  4. uwsgi在高并发下比gunicorn有更好的吞吐量和更少的错误数

参考

参考

参考

[py]django上线部署-uwsgi+nginx+py3/django1.10的更多相关文章

  1. Django 部署 uwsgi + nginx + supervisor

    Django 部署 uwsgi + nginx + supervisor https://hacpai.com/article/1460607620615?p=1&m=0 zonghua • ...

  2. django 本地项目部署uwsgi 以及云服务器部署 uwsgi+Nginx+Docker+MySQL主从

    一 .django 本地项目部署uwsgi 1 本地部署项目 uwsgi安装测试 通过uwsgi 进行简单部署 安装uwsgi命令:pip install uwsgi -i http://pypi.d ...

  3. django项目在uwsgi+nginx上部署遇到的坑

    本文来自网易云社区 作者:王超 问题背景 django框架提供了一个开发调试使用的WSGIServer, 使用这个服务器可以很方便的开发web应用.但是 正式环境下却不建议使用这个服务器, 其性能.安 ...

  4. django自带wsgi server vs 部署uwsgi+nginx后的性能对比

    一.下面先交代一下测试云主机 cpu: root@alexknight:/tmp/webbench-1.5# cat /proc/cpuinfo |grep model model : model n ...

  5. Django应用部署:nginx+uwsgi方式

    环境准备 nginx+uwsgi方式部署顾名思义,需要nginx和uwsgi两个软件包. nginx不用说,是必备的,关于nginx的安装本文不再赘述,详情可以自行搜索或者参考我以前的文章: Debi ...

  6. (转) 解决django项目部署到nginx+uwsgi服务器后 admin页面样式消失的问题

    原贴地址:https://blog.csdn.net/qq_42571805/article/details/80862455 摘要 uwsgi为主要服务器,nginx为反向代理服务器部署完成之后发现 ...

  7. Django上线部署之uWSGI

    环境: 1.CentOS 7.2 64位 2.SQL Server 2016 Enterprise 64位 3.Python 3.6.5 64位 4.root用户 要求: 按照顺序部署 1.Windo ...

  8. CentOS 傻瓜式部署uWSGI + nginx + flask

    交代背景 这篇帖子是为了提供我自己的July Novel站点的小说数据支撑.解决分布式部署爬虫程序的繁琐过程,由于本人对shell编程并不熟悉,故而先逐步记录操作步骤,通过以下操作达到节省时间的方式. ...

  9. Django上线部署之IIS

    环境: 1.Windows Server 2016 Datacenter 64位 2.SQL Server 2016 Enterprise 64位 3.Python 3.6.0 64位 4.admin ...

随机推荐

  1. JS - 点击事件排除父级标签

    点击事件排除父级标签,这里使用的是stopPropagation()方法.event.stopPropagation(); 对了,这里还用了解除click事件,unbind. 下面这篇博文,介绍挺全的 ...

  2. Matlab当中size() length()等函数讲解

    在Matlab中: size:获取数组的行数和列数 length:数组长度(即行数或列数中的较大值) numel:元素总数. s=size(A): 当只有一个输出参数时,返回一个行向量,该行向量的第一 ...

  3. sencha touch 我的公用类myUtil(废弃 仅参考)

    /*公共类*/ Ext.define('myUtil', { statics: { //store公用加载方法 storeLoadById: function (id) { var store = E ...

  4. 改变presentModalView大小

    rc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; rc.modalPresentationStyle = UIModalP ...

  5. 【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)

    教程目录一 计时器简介二 计时器实现三 Demo下载 一 计时器简介在手机上跑游戏时,可能由于运动物体过多,导致帧频太低,计时不准确.比如一些倒计时的游戏,可能倒计时30s,变成了35s.比如ipho ...

  6. 虚拟机VMware怎么完全卸载干净,如何彻底卸载VMware虚拟机

    亲测好使. 1.禁用VM虚拟机服务 首先,需要停止虚拟机VMware相关服务.按下快捷键WIN+R,打开windows运行对话框,输入[services.msc],点击确定.如下图. 在服务管理中,找 ...

  7. 【OOP】C++ const成员函数

    预备知识 1.代码转换分析技巧 在早期某些编译器会将C++代码翻译为C代码,然后使用C编译器生成可执行文件.其中翻译的一个转化就是:将this指针显式添加到成员函数的第一个参数位置上,并在成员函数调用 ...

  8. Java Agent初探——动态修改代码

    用了一下午总算把java agent给跑通了,本篇文章记录一下具体的操作步骤,以免遗忘... 通过java agent可以动态修改代码(替换.修改类的定义),进行AOP. 目标: ? 1 为所有添加@ ...

  9. vue--双向数据绑定

    <template> <div id="app"> <p>{{msg}}</p> <input v-model="m ...

  10. 04 用户个人信息和二次开发django的文件存储系统

    用户的个人信息的前端页面如下: 业务逻辑分析 从上图中可以看出,需要后端传送的数据有,用户的名字和练习的地址,和最近的浏览记录. 用户的名字和联系的地址可以通过地址表(adress)中获得,地址表可以 ...