ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

docker实现nginx代理uWSGI启动Python

2021-10-27 16:03:18  阅读:162  来源: 互联网

标签:code Python Django nginx html mydjango docker uwsgi


通过docker实现nginx代理uWSGI启动Python

安装Django,启动一个Django项目

安装Django

书写Dockerfile打包镜像

FROM python:3.7
RUN mkdir /code
COPY requirements.txt /code/
WORKDIR /code
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

requirements.txt文件内容

#指定容器内安装的Django版本
Django==2.2.6
#容器内安装uwsgi
uwsgi

执行镜像打包命令

docker build -t Django:1.0 .

通过Django:1…0启动容器

version: '3'
services:
        django:
                image: django:1.0
                container_name: django
                restart: always
                ports:
                        - 8200:8200
                        - 8222:8888
                command: ping 127.0.0.1 #是容器一直存在不退出
                volumes:
                        #在容器内部生成mydjango应用时所生成的文件中的配置文件
                        - ./settings.py:/code/mydjango/mydjango/settings.py
                        #uwsgi的配置文件,与Django应用的manage.py处于同级目录 启动uwsgi时用
                        - ./uwsgi.ini:/code/mydjango/uwsgi.ini

进入容器内,生成一个Django应用,将Django项目里的settings.py挂载出来

#进入Django容器
docker exec -it django /bin/bash
#生成Django应用
django-admin startproject mydjango
#进入项目文件夹
cd /code/mydjango
#启动应用
python manage.py runserver 0.0.0.0:8200

浏览器访问:http://ip:8200查看是否访问正常

将uwsgi.ini挂载出来,配置uwsgi.ini文件

[uwsgi]
#服务端口此处端口写容器内端口
http = 0.0.0.0:8888  
 
#指定与Nginx通信的方式,不影响uwsgi本身运行。如果配置了需要到nginx中进行相关配置-才能通过nginx访问Django
# socket = 127.0.0.1:8001
 
# 启动一个master进程,来管理其余的子进程
master = True
processes = 4
threads = 2
 
#python虚拟环境目录绝对路径。如果有的话,home是虚拟环境根目录,PYTHNONHOME是虚拟环境下的bin目录(放置了Python执行文件)
#home = /env
#PYTHONHOME = /env/bin
 
#django项目目录,与manager.py同级
chdir = /code/mydjango
 
#主应用中的wsgi,下面这种配法是在Django根目录下运行uwsgi有效,主APP名为有settings.py的那个目录名。如果是其他目录运行,下面建议写成绝对路径。
wsgi-file = /code/mydjango/mydjango/wsgi.py
 
#服务停止时自动移除unix Socket和pid文件
vacuum = true
 
#设置每个工作进程处理请求的上限,达到上限时,将回收(重启)进程,可以预防内存泄漏
max-requests=5000
 
#设置后台运行保存日志。只要配置了daemonize就会让uwsgi后台运行,同时将日志输出到指定目录
daemonize=/code/mydjango/uwsgi.log
 
#保存主进程的pid,用来控制uwsgi服务
pidfile=/code/mydjango/uwsgi.pid
#uwsgi --stop/reload xxx.pid 停止/重启uwsgi
 
#静态文件映射
#static-map = /static=Django下static目录的绝对路径

进入容器内,启动uwsgi

#进入容器内
docker exec -it django /bin/bash
#进入项目文件夹
cd /code/mydjango
#启动uwsgi
uwsgi --ini uwsgi.ini

浏览器访问:http://ip:8222访问正常即成功

安装nginx

version: '3'
services:
        nginx-uwsgi:
                image: nginx
                container_name: nginx-uwsgi
                restart: always
                ports:
                        - 8333:80
                volumes:
                        - ./nginx:/etc/nginx
                        - ./html:/usr/share/nginx/html
                        #将nginx文件夹整个挂载出来
                        - ./logs:/var/log/nginx
                        - /etc/localtime:/etc/localtime:ro

启动nginx容器,将nginx文件夹挂载出来

docker-compose up -d 

进入nginx文件夹,修改nginx/con.d/default.conf文件

#配置负载均衡
upstream uwsgi {
#配置主机名和端口
      server ip:8222;
  }
server {
#配置监听
    listen       80;
    listen  [::]:8333;
    #配置服务名
    server_name  ;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    #引入配置文件
       uwsgi_params;
       #规定访问协议
       uwsgi_pass uwsgi;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
   # location /static {
    #   alias /code/mydjango/;

    #}
    # location = /50x.html {
    #    root   /usr/share/nginx/html;
   # }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


浏览器访问:http://ip:端口,访问成功即完成

坑窝

uwsgi.ini文件明确访问协议http、socket

uwsgi.ini文件中明确wsgi-file地址编写准确,否则报错无法启动uwsgi

nginx与浏览器访问明确访问端口,编写uwsgi.ini时使用容器内端口,访问采用映射端口

uwsgi.ini文件如下

[uwsgi]
#服务端口
#http = 0.0.0.0:8888
 
#指定与Nginx通信的方式,不影响uwsgi本身运行。如果配置了需要到nginx中进行相关配置-才能通过nginx访问Django
socket = 0.0.0.0:8888
 
# 启动一个master进程,来管理其余的子进程
master = True
processes = 4
threads = 2
 
#python虚拟环境目录绝对路径。如果有的话,home是虚拟环境根目录,PYTHNONHOME是虚拟环境下的bin目录(放置了Python执行文件)
#home = /env
#PYTHONHOME = /env/bin
 
#django项目目录,与manager.py同级
chdir = /code/mydjango
 
#主应用中的wsgi,下面这种配法是在Django根目录下运行uwsgi有效,主APP名为有settings.py的那个目录名。如果是其他目录运行,下面建议写成绝对路径。
wsgi-file = /code/mydjango/mydjango/wsgi.py
 
#服务停止时自动移除unix Socket和pid文件
vacuum = true
 
#设置每个工作进程处理请求的上限,达到上限时,将回收(重启)进程,可以预防内存泄漏
max-requests=5000
 
#设置后台运行保存日志。只要配置了daemonize就会让uwsgi后台运行,同时将日志输出到指定目录
daemonize=/code/mydjango/uwsgi.log
 
#保存主进程的pid,用来控制uwsgi服务
pidfile=/code/mydjango/uwsgi.pid
#uwsgi --stop/reload xxx.pid 停止/重启uwsgi
 
#静态文件映射
#static-map = /static=Django下static目录的绝对路径
buffer-size = 65536

nginx文件

引入配置文件(uwsgi_params),规定访问协议为(uwsgi_pass)

defualt.conf文件如下

upstream uwsgi {
      server ip:8222;
  }
server {
    listen       80;
    listen  [::]:8333;
    server_name  ip;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
       include  uwsgi_params;
       uwsgi_pass uwsgi;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
   # location /static {
    #   alias /code/mydjango/;

    #}
    # location = /50x.html {
    #    root   /usr/share/nginx/html;
   # }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


标签:code,Python,Django,nginx,html,mydjango,docker,uwsgi
来源: https://blog.csdn.net/yj970605/article/details/120994658

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有