學習大牛筆記nginx + gunicorn + supervisor
安裝 gunicorn
pip install gunicorn
pip 是一個重要的工具,python 用來管理包。還有一個最佳生產就是每次使用 pip 安裝的庫,都寫入一個 requirement 文件裏面,既能知道自己安裝了什麽庫,也方便別人部署時,安裝相應的庫。
pip freeze > requirements.txt
以後每次 pip 安裝了新的庫的時候,都需freeze 一次。
當我們安裝好 gunicorn 之後,需要用 gunicorn 啟動 flask,註意 flask 裏面的name裏面的代碼啟動了 app.run(),這個含義是用 flask 自帶的服務器啟動 app。這裏我們使用了 gunicorn,myapp.py 就等同於一個庫文件,被 gunicorn 調用。
gunicron -w4 -b0.0.0.0:8000 myapp:app
此時,我們需要用 8000 的端口進行訪問,原先的5000並沒有啟用。其中 gunicorn 的部署中,,-w 表示開啟多少個 worker,-b 表示 gunicorn 開發的訪問地址。
想要結束 gunicorn 只需執行 pkill gunicorn,有時候還的 ps 找到 pid 進程號才能 kill。可是這對於一個開發來說,太過於繁瑣,因此出現了另外一個神器---supervisor
,一個專門用來管理進程的工具,還可以管理系統的工具進程。
安裝 supervisor
pip install supervisor echo_supervisord_conf > supervisor.conf # 生成 supervisor 默認配置文件 vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 進程管理
在myapp supervisor.conf 配置文件底部添加 (註意我的工作路徑是/home/rsj217/rsj217/
)
[program:myapp] command=/home/rsj217/rsj217/myproject/venv/bin/gunicorn -w4 -b0.0.0.0:2170 myapp:app ; supervisor啟動命令 directory=/home/rsj217/rsj217/myproject ; 項目的文件夾路徑 startsecs=0 ; 啟動時間 stopwaitsecs=0 ; 終止等待時間 autostart=false ; 是否自動啟動 autorestart=false ; 是否自動重啟 stdout_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.log ; log 日誌 stderr_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.err ; 錯誤日誌
supervisor的基本使用命令
supervisord -c supervisor.conf 通過配置文件啟動supervisor
supervisorctl -c supervisor.conf status 察看supervisor的狀態
supervisorctl -c supervisor.conf reload 重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] 啟動指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname] 關閉指定/所有 supervisor管理的程序進程
supervisor 還有一個web的管理界面,可以激活。更改下配置
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
username=user ; should be same as http_username if set
password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
現在可以使用 supervsior 啟動 gunicorn啦。運行命令 supervisord -c supervisor.conf
訪問 http://127.0.0.1:9001 可以得到 supervisor的web管理界面,訪問 http://127.0.0.1:2170 可以看見gunciron 啟動的返回的 hello world
安裝配置 nginx
采用 apt-get方式安裝最簡單。運行 sudo apt-get install nginx
。安裝好的nginx的二進制文件放在 /usr/sbin/
文件夾下面。而nginx的配置文件放在 /etc/nginx
下面。
使用 supervisor 來管理 nginx。這裏需要註意一個問題,linux的權限問題。nginx是sudo的方式安裝,啟動的適合也是 root用戶,那麽我們現在也需要用 root用戶啟動supervisor。增加下面的配置文件
[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/rsj217/rsj217/myproject/log/nginx.log
stderr_logfile=/home/rsj217/rsj217/myproject/log/nginx.err
到此為止,進步的 web 部屬已經完成。當然,最終我們需要把項目代碼部屬到服務器上.批量的自動化部屬需要另外一個神器 fabric.具體使用,就不再這篇筆記闡述。項目源碼中包含了fabric文件。下載fabric,更改裏面的用戶名和秘密,就可以部屬在自己或者遠程的服務器上了。
作者:人世間
鏈接:http://www.jianshu.com/p/be9dd421fb8d
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請註明出處。
學習大牛筆記nginx + gunicorn + supervisor