在CentOS上配置Nginx+Gunicorn+Python+Flask環(huán)境的教程
Python基礎(chǔ)環(huán)境搭建
CENTOS 6.X 系列默認安裝的 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 應用程序,但 2.6 不能刪除,因為系統(tǒng)對它有依賴。
1、安裝 sqlite-devel
因為 Flask 應用程序可能使用能 Sqlite 數(shù)據(jù)庫,所以這個得裝上(之前因為沒裝這個,導致 Python 無法導入 sqlite3 庫。
當然,也可以從源碼編譯安裝。
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應用程序
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,應該可以看到"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;
#默認請求
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應該可以看到"hello flask 01"。
自動啟動:
那么,如果想開機時自動啟動怎么辦呢?或者說,如果機器重啟了,那WEB服務就斷了。
其實呢,也很簡單,只要在/etc/rc.d/rc.local中加入一句就可以了:
supervisord -c /tmp/wwwroot/supervisor.conf
相關(guān)文章
Python利用keyboard模塊實現(xiàn)鍵盤記錄操作
模擬鍵盤操作執(zhí)行自動化任務,我們常用的有pyautowin等自動化操作模塊。今天介紹的這個模塊叫做keyboard,它是純Python原生開發(fā),編譯時完全不需要依賴C語言模塊。一行命令就能完成安裝,非常方便,需要的可以了解一下2022-10-10
用Matlab讀取CSV文件出現(xiàn)不匹配問題及解決
這篇文章主要介紹了用Matlab讀取CSV文件出現(xiàn)不匹配問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python創(chuàng)建一個功能齊全的隨機驗證碼生成器
驗證碼通過生成一張包含難以識別的文本、數(shù)字或圖像的圖像,要求用戶正確輸入其中的內(nèi)容,用于驗證用戶身份,本篇教程將帶領(lǐng)您一步步使用Python創(chuàng)建一個功能齊全的隨機驗證碼生成器,我們將通過導入必要的庫、定義輔助函數(shù)以及編寫主函數(shù)check_code()2024-04-04
python實現(xiàn)顏色rgb和hex相互轉(zhuǎn)換的函數(shù)
這篇文章主要介紹了python實現(xiàn)顏色rgb和hex相互轉(zhuǎn)換的函數(shù),可實現(xiàn)將rgb表示的顏色轉(zhuǎn)換成hex值的功能,非常具有實用價值,需要的朋友可以參考下2015-03-03

