最近雖然學(xué)術(shù)進(jìn)展依然緩慢,可是沒有那么大的心理壓力了。
原因可能有二,一是前一段剛投了一篇文章出去,二是前一段審了一篇投在還不錯(cuò)的期刊上的超水的文章,結(jié)果別的reviewer還給放過(guò)去了,我直接讓打回重改了??赡苁强吹竭@么水的文章都有希望發(fā)這樣的期刊吧……
最近創(chuàng)建了一個(gè)自己的微信公眾號(hào),滑雪前線,用來(lái)分享一些滑雪相關(guān)資訊,因?yàn)槲野l(fā)現(xiàn)國(guó)內(nèi)的滑雪雖然已經(jīng)熱起來(lái)了,但是還沒有形成相應(yīng)的文化。
可是自己畢竟是碼農(nóng)出身,天生的手賤,不甘于每天安安心心找?guī)灼恼路g翻譯、搬運(yùn)搬運(yùn),總想要把技術(shù)方面的東西用上來(lái)。這不,開始給公眾號(hào)搭建服務(wù)器了。
主要參考這篇Blog,但是由于按照原博客Token驗(yàn)證不了,且原博客werobot版本較老,對(duì)代碼有所微調(diào)。
買的是阿里云最便宜1核1G內(nèi)存,1M帶寬,40G硬盤的ECS服務(wù)器,OS Ubuntu 14.04 64位操作系統(tǒng),一個(gè)月68塊錢,對(duì)我來(lái)說(shuō)也就是兩頓飯錢。
主要組件展示
- Nginx
- Gunicorn
- Python
- Pip
- Virtualenv
- Flask
- Supervisor
- WeRoBot SDK
簡(jiǎn)單說(shuō)下主要邏輯關(guān)系,我們用Nginx作為Web服務(wù)器,該服務(wù)器無(wú)法直接和Flask (or Python)交互,所以我們需要引入Gunicorn,它是一個(gè)獨(dú)立的WSGI容器,可以容納WSGI應(yīng)用并且提供HTTP服務(wù)。而后,用Supervisor 管理服務(wù)器進(jìn)程,當(dāng)某個(gè)應(yīng)用掛掉,可以自動(dòng)重啟。
安裝Virtualenv及必要組件
用來(lái)創(chuàng)建不同的Python隔離環(huán)境,可以保證一個(gè)干凈的環(huán)境。
$ adduser wechatmanager #創(chuàng)建新用戶
$ sudo apt-get update
$ sudo apt-get install zsh # 強(qiáng)烈推薦該shell
$ sudo apt-get install -y python python-pip python-virtualenv # 安裝各類組件
$ sudo mkdir /home/wechatmanager && cd /home/wechatmanager
$ sudo virtualenv wechat_connector# 此時(shí)在mirror文件夾下創(chuàng)建一個(gè)虛擬環(huán)境
$ cd /home/wechatmanager/wechat_connector
$ sudo source ./bin/activate # 激活虛擬環(huán)境
$ sudo apt-get install -y nginx gunicorn # 安裝各類組件
# 取消激活命令為
deactivate
安裝WeRoBot SDK
$ sudo pip install werobot
基本應(yīng)答helloworld代碼 in app.py:
import werobot
robot = werobot.WeRoBot(token='tokenhere')
# 消息處理函數(shù)
@robot.handler
def echo(message):
return 'Hello World!'
robot.run(server="gunicorn", port=8000)
然后去微信公眾平臺(tái)把Token改為你自己設(shè)置的Token。
配置 Nginx
$ sudo /etc/init.d/nginx start #啟動(dòng)nginx
$ sudo rm /etc/nginx/sites-enabled/default #刪除默認(rèn)配置
$ sudo touch /etc/nginx/sites-available/wechat_connector#建立項(xiàng)目文件
$ sudo ln -s /etc/nginx/sites-available/wechat_connector/etc/nginx/sites-enabled/wechat_connector #設(shè)置軟鏈接
$ sudo vim /etc/nginx/sites-enabled/wechat_connector #編輯項(xiàng)目文件
添加:
server {
server_name your_domain;
listen 80;
location /weixin {
proxy_pass http://127.0.0.1:8000;
}
}
騰訊公眾號(hào)默認(rèn)只能連接http的80端口和https的443端口
當(dāng)公眾號(hào)向(your_domain/weixin)發(fā)送消息,便會(huì)反向代理到127.0.0.1的8000端口,這也是為什么我們之前的代碼監(jiān)聽8000端口的原因
# 重啟 nginx:
$ sudo /etc/init.d/nginx restart
配置 Supervisor
$ sudo apt-get install -y supervisor #安裝
$ sudo vim /etc/supervisor/conf.d/test.conf #創(chuàng)建配置文件
添加:
[program:wechat_connector]
command = python /home/wechatmanager/wechat_connector/app.py
directory = /home/wechatmanager/wechat_connector
timeout = 60*60
user = newuser
autostart = true
autorestart = true
redirect_stderr = true
stdout_logfile = /home/mirror/logs/wechat_connector.log
啟動(dòng)Supervisor
$ sudo supervisorctl reread
$ sudo supervisorctl update
$ sudo supervisorctl start wechat_connector ## 重啟supervisor
$ sudo supervisorctl restart wechat_connector
微信平臺(tái)端設(shè)置
進(jìn)入微信公眾平臺(tái)—>開發(fā)者模式—>設(shè)置URL和Token,URL為your_domain/weixin,Token為你在werobot中設(shè)定的
如果配置沒有問(wèn)題,順利通過(guò),用微信給你的公眾平臺(tái)發(fā)消息應(yīng)該能看到’hello world!’。
Tips: 出現(xiàn)任何問(wèn)題,重啟supervisor,倘若仍不能夠解決,請(qǐng)查看log文件。
參考
阿里云ECS搭建微信公眾平臺(tái)
WeRoBot
VPS環(huán)境搭建詳解
Flask on Ubuntu