認証を使うようなサービスは、SSLを用いてセキュアな通信をしたい。
Flask自身が持ってるwebサーバでは、SSLが使えないので、
apacheやwsgiの力を借りる必要がある。
まず、オレオレ証明書を作る。
$ openssl genrsa -aes128 1024 > server.key $ openssl req -new -key server.key > server.csr $ openssl x509 -in server.csr -days 365 -req -signkey server.key > server.crt
とこんな感じで作れる。
作成したものは、それぞれ
server.key :秘密鍵
server.csr :公開鍵
server.crt :デジタル証明書
である。
apacheの設定は、SSLの方は、
LoadModule ssl_module modules/mod_ssl.so
\#Listen 443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLPassPhraseDialog builtin
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost *:443>
ServerName example.com
WSGIScriptAlias / /home/hogehoge/apps/hello.wsgi
SSLEngine On
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HiGH:+MEDIUM:+LOW
SSLCertificateFile /home/hogehoge/apps/server.crt
SSLCertificateKeyFile /home/hogehoge/apps/server.key
<Directory /home/hogehoge/apps>
WSGIProcessGroup hello
WSGIApplicationGRoup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
となる。
通常のhttpの方は、
<VirtualHost *:80>
ServerName example.com
WSGIDaemonProcess hello user=www-data group=www-data
WSGIScriptAlias / /home/hogehoge/apps/hello.wsgi
<Directory /home/hogehoge/apps>
WSGIProcessGroup hello
WSGIApplicationGRoup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
とした。
サービス本体は、
hello.py
\#!/usr/bin/python
\# -+- coding:utf-8 -*-
from flask import Flask
app = Flask(\_\_name\_\_)
@app.route("/")
def hello():
return "Hello World!"
if \_\_name\_\_ == "\_\_main\_\_":
app.run()
また、hello.wsgiは、
\#!/usr/bin/python \# -+- coding:utf-8 -*- import sys sys.path.insert(0, '/home/hogehoge/apps') from hello import app as application
としておく。
これで、https://127.0.0.1/ でアクセス出来るようになった。
参考: