亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Docker構建python Flask+ nginx+uwsgi容器

瀏覽:3日期:2022-07-28 16:32:35

安裝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 makeyum -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_centos12. 而此時的鏡像,則是有我們安裝好的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/srcwget 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-optimizationsmake 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>

Docker構建python Flask+ nginx+uwsgi容器

運行yum install python-devel.x86_64即可解決,并重新pip install即可下載。

配置uWSGI服務器

相關uwsgi.ini文件內容如下:

[uwsgi]socket = /tmp/uwsgi.sockchown-socket = nginx:nginxchmod-socket = 664# Graceful shutdown on SIGTERM, see https://github.com/unbit/uwsgi/issues/849#issuecomment-118869386hook-master-start = unix_signal:15 gracefully_kill_them_all

在項目目錄下/app/創建uwsgi.ini文件:

[uwsgi]uwsgi-socket = /tmp/uwsgi.sockchmod-socket = 777callable = appwsgi-file = main.pybuffer-size = 65535processes = %(%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即可解決。安裝好后會生成如下目錄:

Docker構建python Flask+ nginx+uwsgi容器

現在我們將配置supervisor,使得supervisor監聽nginx和uwsgi服務。

首先在/etc目錄下創建supervisor文件,然后創建supervisord.conf文件和conf.d目錄:

Docker構建python Flask+ 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.conf文件并編輯:

[supervisord]nodaemon=true[program:uwsgi]command=/usr/bin/uwsgi --ini /etc/uwsgi/uwsgi.ini --die-on-term --need-appstdout_logfile=/dev/stdoutstdout_logfile_maxbytes=0stderr_logfile=/dev/stderrstderr_logfile_maxbytes=0[program:nginx]command=/usr/local/nginx/sbin/nginxstdout_logfile=/dev/stdoutstdout_logfile_maxbytes=0stderr_logfile=/dev/stderrstderr_logfile_maxbytes=0# Graceful stop, see http://nginx.org/en/docs/control.htmlstopsignal=QUIT

以上路徑均為實際目錄配置,如果有不一樣則需要更改。

然后將supervisor啟動:

Docker構建python Flask+ nginx+uwsgi容器

以上配置弄好后,我們將容器重新打包生成一個新的鏡像,記為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

然后隨便跑一個接口測試:

Docker構建python Flask+ nginx+uwsgi容器

到此這篇關于Docker構建python Flask+ nginx+uwsgi容器的文章就介紹到這了,更多相關Docker構建Flask+ nginx+uwsgi內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 综合精品在线 | 一级aa免费视频毛片 | 尤物视频在线看 | 欧美日韩专区国产精品 | 黄色片在线视频 | 日韩毛片欧美一级国产毛片 | 午夜激情福利视频 | 亚洲精品主播一区二区三区 | 日本一级特黄aa毛片免费观看 | 91精品在线看 | 欧美一区二区在线观看免费网站 | 中国一级特黄剌激爽毛片 | 日韩不卡一级毛片免费 | 精品免费久久久久久成人影院 | 午夜在线观看视频免费 成人 | 末成年一级在线看片 | 成人网免费观看 | 欧美日本一道高清二区三区 | 日本中出视频 | 中文在线观看视频 | 丁香亚洲综合五月天婷婷 | 色99在线 | 玛雅视频网站在线观看免费 | 九九视频免费精品视频免费 | 色婷婷在线播放 | 一级做a爰片性色毛片小说 一级做a爰片性色毛片中国 | 国产在视频线精品视频二代 | 一本本久综合久久爱 | 青青热久免费精品视频精品 | 最新黄色网址在线观看 | 毛片最新网址 | 国产二区三区 | 国产黄色在线播放 | 欧美成人特黄级毛片 | 91免费永久国产在线观看 | 91av爱爱| 亚洲不卡在线视频 | 精品久久洲久久久久护士 | 免费在线观看中日高清生活片 | 亚洲国产国产综合一区首页 | 国产日产高清欧美一区二区三区 |