Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save willwhui/882ca2713ad4b096fef80adf3e3d69e7 to your computer and use it in GitHub Desktop.
Save willwhui/882ca2713ad4b096fef80adf3e3d69e7 to your computer and use it in GitHub Desktop.
搭建api.ai webhook(在sentris.net的vps上使用python+flask+jinja)
搭建api.ai webhook(在sentris.net的vps上使用python+flask+jinja)
@willwhui
Copy link
Author

willwhui commented Jun 29, 2017

VPS OS版本(cat /etc/issue):Ubuntu 16.10
默认带了python3.5.2
需安装pip3: sudo apt-get install python3-pip
需安装flask:sudo pip3 install flask
安装flask成功后,提醒可以升级pip到9.0.1: sudo pip3 install --upgrade pip
于是升级了pip3,不知道有没有问题。
安装flask的同时,自动安装了需要用到的jinja2模板

安装之前,硬盘空间:忘了记录了...
完成后,硬盘空间:1.02GB

按照教程搭建一个简单的web服务

运行简单的web服务之前,系统资源:
Tasks: 24 total, 1 running, 23 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.8 us, 6.6 sy, 0.0 ni, 75.7 id, 12.6 wa, 0.0 hi, 1.3 si, 0.0 st
KiB Mem : 131072 total, 20624 free, 101576 used, 8872 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 20624 avail Mem

运行简单的web服务之后,系统资源:
Tasks: 25 total, 1 running, 24 sleeping, 0 stopped, 0 zombie
%Cpu(s): 4.9 us, 6.7 sy, 0.0 ni, 86.9 id, 0.2 wa, 0.0 hi, 1.4 si, 0.0 st
KiB Mem : 131072 total, 5880 free, 116404 used, 8788 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 5880 avail Mem

让外部可以访问flask服务,参见这个专门介绍Flask的网站
app.run(host='0.0.0.0')

@willwhui
Copy link
Author

willwhui commented Jun 29, 2017

(以后用)nginx作为反向代理

参考这里进行配置:
sudo apt-get install nginx
修改/ext/nginx/sites-available/default

    server {
      listen  80;
      server_name XXX.XXX.XXX; #公网地址
    
      location / {
        include      uwsgi_params;
        uwsgi_pass   127.0.0.1:8001;  # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
        uwsgi_param UWSGI_PYHOME /home/www/my_flask/venv; # 指向虚拟环境目录
        uwsgi_param UWSGI_CHDIR  /home/www/my_flask; # 指向网站根目录
        uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序
      }
    }

重启nginx:
sudo service nginx restart

@willwhui
Copy link
Author

willwhui commented Jun 29, 2017

支持https

按照python flask搭建https安全协议提到的方法,安装并使用openssl
安装的时候使用
pip3 install pyOpenSSL
因为本地没有安装pip

然而,使用ad-hoc作为ssl context并没有成功,在本地python3.4下运行python3 app.py之后报错:
TypeError: Using ad-hoc certificates requires the pyOpenSSL library.

werkzeug.serving展示的源码是:

def _get_openssl_crypto_module():
    try:
        from OpenSSL import crypto
    except ImportError:
        raise TypeError('Using ad-hoc certificates requires the pyOpenSSL '
                        'library.')
    else:
        return crypto

这错误看起来是pythone3认为没有openssl。

于是按照这里的方法
sudo apt-get update
sudo apt-get install python3-openssl
虽然也安装成功了,但是依然显示同样的错误。。。

ps,这里提到了flask使用ssl指定自己的context一种方法。
(以及一个bug?)

在python3命令行下执行
form OpenSSL import crypto
提示没有cryptography
于是按照这里的说明安装cryptography:

sudo apt-get install build-essential libssl-dev  libffi-dev python-dev
pip3 install cryptography

注意:
文章中
pip install crpytography是作者拼错了,应为:
pip install cryptography
落实到我这里应该是:
pip3 install cryptography
所以不要copy/paste他的。

这样就好了。
总结,估计正确的方法是:

sudo apt-get install build-essential libssl-dev  libffi-dev python-dev
sudo pip3 install cryptography
sudo pip3 install pyOpenSSL

VPS下:
sudo pip3 install cryptography
出错
error: command 'x86_64-linux-gnu-gcc' failed with exit status 4

看起来是VPS的内存太小:
dmesg | tail
显示:

[474324.123066] Memory cgroup out of memory: Kill process 17317 (sshd) score 9 or sacrifice child
[474324.123131] Killed process 21362 (sshd) total-vm:106912kB, anon-rss:1080kB, file-rss:0kB

尝试建立swap分区:
运营商关闭了建立swap的能力。。。

尝试把本地的拷贝到服务器上去:
首先sudo find / -name "cryptography",找到需要拷贝的目录
然后scp -P port-number -r source-dir username@xxx.xxx.xxx.xxx:dest-parent-dir
然后设置python path
sudo vi /etc/profile
增加export PATH=<你要加入的路径>:$PATH

但是,运行python3 app.py时,依然找不到依赖的包_openssl
貌似这个包是编译时产生的。

@willwhui
Copy link
Author

willwhui commented Jun 29, 2017

本地python3.5用不了flask

本地ubuntu14.04:
有python 2.7, 3.4两个预装的版本以及一个为了和服务器保持一致自行安装的python3.5

@willwhui
Copy link
Author

willwhui commented Jun 30, 2017

因为无法使用OpenSSL 停止在此vps上搭建web服务

原因:内存太小无法编译

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment