centos+flask+uwsgi+nginx+supervisor部署小結

來源:互聯網
上載者:User
0x00 head

以前喜歡用ubuntu,但實際部署項目實驗室還是要求用centos,只好再搭一遍,中途遇到了不少的坑,做個筆記以備之後查詢,不想再去翻那一堆書籤了,亂七八糟的,還是自己總結的東西看著舒服。 0x01 安裝pip和easy_install

安裝easy_install

wget -q http://peak.telecommunity.com/dist/ez_setup.pypython ez_setup.py

安裝pip

先安裝依賴:

yum groupinstall "Development tools"yum install zlib zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gztar zvxf 1.5.5.tar.gz   cd pip-1.5.5/python setup.py install
0x02 安裝uwsgi
pip install uwsgi

在項目的根目錄下建立設定檔:

vim config.ini

填寫以下內容:

[uwsgi]socket = 127.0.0.1:8001     //啟動程式時所使用的地址和連接埠chdir = /home/www/          //你的項目目錄wsgi-file = manage.py       //flask程式的開機檔案callable = app              //程式內啟用的application變數名processes = 2               //處理器個數threads = 4                 //線程個數stats = 127.0.0.1:9191      //擷取uwsgi統計資訊的服務地址

關於wsgi在web服務中的作用,請看這裡:python wsgi 簡介

啟動服務:

uwsgi config.ini
0x03 安裝nginx

配置源並安裝:

yum install yum-fastestmirroryum install nginx

查看nginx應用的設定檔:

nginx -t

我們需要改動設定檔的地方如下(注意是需要改動的地方,其餘的地方按你的需要去改):

server {        listen       80;        server_name  www.test.com;  # nginx在只有一個server的時候會忽略這個選項,所以隨便填吧        access_log  /logs/python.flask.access.log;  # 日誌位置        location / {            include      uwsgi_params;            uwsgi_pass   127.0.0.1:8001;            uwsgi_param UWSGI_PYHOME /usr/bin/python; # 指向虛擬環境目錄(我沒用虛擬環境,寫的直接是python程式的位置)            uwsgi_param UWSGI_CHDIR  /usr/apps/python/your_pro; # 指向網站根目錄            uwsgi_param UWSGI_SCRIPT manage:app; # 指定啟動程式        }

其餘的按需修改吧。

在開啟uwsgi後,繼續開啟nginx,基礎的配置就OK了。 0x04 安裝Supervisor

經過以上的配置,其實你的網站已經可以正常訪問了,速度嗖嗖的。但有很多時候會有意外情況,uwsgi 掛了怎麼辦。這個時候就需要一個進程守護的程式來協助你重新啟動它。

Supervisor是一個進程管理工具,通俗點就是幫你監控某個進程,如果發現它掛了,立馬把它續起來。

安裝:

pip install supervisor

組建組態檔案:

cd /usr/bin/./echo_supervisord_config > /etc/supervisord.conf

接下來修改設定檔,在設定檔中;是注釋符,去掉前面的分號:

[program:project_name]          # project_name為項目名稱command = uwsgi --ini /home/flask/config.ini  # 把啟動uwsgi的命令寫在這,將config.ini的路徑替換為絕對路徑stopsignal=QUITautostart=trueautorestart=truestdout_logfile=/var/log/uwsgi/supervisor_flask.log      # 作業記錄stderr_logfile=/var/log/uwsgi/supervisor_flask_err.log  # 錯誤記錄檔

啟動:

supervisord -c /etc/supervisord.conf

之後先查看 uwsgi 的進程:

ps aux | grep uwsgi

kill掉它,之後再ps一下看進程起來沒有:

killall uwsgi

完。

最後放上一張美美的截圖,介面用的妹子UI:

參考文檔:
http://www.tuicool.com/articles/Ejm2u2
http://www.cnblogs.com/dspace/archive/2016/07/06/5647587.html
https://segmentfault.com/a/1190000004294634

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.