1.安装必须的几个工具

yum install python-devel # 开发版包含python.h头文件
yum install epel-release # 安装setputools工具 centos默认没有
yum install python-setuptools # 安装setputools工具
easy_install pip # 安装强大的pip
pip install virtualenv #安装虚拟环境
pip install supervisor #安装supervisor

2.在虚拟机中安装flask
cd /www/shop
vim shop.py # 一下内容

from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def index():
return ‘hello world’
if __name__ == ‘__main__':
app.debug = True
app.run()

安装环境 flask是在虚拟机中安装的
virtualenv venv
source venv/bin/activate

pip install flask
pip install gunicorn

3.配置supervisor

echo_supervisord_conf > /etc/supervisord.conf

supervisord -c /etc/supervisord.conf

ps aux | grep supervisord

vi /etc/supervisord.conf 最后两行改为

[include]
files = /etc/supervisor/*.conf
vim /etc/supervisor/shop.conf 适当修改

[program:shop]
command=/www/shop/venv/bin/gunicorn -w4 -b0.0.0.0:8001 shop:app
;numprocs=1
;process_name=%(program_name)s
directory=/www/shop
user=root
autorestart=true
redirect_stderr=true
stdout_logfile = /www/shop/log/info.log
loglevel=info
supervisorctl reload 重载配置
supervisorctl status 查看状态

4.配置nginx
新建nginx配置文件 /usr/local/nginx/conf/vhost/shop.conf

server {

listen 80;
server_name yourdomain.com;

root /www/shop;

location / {
proxy_pass http://127.0.0.1:8001; #gunicorn对应的端口
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
保存后测试配置并重启nginx配置
/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx reload
直接下载看:部署flask项目

在supervisor默认配置中,其启动的sock等都会放到tmp目录,而tmp目录会自动清理导致无法使用supervisorctl命令,此时:

修改supervisor.conf文件,修改到/var/run/及/var/log/目录,具体配置就不进行贴了,简单直接搜索tmp进行修改即可。
重启supervisor服务,记得kill原来服务。

来自:http://blog.csdn.net/binggoogle/article/details/70820966

需求:最近用php做了一个功能需要用shell定时跑,但是这个数据很大,php一次执行不完,所以需要分页。那么问题来了,由于并不知道要分多少页面。shell脚本的crontab并不太好设定

那么我就用Python写了个程序,来调用这个分页的php接口。好了废话少说,直接上代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import urllib2  
import json
import datetime

starttime = datetime.datetime.now()

types = sys.argv[1]
counts = sys.argv[2]

setDomain = 'http://localhost'
url =setDomain+'/views/count?type='+types+'&count='+counts  

def geturls( strurl ):
	# print strurl
	getResult = urllib2.urlopen(strurl,timeout = 10)
	res = int(getResult.read())
	getResult.close()
	if res != 1:
		print 'timeout'
 	return

if types and counts:
	data = urllib2.urlopen(url,timeout = 10)  
	getJson = data.read()
	s = json.loads(getJson)

	if  s['code'] == 0:
		for urls in s['message']:
			geturls(urls)
	else:
		print s['message']

	# get time
	endtime = datetime.datetime.now()
	end = (endtime - starttime).seconds
	print 'success : '+str(end)+' S'
else:
	sys.exit(0)

把上面的代码保存成 do.py

# python do.py 1 250
success : 36 S

简单说明一下  url接口是用来获取分页数目的  而urls最终是执行分页  用urllib2.urlopen获取结果 datetime获取执行时间

目前的程序需要执行36S 还能容忍,以后会改进一下,比如加入队列处理防止超时