在CentOS上配置Nginx+Gunicorn+Python+Flask環(huán)境的教程
Python基礎(chǔ)環(huán)境搭建
CENTOS 6.X 系列默認(rèn)安裝的 Python 2.6 ,目前開發(fā)中主要是使用 Python 2.7 ,這兩個版本之間還是有不少差異的,程序在 Python 2.6 下經(jīng)常會出問題。
比如: re.sub 函數(shù) ,2.7 支持 flags 參數(shù),而 2.6 卻不支持。
所以,打算安裝 Python 2.7 來運行 Flask 應(yīng)用程序,但 2.6 不能刪除,因為系統(tǒng)對它有依賴。
1、安裝 sqlite-devel
因為 Flask 應(yīng)用程序可能使用能 Sqlite 數(shù)據(jù)庫,所以這個得裝上(之前因為沒裝這個,導(dǎo)致 Python 無法導(dǎo)入 sqlite3 庫。
當(dāng)然,也可以從源碼編譯安裝。
yum install sqlite-devel -y
2、安裝 Python 2.7
wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz tar xf Python-2.7.8.tgz cd Python-2.7.8 ./configure --prefix=/usr/local make && make install
安裝成功之后,你可以在 /usr/local/bin/python2.7 找到 Python 2.7。
3、安裝 setuptools + pip
這里需要注意,一定要使用 python2.7 來執(zhí)行相關(guān)命令。
# First get the setup script for Setuptools: wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py # Then install it for Python 2.7 : python2.7 ez_setup.py # Now install pip using the newly installed setuptools: easy_install-2.7 pip # With pip installed you can now do things like this: pip2.7 install [packagename] pip2.7 install --upgrade [packagename] pip2.7 uninstall [packagename]
4、使用 virtualenv
# Install virtualenv for Python 2.7 and create a sandbox called my27project: pip2.7 install virtualenv virtualenv-2.7 my27project # Check the system Python interpreter version: python --version # This will show Python 2.6.6 # Activate the my27project sandbox and check the version of the default Python interpreter in it: source my27project/bin/activate python --version # This will show Python 2.7.X deactivate
基本就是這些了,網(wǎng)上很多教程都說要做軟鏈接,但我感覺那樣做或多或少會對系統(tǒng)有一些未知的影響。這個方法能盡量保持系統(tǒng)的完整性,很多自帶 Python 程序其實在頭部都指定了 #!/usr/bin/python ,所以它們用的其實是 Python 2.6 ,而不是新安裝的 Python 2.7 。
Nginx+Supervisor+Gunicorn部署Flask應(yīng)用程序
1.安裝supervisor
$ sudo pip install supervisor
創(chuàng)建一個Flask程序
創(chuàng)建虛擬環(huán)境:
$ mkdir /tmp/wwwroot/web1 $ cd /tmp/wwwroot/web1 $ virtualenv deps $ source deps/bin/activate $ pip install flask gunicorn
創(chuàng)建一個簡單的Flask程序:
$ cat > myapp.py << EOF
from flask import Flask app = Flask(__name__) @app.route("/") def index(): return "hello flask 01"
使用gunicorn執(zhí)行Flask程序:
最簡單的用法:
$ gunicorn -b 127.0.0.1:3721 myapp:app
現(xiàn)在訪問http://127.0.0.1:3721,應(yīng)該可以看到"hello flask 01"。
這里3721端口只是一個演示。
2.配置supervisor
創(chuàng)建配置文件:
$ cd /tmp/wwwroot $ echo_supervisord_conf > supervisor.conf $ cat >> supervisor.conf << EOF
[program:myapp] ;user=digwtx command=/tmp/wwwroot/web1/deps/bin/gunicorn -b 127.0.0.1:3721 myapp:app directory=/tmp/wwwroot/web1 process_name=%(program_name)s ; process_name expr (default %(program_name)s) numprocs=1 ; number of processes copies to start (def 1) stopsignal=QUIT ; signal used to kill process (default TERM) redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/tmp/myapp.log ; stdout log path, NONE for none; default AUTO
啟動進程:
$ supervisord -c supervisor.conf
管理進程:
$ supervisorctl -c supervisor.conf
3.配置nginx:
主要是把請求轉(zhuǎn)交給gunicorn進行處理。
server { listen 8080; #默認(rèn)請求 location / { #請求轉(zhuǎn)向本機ip:3721 proxy_pass http://127.0.0.1:3721; # 這里是gunicorn監(jiān)聽的地址 proxy_redirect off; proxy_set_header Host $host:8080; #如果端口不是80要寫上 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
現(xiàn)在重啟nginx,訪問http://127.0.0.1:8080應(yīng)該可以看到"hello flask 01"。
自動啟動:
那么,如果想開機時自動啟動怎么辦呢?或者說,如果機器重啟了,那WEB服務(wù)就斷了。
其實呢,也很簡單,只要在/etc/rc.d/rc.local中加入一句就可以了:
supervisord -c /tmp/wwwroot/supervisor.conf
相關(guān)文章
Python strip lstrip rstrip使用方法
Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左邊的字符,rstrip用于去除右邊的字符。這三個函數(shù)都可傳入一個參數(shù),指定要去除的首尾字符。2008-09-09python爬蟲入門教程--優(yōu)雅的HTTP庫requests(二)
requests 實現(xiàn)了 HTTP 協(xié)議中絕大部分功能,它提供的功能包括 Keep-Alive、連接池、Cookie持久化、內(nèi)容自動解壓、HTTP代理、SSL認(rèn)證等很多特性,下面這篇文章主要給大家介紹了python爬蟲入門中關(guān)于優(yōu)雅的HTTP庫requests的相關(guān)資料,需要的朋友可以參考下。2017-05-05如何解決Selenium包安裝成功卻無法導(dǎo)入的問題
這篇文章主要介紹了如何解決Selenium包安裝成功卻無法導(dǎo)入的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08使用Python根據(jù)一個列表的順序?qū)ζ渌斜磉M行排序
這篇文章主要介紹了使用Python根據(jù)一個列表的順序?qū)ζ渌斜磉M行排序,根據(jù)列表B中每個元素的下標(biāo)來獲取列表A中對應(yīng)位置的元素,將其作為排序依據(jù)即可,需要的朋友可以參考下2023-10-10