Docker构建python Flask+ nginx+uwsgi容器
作者:Charlie。 发布时间:2023-07-25 06:36:43
安装Nginx
首先拉下centos镜像docker pull centos
我们安装最新的nginx1.19版本:下载地址
将centos镜像运行起来并进入:
docker run --name ver -d -p 8051:80 -it nginx_start
将nginx-1.19.0.tar.gz这个包放入容器里面:
docker cp nginx-1.19.0.tar.gz 10e87af84c05:/root
(10e87af84c05为centos容器id)
安装nginx前先装一些依赖:
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
解压:
tar -zxvf nginx-1.19.0.tar.gz
#进入到nginx-1.10.1 ,并配置nginx
cd nginx-1.19.0
#配置nginx
#--prefix 指定安装的目录
#/usr/local/nginx 是安装目录,不能和自己下载的文件目录重了
#./configure --prefix=/usr/local/nginx
#带ssl stub_status模块 添加strem模块 –with-stream,这样就能传输tcp协议了
#http_stub_status_module 状态监控
#http_ssl_module 配置https
#stream 配置tcp得转发
#http_gzip_static_module 压缩
#http_sub_module 替换请求
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
注:
在这里我出现了pcre和zlib缺失的错,可以使用yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
命令,安装所有依赖。
再 make & make install
进行编译安装
安装成功后,在./configure --prefix=/usr/local/nginx
指定目录会生成四个文件,我们也只需要输入/usr/local/nginx/sbin/nginx
来启动nginx服务即可。
要验证是否成功,可以输入curl localhost
来查看是否启动成功。
生成镜像
10. 将装有nginx的centos容器打包为镜像docker commit ba5ba0d81912 nginx_centos
(ba5ba0d81912 为容器ID,重命名为nginx_centos)
11. 重新运行新的镜像:docker run --name ver -d -p 8051:80 -it nginx_centos
12. 而此时的镜像,则是有我们安装好的nginx,我们就可以拿他开始为所欲为,做一些其他的骚操作了。
安装python2.7环境
yum install gcc openssl-devel bzip2-devel
用 wget 下载 python 2.7 并解压
yum -y install wget
进入目录 /usr/src 再用 wget 下载 python 2.7
cd /usr/src
wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
再解压 python2.7
tar -zxvf Python-2.7.15.tgz
安装 python 2.7
进入上面解压的 Python-2.7.15 解压文件中使用下面命令行安装
cd Python-2.7.15
./configure --enable-optimizations
make altinstall
安装 PIP
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python2.7 get-pip.py
因为版本为2.7,且requirements.txt里面有一个 MYSQL-python
的库,会报一个找不到libmysqlclient-dev
的错,执行yum install mysql-devel
即可解决。
安装UWSGI
pip install uwsgi
的时候会报一个错:
plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
#include <Python.h>
运行yum install python-devel.x86_64
即可解决,并重新pip install即可下载。
配置uWSGI服务器
相关uwsgi.ini
文件内容如下:
[uwsgi]
socket = /tmp/uwsgi.sock
chown-socket = nginx:nginx
chmod-socket = 664
# Graceful shutdown on SIGTERM, see https://github.com/unbit/uwsgi/issues/849#issuecomment-118869386
hook-master-start = unix_signal:15 gracefully_kill_them_all
在项目目录下/app/创建uwsgi.ini
文件:
[uwsgi]
uwsgi-socket = /tmp/uwsgi.sock
chmod-socket = 777
callable = app
wsgi-file = main.py
buffer-size = 65535
processes = %(%k * 2)
threads = %(%k * 20
其中每个参数的意思:
uwsgi-socket:将uwsgi-socket这个配置项指定了一个文件,这个文件是Unix套接字,即通过文件系统
(而非网络地址)进行寻址和访问的套接字。配置uwsgi-socket之后,还需要配置chmod-socket,
Unix socket是个文件,所以会受到Unix系统的权限限制,可以配置成660或者777,
使得uwsgi客户端能够访问这个Unix socket文件,这里配置为777。
callable:设置在收到请求时,uwsgi加载的模块中哪个变量将被调用,默认是名字为“application”的变量。
wsgi-file:加载指定的wsgi文件。
buffer-size:设置用于uwsgi包解析的内部缓存区大小。默认是4k。
processes和threads,分别是开启的进程数和线程数,而%k是魔数变量,代表CPU核数,如果我们是双核CPU,
那这里的processes和threads分别为4和40,即有4个进程,每个进程有40个线程。
安装Supervisor(可选)
直接yum安装会报一个No package supervisor available.
的错误,那是因为CentOS是RedHat企业版编译过来的,去掉了所有关于版权问题的东西。只需要执行yum install epel-release
即可解决。安装好后会生成如下目录:
现在我们将配置supervisor,使得supervisor监听nginx和uwsgi服务。
首先在/etc
目录下创建supervisor
文件,然后创建supervisord.conf
文件和conf.d目录:
supervisord.conf目录配置如下:
; supervisor config file
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
再在conf.d目录下创建supervisord.conf
文件并编辑:
[supervisord]
nodaemon=true
[program:uwsgi]
command=/usr/bin/uwsgi --ini /etc/uwsgi/uwsgi.ini --die-on-term --need-app
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=/usr/local/nginx/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
# Graceful stop, see http://nginx.org/en/docs/control.html
stopsignal=QUIT
以上路径均为实际目录配置,如果有不一样则需要更改。
然后将supervisor启动:
以上配置弄好后,我们将容器重新打包生成一个新的镜像,记为base_v3
,我们写一个打包docker应用的Dockerfile:
FROM base_v3
# 创建工作路径
RUN mkdir /app
# 指定容器启动时执行的命令都在app目录下执行
WORKDIR /app
# 替换nginx的配置
COPY nginx.conf /etc/nginx/nginx.conf
# 将本地app目录下的内容拷贝到容器的app目录下
COPY ./app/ /app/
这里,在Dockerfile和app同级目录下,再建立一个nginx.conf文件,并将nginx.conf内容修改如下:
user nginx;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log warn;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 20480;
events {
use epoll;
worker_connections 20480;
multi_accept on;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#请求量级大建议关闭acccess_log
#access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 300s;
client_header_timeout 300s;
client_body_timeout 300s;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types text/html application/javascript application/json;
include /usr/local/nginx/conf.d/*.conf;
server {
listen 6666;
charset utf-8;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
uwsgi_send_timeout 300;
uwsgi_connect_timeout 300;
uwsgi_read_timeout 300;
}
}
}
接下来只需要docker build -t new_project .
并docker run --name test -d -p 8055:6666 -v /root/web/mim_backend/data:/app/static -v /root/logs/mim_backend:/app/log -it new_project
即可。
当然,这个时候进去nginx和uwsgi没有自动启动,需要手动拉起来,如想自动拉起服务,可选用supervisor或者在dockerfile里面加一个ENTRYPOINT nginx -g "daemon on;" && uwsgi --ini /app/uwsgi.ini
然后随便跑一个接口测试:
来源:https://blog.csdn.net/caoyu1221/article/details/106826338
猜你喜欢
- 复制代码CREATE FUNCTION fGetStrBySplit ( @Source VARCHAR(max), @Index INT,
- 本文从算法入手同时通过实例来讲解如何在SQL Server数据库中实现最优最简的整个过程。算法是计算机科学中一个重要的研究方向,是解决复杂问
- js关于 byval 与 byref 二者区别: byval 传递数值,实参和形参分处不同的内存单元,互不干扰! byref 传递地址,实参
- 减少HTTP请求,是可以提高网站速度的,把所有的背景图像都放到一个图片文件中,然后通过CSS的background-image和backgr
- 前言使用np.savetxt()方法可以将数据保存为txt文件或者是csv文件。1 np.savetxt()存储txt文件1-1 基础参数n
- WebDriver简介selenium从2.0开始集成了webdriver的API,提供了更简单,更简洁的编程接口。selenium web
- 定义行为要定义行为,通过继承 yii\base\Behavior 或其子类来建立一个类。如:namespace app\components
- 说到排序,我想起一个故事,大意是说唐僧师徒西游美利坚,孙悟空买了本词典,开始逐条背诵单词。他们第一次下美国馆子的时候,不管服务员推荐什么,孙
- 目前绝大多数手机都支持WAP 2.0。WAP 2.0的页面设计具有更好的视觉效果,更接近网页。不过由于手机千差万别,手机浏览器的能力也各不相
- 当然,如果你的网站文章中有图片,那么请记得一定要打上自己的LOGO,而且这个LOGO不要固定在这些图片的某个角落里,一定要随机出现在图片的任
- 有朋友问,在数据库中如何查询数据所在的行,一般我们建议一个自增字段就可以了.但是有时却会删除数据,那么那个自增字段也不正确了先不管朋友们为什
- 往往我们在建立一个较为大型的网站的时候会有很多的副页面框架模式甚至一些小的细节元素都是相同的,但是令人困扰的是在更新它们时却要费些周折,要一
- MaxDB是MySQL AB公司通过SAP认证的数据库。MaxDB数据库服务器补充了MySQL AB产品系列。某些MaxDB特性在MySQL
- 阅读上一章:Css布局Chapter 13 为文字指定样式我想以一章的篇幅来讨论用CSS设定文字样式的做法是个好点子.一般处理文字内容大概是
- Oracle的系统要求企业版:CPU最低PENTIUM200M推荐PENTIUMIII1G以上 内存最低128M推荐512M 硬盘空间系统盘
- vbscript中,错误处理使用on error resume next来完成,如果在你的代码里加入这一句,在这句之后的其他代码如果出现错误
- 希腊Web 设计师Christos Chiotis 发表在 CssGlobe 的一篇文章,讲述了黄金分割率在 CSS 中的应用。黄金分割率是
- 关于浏览器的最离奇的统计结果之一就是Internet Explorer 版本6,7和8共存。截至本文,Internet Explorer各个
- 研究好多天了,也试过好多办法了,总结出目前发现最好的方法:先说一下基本的东西:<%@ codepage=65001%>
- 前段时间和哥们胡侃的时候还说腾讯抄的有水平,不过这次我溴了,腾讯房产被新浪乐居给幽默了一把。乐居:http://bj.house.sina.