在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
安装Nginx 首先拉下centos镜像 我们安装最新的nginx1.19版本:下载地址 将centos镜像运行起来并进入: docker run --name ver -d -p 8051:80 -it nginx_start 将nginx-1.19.0.tar.gz这个包放入容器里面:
安装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缺失的错,可以使用 再 安装成功后,在 要验证是否成功,可以输入 生成镜像 10. 将装有nginx的centos容器打包为镜像 安装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里面有一个 安装UWSGI
运行 配置uWSGI服务器 相关 [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] 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套接字,即通过文件系统 callable:设置在收到请求时,uwsgi加载的模块中哪个变量将被调用,默认是名字为“application”的变量。 wsgi-file:加载指定的wsgi文件。 buffer-size:设置用于uwsgi包解析的内部缓存区大小。默认是4k。 processes和threads,分别是开启的进程数和线程数,而%k是魔数变量,代表CPU核数,如果我们是双核CPU, 安装Supervisor(可选) 直接yum安装会报一个
现在我们将配置supervisor,使得supervisor监听nginx和uwsgi服务。 首先在
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] 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启动:
以上配置弄好后,我们将容器重新打包生成一个新的镜像,记为 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构建python Flask+ nginx+uwsgi容器的文章就介绍到这了,更多相关Docker构建Flask+ nginx+uwsgi内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论