[py]django上线部署-uwsgi+nginx+py3/django1.10

时间:2022-06-05 18:44:00

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

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

- 克隆代码进入项目
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

[py]django上线部署-uwsgi+nginx+py3/django1.10

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

生产部署-nginx+uwsgi+django10

部署详情说明

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

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

探究下怎么制作docker镜像

  • 先搞清楚nginx+uwsgi+django物理机上是怎么配合工作的,确保手动一步步启动完成没问题,然后再做镜像
- 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镜像.

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

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

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

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

[py]django上线部署-uwsgi+nginx+py3/django1.10

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

- 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
  • 修改上面docekrfile的这部分为自己的git项目地址

    [py]django上线部署-uwsgi+nginx+py3/django1.10

    即可修改:
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为

    [py]django上线部署-uwsgi+nginx+py3/django1.10
settings.py

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

    [py]django上线部署-uwsgi+nginx+py3/django1.10
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
}

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

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

遇到的问题

界面乱码,css未加载成功

[py]django上线部署-uwsgi+nginx+py3/django1.10

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

    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.py

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

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

修改uwsgi.ini

[py]django上线部署-uwsgi+nginx+py3/django1.10

修改为自己项目的地址,

module=blogproject.wsgi:application

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

[py]django上线部署-uwsgi+nginx+py3/django1.10

nginx调用uwsgi.ini

[py]django上线部署-uwsgi+nginx+py3/django1.10

[py]django上线部署-uwsgi+nginx+py3/django1.10

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

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

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

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有更好的吞吐量和更少的错误数

参考

[py]django上线部署-uwsgi+nginx+py3/django1.10

参考

[py]django上线部署-uwsgi+nginx+py3/django1.10

参考

[py]django上线部署-uwsgi+nginx+py3/django1.10

[py]django上线部署-uwsgi+nginx+py3/django1.10

[py]django上线部署-uwsgi+nginx+py3/django1.10

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

  1. Django 部署 uwsgi &plus; nginx &plus; supervisor

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

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

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

  3. django项目在uwsgi&plus;nginx上部署遇到的坑

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

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

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

  5. Django应用部署:nginx&plus;uwsgi方式

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

  6. &lpar;转&rpar; 解决django项目部署到nginx&plus;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 &plus; nginx &plus; 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. HttpClient session

    session概述 session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session ...

  2. Wilson定理证明

    Wilson定理证明 就是那个\((p-1)! \equiv -1 \pmod{p}\),\(p\)是一个素数. Lemma A \(\mathbb{Z}_p\)可以去掉一个零元变成一个群. 即\(\ ...

  3. 关于JQ的&dollar;&period;deferred函数。参考网络文档

    由于jQuery版本问题对Deferred对象的实现有所不同,具体请参照jQuery api:   jQuery.Deferred()基于Promises/A规范实现,因为jQuery本身的设计风格, ...

  4. postgresql编译安装与调试(二)

    接前文postgresql编译安装与调试(一),继续说说postgresql的编译安装与调试. 上一篇已经详细说明了如何在Linux系统上编译安装postgresql,这次我们在此基础上简单讲讲如何在 ...

  5. python—文件创建

    # 1.文件操作# day1.txt# 1.文件路径:E:\day1.txt# 2.编码方式:utf-8.gbk# 3.操作方式:只读,只写,追加,读写,写读# 以什么编码方式储存的方式储存就以什么编 ...

  6. Immutable 学习

    1.什么是Immutable Data? Immutable Data 就是一旦创建,就不能再被更改的数据.对 Immutable 对象的任何修改或添加删除操作都会返回一个新的 Immutable 对 ...

  7. Java 图形化界面设计(GUI)实战练习(代码)

    关于Java图形化界面设计,基础知识网上可搜,下面简单介绍一下重点概念,然后就由浅入深代码实例. 程序是为了方便用户使用的,Java引入图形化界面编程. 1.JFrame 是容器类 2.AWT 是抽象 ...

  8. 使用SHOW binlog events查看binlog内容

    用mysqlbinlog命令行查看binlog,觉得比较麻烦,突然发现原来mysql有个命令可以直接查看. SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] ...

  9. app后端设计-- 数据库分表

    当项目上线后,随着用户的增长,有些数据表的规模会以几何级增长,当数据达到一定规模的时候(例如100万条),查询,读取性能就下降得很厉害,这时,我们就要考虑分表. 更新表数据时会导致索引更新,当单表数据 ...

  10. Python3 字典Dict(十三)

    Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 字典是另一种可变容器模型,且可存储任意类 ...