這個(gè)系統(tǒng)的功能非常簡(jiǎn)單炸渡,只是在頁(yè)面上顯示一些文本(Hello区匣,XXX)梦皮。搭建這個(gè)系統(tǒng)的初衷并不是為了完成這個(gè)功能炭分,而是將碼農(nóng)周刊中使用到的一些技術(shù)都自己測(cè)試一遍。一方面了解下現(xiàn)在互聯(lián)網(wǎng)公司所使用的一些技術(shù)届氢,另一方面為以后工具選型積累一些知識(shí)欠窒,提供一些參考。
搭建思路也是按照開發(fā)思路退子,先出來(lái)一個(gè)雛形岖妄,然后慢慢豐滿羽翼。
其中有些技術(shù)和碼農(nóng)周刊中使用的不一樣寂祥,主要考慮到本人的熟悉程度和工作需要荐虐。比如周刊的web開發(fā)框架是pyramid,WSGI協(xié)議實(shí)現(xiàn)使用gunicorn丸凭,我這里分別使用的是flask和uWSGI福扬。
網(wǎng)頁(yè)顯示Hello Simon
- 安裝virtualenv隔離環(huán)境
$ sudo pip install virtualenv
$ virtualenv --no-site-packages aries # 不使用系統(tǒng)的依賴
hqi@hqi-VirtualBox:~/buildout$ virtualenv --no-site-packages aries
New python executable in /home/hqi/buildout/aries/bin/python
Installing setuptools, pip, wheel...done.
hqi@hqi-VirtualBox:~/buildout/aries$ source ./bin/activate
(aries) hqi@hqi-VirtualBox:~/buildout/aries$
使用virtualenv沙盒創(chuàng)建一個(gè)虛擬環(huán)境。
- 安裝web開發(fā)框架flask
pip install flask
- 網(wǎng)頁(yè)顯示
(aries) hqi@hqi-VirtualBox:~/buildout/aries$ mkdir app
新建三個(gè)文件惜犀,app/init.py, app/views.py和run.py
其中init.py創(chuàng)建flask application實(shí)例铛碑,views.py引入視圖,完成URL映射虽界,run.py監(jiān)聽請(qǐng)求
init.py文件內(nèi)容
from flask import Flask
app = Flask(__name__)
from app import views
views.py內(nèi)容
from flask import Flask
app = Flask(__name__)
from app import views
(aries) hqi@hqi-VirtualBox:~/buildout/aries/app$ cat views.py
from app import app
# URL映射汽烦,分別為/和/index
@app.route('/')
@app.route('/index')
def welcome():
return "Hello, Wendy & Simon!"
run.py內(nèi)容:
from app import app
app.debug = True
執(zhí)行結(jié)果
(aries) hqi@hqi-VirtualBox:~/buildout/aries$ python run.py
* Running on http://192.168.1.109:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 214-462-276
訪問網(wǎng)頁(yè)
我們使用的是flask自帶的WSGI實(shí)現(xiàn)工具,沒有用到gunicorn或者uWSGI莉御,后面會(huì)介紹Flask如何配合uWSGI使用撇吞。
- 集成celery異步任務(wù)調(diào)度器
Celery是Python開發(fā)的分布式任務(wù)調(diào)度模塊,接口簡(jiǎn)單礁叔,開發(fā)容易牍颈,通過(guò)第三方消息服務(wù)傳遞任務(wù)。stack overflow上有討論celery和apscheduler的選擇琅关,結(jié)論是推薦使用celery的多些煮岁。有關(guān)celery的高級(jí)應(yīng)用,會(huì)專門寫一篇文章道來(lái)涣易。
Flask 與 Celery 整合是十分簡(jiǎn)單人乓,不需要任何插件。
- 安裝celery
(aries) hqi@hqi-VirtualBox:~/buildout/aries$ pip install celery
- 在初始化代碼中添加celery部分都毒,使用redis作為celery消息通道
from flask import Flask
from celery import Celery
flask_app = Flask(__name__)
celery = Celery(flask_app.name, \
broker='redis://localhost:6379/0')
celery.conf.update(flask_app.config)
from app import views
- 添加控制層代碼control.py
from app import celery
@celery.task
def get_name(name="Simon"):
return name
- 在view.py中添加調(diào)用控制層接口
from app import flask_app
import control
@flask_app.route('/')
@flask_app.route('/index')
def welcome(content='Simon'):
return "Hello, " + content
@flask_app.route('/name')
def print_name():
return control.get_name("Wendy & Simon")
============ flask 和 Celery整合介紹完畢 =================
- 安裝uWSGI
$ pip install uWSGI
$ uwsgi -h
uwsgi -h出現(xiàn)幫助提示,表明安裝成功
- 配置uWSGI
(aries) hqi@hqi-VirtualBox:~/buildout/aries/uwsgi$ vi aries_uwsgi.ini
[uwsgi]
# uwsgi啟動(dòng)所使用的地址與端口
socket = 192.168.1.109:8001
#網(wǎng)站目錄
chdir = /home/hqi/buildout/aries
# python 啟動(dòng)程序
wsgi-file = run.py
#flask application實(shí)例名稱
callable = flask_app
# 處理器個(gè)數(shù)和線程數(shù)
processes = 4
thread = 2
#狀態(tài)檢測(cè)地址
stats = 192.168.1.109:9191
$ uwsgi --ini /home/hqi/buildout/aries/uwsgi/aries_uwsgi.ini --http :8002
遇到的問題:
- unable to load configuration from uwsgi
run.py中執(zhí)行程序要放在main中 - invalid request block size: 21573 (max 4096)...skip
uWSGI默認(rèn)協(xié)議是tcp碰缔,而瀏覽器訪問協(xié)議是http账劲,解決方案是直接提供http服務(wù)。
uwsgi --ini /home/hqi/buildout/aries/uwsgi/aries_uwsgi.ini --http :8002 - 如果通過(guò)nginx訪問uwsgi,uwsgi則可使用以下方式啟動(dòng)
ref: http://www.tuicool.com/articles/2Araye
- 安裝并運(yùn)行Nginx
$ sudo apt-get install nginx
$ /etc/init.d/nginx start
訪問主機(jī)IP地址瀑焦,會(huì)看到Nginx歡迎頁(yè)面腌且。
由于Nginx不能直接執(zhí)行托管Python應(yīng)用程序,所以需要借助uWSGI
- 配置Nginx和uWSGI
Nginx和uWSGI通過(guò)socket文件相互通信
- 配置Nginx
Nginx默認(rèn)的配置文件位于:
/etc/nginx/sites-enabled/default
我們?cè)趧e的目錄下新建一個(gè)配置文件
(aries) hqi@hqi-VirtualBox:~/buildout/aries/nginx$ cat aries_nginx.conf
server {
listen 316; # 80端口被占榛瓮,使用別的端口號(hào)
server_name 192.168.1.109;
charset utf-8;
client_max_body_size 75M;
location / {
include uwsgi_params;
# config uwsgi link
# uwsgi_pass unix:/home/hqi/buildout/aries/nginx/aries_uwsgi.sock;
uwsgi_pass 192.168.1.109:8001;
uwsgi_param UWSGI_PYHOME /home/hqi/buildout/aries;
uwsgi_param UWSGI_CHDIR /home/hqi/buildout;
uwsgi_param UWSGI_SCRIPT run:flask_app;
}
}
# Two ways to restart nginx
$ sudo /etc/init.d/nginx restart
$ sudo service nginx restart
$ sudo nginx -c ~/buildout/aries/nginx/aries_nginx.conf
nginx: [emerg] "server" directive is not allowed here in /home/hqi/buildout/aries/nginx/aries_nginx.conf:1
Nginx必須指定配置主文件铺董,再通過(guò)主文件加載*.conf配置文件。
檢測(cè)配置文件是否正確:
$ sudo nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo service nginx restart
[sudo] password for hqi:
* Restarting nginx nginx [ OK ]
- 安裝supervisor
supervisor使用python寫的進(jìn)程控制系統(tǒng)禀晓。它會(huì)監(jiān)測(cè)后臺(tái)進(jìn)程的運(yùn)行狀態(tài)精续,幫助你維護(hù),如果它們不小心crash了粹懒,還會(huì)幫助你重啟重付。
$ sudo apt-get install supervisor
$ echo_supervisord_conf > aries_supervisor.conf
配置supervisor配置文件,其中最重要的配置項(xiàng)為program和group
[program:nginx]
command=nginx -c /home/hqi/buildout/aries/nginx/aries_nginx.conf ; the program (relative uses PATH, can take args)
process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
directory=/home/hqi/buildout/aries/supervisor ; directory to cwd to before exec (def no cwd)
umask=022 ; umask for process (default None)
priority=999 ; the relative start priority (default 999)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=INT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/home/hqi/buildout/aries/supervisor/log/stdout.log ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false ; emit events on stdout writes (default false)
stderr_logfile=/home/hqi/buildout/aries/supervisor/log/stderr.log ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
##### Start supervisor server #####
$ sudo supervisord -c aries_supervisor.conf
##### Shutdown supervisor server #####
$ sudo supervisorctl shutdown
##### Reload configuration file #####
$ sudo supervisorctl reload
===================================
以上一個(gè)基本系統(tǒng)搭建完成凫乖,下面我會(huì)介紹數(shù)據(jù)庫(kù)的存取和SQLAlchemy的使用确垫,數(shù)據(jù)庫(kù)選擇MySQL數(shù)據(jù)庫(kù)