yywx001 发表于 2018-8-9 08:28:35

mysql + Python3.5.2 + Django + Uwsgi + Nginx实现生产环境

  官方文档:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
  前面已经安装好mysql数据库,Python3.5.2,pip,django1.8.5,nginx(前面安装脚本需要删除:--without-http_uwsgi_module)
  一
  安装uwsgi:
  pip install uwsgi
  uwsgi --version
  2.0.15
  测试uwsgi:
  cat test.py
  # test.py
  def application(env, start_response):
  start_response('200 OK', [('Content-Type','text/html')])
  return # python3
  #return ["Hello World"] # python2
  运行:
  uwsgi --http :8000 --wsgi-file test.py
  浏览器访问服务器IP:8000,返回Hello World,测试正常。
  用uwsgi 启动django测试:
  uwsgi --http :8001 --chdir /root/blogproject/ --module blogproject.wsgi
  浏览器访问服务器IP:8001,返回网页内容,测试正常。
  把参数写到配置文件里:
  
  # 项目目录
  chdir=/root/blogproject/
  # 指定项目的application
  module=blogproject.wsgi
  # 进程个数
  workers=5
  pidfile=/root/blogproject/uwsgi.pid
  # 指定IP端口
  http= :8080
  # 指定静态文件
  static-map=/static=/root/blogproject/static
  # 启动uwsgi的用户名和用户组
  # uid=root
  # gid=root
  # 启用主进程
  master=true
  # 自动移除unix Socket和pid文件当服务停止的时候
  vacuum=true
  # 序列化接受的内容,如果可能的话
  thunder-lock=true
  # 启用线程
  enable-threads=true
  # 设置自中断时间
  harakiri=30
  # 设置缓冲
  post-buffering=4096
  # 设置日志目录
  daemonize=/root/blogproject/script/uwsgi.log
  # 指定sock的文件路径
  socket=/root/blogproject/script/uwsgi.sock
  #socket = 127.0.0.1:8001
  #设置sock文件权限
  #chmod-socket = 664
  启动和关闭进程
  uwsgi --ini uwsgi.ini   # 启动uwsgi配置
   getting INI configuration from uwsgi.ini
   added mapping for /static => /root/blogproject/static# 启动成功,可以执行ps -ef |grep uwsgi 查看相关进程
  uwsgi --stop uwsgi.pid# 关闭uwsgi
  uwsgi --reload uwsgi.pid#重新加载配置
  二
  Nginx配置:
  upstream django {
  #      server 127.0.0.1:8001;
  server unix:///root/blogproject/script/uwsgi.sock;
  }
  server {
  listen 80;
  server_name www.victory168.top victory168.top;
  access_log/var/log/nginx/access.log;# Nginx log file
  error_log/var/log/nginx/error.log;    # Nginx error log file
  charsetutf-8; # Nginx coding
  gzip on;# gzip
  gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;# gzip type
  error_page404         /404.html;#404 file
  error_page   500 502 503 504/50x.html;# 50x file
  # set uwsgi path
  location / {
  include uwsgi_params;# import Nginx module with uWSGIcommunication
  # set uwsgi sock path file
  #            uwsgi_pass unix:/root/blogproject/script/uwsgi.sock;
  uwsgi_pass django;
  }
  # set static path
  location /static/ {
  alias/root/blogproject/static/;
  #            indexindex.html index.htm;
  }
  }
  nginx -t# 检查nginx语法问题
  三
  django静态文件收集
  1.setting.py设置
  DEBUG = False
  STATIC_ROOT = os.path.join(BASE_DIR, 'statics')
  2. 执行collectstatic命令:
  python manage.py collectstatic
  3.启动Nginx和uwsgi程序的用户应一致
  遇到问题查看Nginx error.log和uwsgi.log,
  4.导出本地开发环境安装的依赖模块:
  pip freeze > requirements.txt
  5.检查服务器端安装的依赖模块:
  # pip list
  Django (2.0)
  Markdown (2.6.11)
  pip (9.0.1)
  Pygments (2.2.0)
  PyMySQL (0.8.0)
  pytz (2017.3)
  setuptools (20.10.1)
  uWSGI (2.0.15)
页: [1]
查看完整版本: mysql + Python3.5.2 + Django + Uwsgi + Nginx实现生产环境