欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Docker構(gòu)建python Flask+ nginx+uwsgi容器

 更新時(shí)間:2020年09月06日 09:57:27   作者:Charlie。  
這篇文章主要介紹了Docker構(gòu)建python Flask+ nginx+uwsgi容器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

安裝Nginx

首先拉下centos鏡像docker pull centos

我們安裝最新的nginx1.19版本:下載地址

將centos鏡像運(yùn)行起來(lái)并進(jìn)入:

docker run --name ver -d -p 8051:80 -it nginx_start

將nginx-1.19.0.tar.gz這個(gè)包放入容器里面:

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
#進(jìn)入到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協(xié)議了
 #http_stub_status_module 狀態(tài)監(jiān)控
 #http_ssl_module  配置https
 #stream 配置tcp得轉(zhuǎn)發(fā)
 #http_gzip_static_module 壓縮
 #http_sub_module 替換請(qǐng)求
 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream

注:

在這里我出現(xiàn)了pcre和zlib缺失的錯(cuò),可以使用yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel命令,安裝所有依賴。

make & make install進(jìn)行編譯安裝

安裝成功后,在./configure --prefix=/usr/local/nginx指定目錄會(huì)生成四個(gè)文件,我們也只需要輸入/usr/local/nginx/sbin/nginx來(lái)啟動(dòng)nginx服務(wù)即可。

要驗(yàn)證是否成功,可以輸入curl localhost來(lái)查看是否啟動(dòng)成功。

生成鏡像

10. 將裝有nginx的centos容器打包為鏡像docker commit ba5ba0d81912 nginx_centos(ba5ba0d81912 為容器ID,重命名為nginx_centos)
11. 重新運(yùn)行新的鏡像:docker run --name ver -d -p 8051:80 -it nginx_centos
12. 而此時(shí)的鏡像,則是有我們安裝好的nginx,我們就可以拿他開始為所欲為,做一些其他的騷操作了。

安裝python2.7環(huán)境

yum install gcc openssl-devel bzip2-devel

用 wget 下載 python 2.7 并解壓

yum -y install wget 

進(jìn)入目錄 /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

進(jìn)入上面解壓的 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

因?yàn)榘姹緸?.7,且requirements.txt里面有一個(gè) MYSQL-python的庫(kù),會(huì)報(bào)一個(gè)找不到libmysqlclient-dev的錯(cuò),執(zhí)行yum install mysql-devel即可解決。

安裝UWSGI

pip install uwsgi的時(shí)候會(huì)報(bào)一個(gè)錯(cuò):

plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
#include <Python.h>


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

配置uWSGI服務(wù)器

相關(guān)uwsgi.ini文件內(nèi)容如下:

[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

在項(xiàng)目目錄下/app/創(chuàng)建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

其中每個(gè)參數(shù)的意思:

uwsgi-socket:將uwsgi-socket這個(gè)配置項(xiàng)指定了一個(gè)文件,這個(gè)文件是Unix套接字,即通過文件系統(tǒng)
(而非網(wǎng)絡(luò)地址)進(jìn)行尋址和訪問的套接字。配置uwsgi-socket之后,還需要配置chmod-socket,
Unix socket是個(gè)文件,所以會(huì)受到Unix系統(tǒng)的權(quán)限限制,可以配置成660或者777,
使得uwsgi客戶端能夠訪問這個(gè)Unix socket文件,這里配置為777。

callable:設(shè)置在收到請(qǐng)求時(shí),uwsgi加載的模塊中哪個(gè)變量將被調(diào)用,默認(rèn)是名字為“application”的變量。

wsgi-file:加載指定的wsgi文件。

buffer-size:設(shè)置用于uwsgi包解析的內(nèi)部緩存區(qū)大小。默認(rèn)是4k。

processes和threads,分別是開啟的進(jìn)程數(shù)和線程數(shù),而%k是魔數(shù)變量,代表CPU核數(shù),如果我們是雙核CPU,
那這里的processes和threads分別為4和40,即有4個(gè)進(jìn)程,每個(gè)進(jìn)程有40個(gè)線程。

安裝Supervisor(可選)

直接yum安裝會(huì)報(bào)一個(gè)No package supervisor available.的錯(cuò)誤,那是因?yàn)镃entOS是RedHat企業(yè)版編譯過來(lái)的,去掉了所有關(guān)于版權(quán)問題的東西。只需要執(zhí)行yum install epel-release即可解決。安裝好后會(huì)生成如下目錄:


現(xiàn)在我們將配置supervisor,使得supervisor監(jiān)聽nginx和uwsgi服務(wù)。

首先在/etc目錄下創(chuàng)建supervisor文件,然后創(chuàng)建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目錄下創(chuàng)建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

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

然后將supervisor啟動(dòng):


