Python3+Django+MySQL+uWSGI+Nginx

系统 1348 0

项目目录结构:

按照下图所示创建 build 文件夹及内容

Docker部署Python3+Django+MySQL+uWSGI+Nginx_第1张图片

Dockerfile:

            
              FROM ubuntu:16.04
FROM python:3.6


ENV http_proxy=http://172.16.6.67:3128
ENV https_proxy=http://172.16.6.67:3128

RUN apt-get -y update && \
    apt-get -y upgrade && \
    apt-get install -y \
    vim \
	git \
	python3-dev \
	python3-setuptools \
	python3-pip \
	nginx \
	supervisor \
        nodejs \
        npm \
        default-libmysqlclient-dev && \
        pip3 install --upgrade -i https://pypi.doubanio.com/simple/ pip setuptools && \
  	rm -rf /var/lib/apt/lists/*

RUN pip3 install -i https://pypi.doubanio.com/simple/ uwsgi

COPY requirements.txt /data/Project/
RUN pip3 install -i https://pypi.doubanio.com/simple/ -r /data/Project/requirements.txt

COPY ./build/nginx/nginx.conf /etc/nginx/nginx.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY ./build/nginx/conf.d  /etc/nginx/conf.d

ENV PYTHONIOENCODING=utf-8

EXPOSE 9990 9999
            
          

dev.env:

            
              ENV=DEV
DEBUG=TRUE
DB_HOST=project.db
MYSQL_DATABASE=project
# So you don't have to use root, but you can if you like
MYSQL_USER=project
# You can use whatever password you like
MYSQL_PASSWORD=123456
# Password for root access
MYSQL_ROOT_PASSWORD=JhYajBJ634XUQvNm9W6e
            
          

default.conf

            
              upstream django {
    server 127.0.0.1:9000;
    #server unix:///data/Project/app.sock;
}

server {
    listen      9990 default_server;

    server_name 172.16.3.7;
    charset     utf-8;

    client_max_body_size 75M;

    location /media  {
	    alias /data/Project/media;
    }

    location /static {
        alias /data/Project/static;
    }

    location / {
        uwsgi_pass  django;
        include     /data/Project/uwsgi_params;
    }
}
            
          

nginx.conf

            
              user root;
worker_processes 1;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /data/Project/logs/nginx/access.log;
	error_log /data/Project/logs/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
#
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}
            
          

docker-compose.yml

            
              version: "2"

services:

  mysql.db:
    container_name: "mysql.db"
    image: "mysql:5.7.19"
    restart: always
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --init-connect='SET NAMES utf8mb4;' --innodb-flush-log-at-trx-commit=0
    expose:
      - "3306"
    volumes:
      - ./build/data/mysql:/var/lib/mysql:rw
    environment:
      - MYSQL_DATABASE=project
      - MYSQL_ROOT_PASSWORD=123456

  web:
    container_name: "web"
    image: "project:base"
    #restart: alaways
    volumes:
      - "./:/data/Project"
#      - "./build/nginx/conf.d:/etc/nginx/conf.d"
#      - "./build/nginx/nginx.conf:/etc/nginx/nginx.conf"
    expose:
      - "9000"
    ports:
      - "9990:9990"
#      - "9000:9000"
#    command: ["/usr/local/bin/uwsgi", "--ini", "/data/Project/uwsgi.ini"]
    command: ["tail", "-f", "/dev/null"]
    env_file: ./build/env/dev.env
    logging:
      driver: "json-file"
      options:
        max-size: "1k"
        max-file: "3"

            
          

修改setting文件里面的数据库配置:

            
              ENV = os.environ.get("ENV")
DB_HOST = os.environ.get("DB_HOST")
DB_PORT = os.environ.get("DB_PORT")
MYSQL_DATABASE = os.environ.get("MYSQL_DATABASE")
# So you don't have to use root, but you can if you like
MYSQL_USER = os.environ.get("MYSQL_USER")
# You can use whatever password you like
MYSQL_PASSWORD = os.environ.get("MYSQL_PASSWORD")

DEV_DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": MYSQL_DATABASE,
        "HOST": DB_HOST,
        "USER": MYSQL_USER,
        "PASSWORD": MYSQL_PASSWORD,
        # "PORT": DB_PORT,
        "OPTIONS": {"init_command": "SET default_storage_engine=INNODB;"},
    }
}

if ENV == "DEV":
    DATABASES = DEV_DATABASES
            
          

Makefile(直接复制过去可能有语法错误)

            
              NAME=project

BUILD_ARG=--build-arg http_proxy=http://172.16.6.67:3128 --build-arg https_proxy=http://172.16.6.67:3128

base:
    echo build ${NAME}:base
    cp build/docker/base/Dockerfile .
    docker build -t ${NAME}:base . ${BUILD_ARG}
    rm Dockerfile
    # docker push ${NAME}:base

dev:
    cp build/docker-compose.yml .
    docker-compose up -d
    rm docker-compose.yml

down:
    cp build/docker-compose.yml .
    docker-compose down
    rm docker-compose.yml

update:
    cp build/docker-compose.yml .
    docker-compose restart
    rm docker-compose.yml

            
          

uwsgi.ini

            
              [uwsgi]
ini = :base

socket = :9000
master = true
processes = 4
enable-threads = true

[base]
chdir = /data/Project
spooler = /data/Project/uwsgi-tasks
daemonize = /data/Project/uwsgi.log
;logto = /data/Project/uwsgi.log
wsgi-file=/data/Project/Project/wsgi.py
chmod-socket=666
            
          

uwsgi_params

            
              uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;
            
          

全部文件创建完之后执行以下命令:

            
              make base
make dev
            
          

然后输入:docker ps查看容器是否启动

容器启动后输入:docker exec -it web bash 进入容器

启动nginx

执行uwsgi uwsgi.ini

ok


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论