以上配置弄好后,我們將容器重新打包生成一個(gè)新的鏡像,記為base_v3,我們寫一個(gè)打包docker應(yīng)用的Dockerfile:

FROM base_v3
 
# 創(chuàng)建工作路徑
RUN mkdir /app
 
# 指定容器啟動(dòng)時(shí)執(zhí)行的命令都在app目錄下執(zhí)行
WORKDIR /app
 
# 替換nginx的配置
COPY nginx.conf /etc/nginx/nginx.conf
 
# 將本地app目錄下的內(nèi)容拷貝到容器的app目錄下
COPY ./app/ /app/

這里,在Dockerfile和app同級(jí)目錄下,再建立一個(gè)nginx.conf文件,并將nginx.conf內(nèi)容修改如下:

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"';
  #請(qǐng)求量級(jí)大建議關(guān)閉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;
   }
  }
}

接下來(lái)只需要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即可。
當(dāng)然,這個(gè)時(shí)候進(jìn)去nginx和uwsgi沒有自動(dòng)啟動(dòng),需要手動(dòng)拉起來(lái),如想自動(dòng)拉起服務(wù),可選用supervisor或者在dockerfile里面加一個(gè)ENTRYPOINT nginx -g "daemon on;" && uwsgi --ini /app/uwsgi.ini

然后隨便跑一個(gè)接口測(cè)試:

到此這篇關(guān)于Docker構(gòu)建python Flask+ nginx+uwsgi容器的文章就介紹到這了,更多相關(guān)Docker構(gòu)建Flask+ nginx+uwsgi內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 繼docker之后podman容器技術(shù)崛起

    繼docker之后podman容器技術(shù)崛起

    這篇文章主要為大家介紹了繼docker之后的podman云原生容器技術(shù)崛起的詳細(xì)介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • docker中mysql開啟日志的實(shí)現(xiàn)步驟

    docker中mysql開啟日志的實(shí)現(xiàn)步驟

    本文主要介紹了docker中mysql開啟日志的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Docker中運(yùn)行的Chrome崩潰問題解決辦法

    Docker中運(yùn)行的Chrome崩潰問題解決辦法

    在開發(fā)與測(cè)試過程中,使用Docker環(huán)境運(yùn)行Chrome瀏覽器可以極大地方便自動(dòng)化測(cè)試與開發(fā),這篇文章主要介紹了Docker中運(yùn)行的Chrome崩潰問題的解決辦法,需要的朋友可以參考下
    2025-08-08
  • 在 Ubuntu 下通過 Docker 部署 Mastodon 服務(wù)器的步驟

    在 Ubuntu 下通過 Docker 部署 Mastodon&nbs

    在Ubuntu系統(tǒng)上通過Docker部署Mastodon服務(wù)器的步驟,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-03-03
  • 清理Docker磁盤空間的幾種有效方法

    清理Docker磁盤空間的幾種有效方法

    隨著Docker的廣泛使用,管理Docker占用的磁盤空間變得越來(lái)越重要,長(zhǎng)時(shí)間運(yùn)行Docker環(huán)境,未使用的容器、鏡像、卷和網(wǎng)絡(luò)等會(huì)逐漸積累,占用大量磁盤空間,本文將介紹幾種清理Docker磁盤空間的有效方法,需要的朋友可以參考下
    2024-08-08
  • docker之WORKDIR指令的使用及說明

    docker之WORKDIR指令的使用及說明

    WORKDIR設(shè)置工作目錄,后續(xù)命令基于此路徑執(zhí)行,若目錄不存在則自動(dòng)創(chuàng)建,RUN指令每層獨(dú)立,不繼承前層狀態(tài),需用WORKDIR確保文件路徑正確,避免因分層存儲(chǔ)導(dǎo)致的文件找不到錯(cuò)誤
    2025-07-07
  • docker中psql數(shù)據(jù)庫(kù)備份與恢復(fù)詳解

    docker中psql數(shù)據(jù)庫(kù)備份與恢復(fù)詳解

    在本篇文章里小編給大家整理的是關(guān)于docker中psql數(shù)據(jù)庫(kù)基本操作內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • Docker?compose搭建ELK服務(wù)基本使用

    Docker?compose搭建ELK服務(wù)基本使用

    這篇文章主要為大家介紹了Docker?compose搭建ELK服務(wù)基本使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Elasticsearch/Kibana密碼設(shè)置方法

    Elasticsearch/Kibana密碼設(shè)置方法

    這篇文章主要介紹了Elasticsearch/Kibana密碼設(shè)置,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • Docker鏡像構(gòu)建原理解析(不裝docker也能構(gòu)建鏡像)

    Docker鏡像構(gòu)建原理解析(不裝docker也能構(gòu)建鏡像)

    這篇文章主要介紹了Docker鏡像構(gòu)建原理解析(不裝docker也能構(gòu)建鏡像),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03

最新評(píng)論