KnowHow

技術的なメモを中心にまとめます。
検索にて調べることができます。

flaskのアプリケーションを新たに公開する(HTTPS)

登録日 :2024/02/10 14:37
カテゴリ :Linux

flaskのアプリケーションを新たに公開するにあたり、実施したこと。

方法1:サービスファイルを作成して公開

一番簡単な方法は、/etc/systemd/systemにgunicornを介して公開するサービスファイルを作成すること。
gunicornで起動するときに、ポート番号を指定します。
簡単であることがメリットの反面、デメリットとしては、専用のポート番号を公開する必要があることと、Webサービスにアクセスするときにポート番号を使う必要があることです。
この場合、空いているポート番号がわかってしまうので、セキュリティ上よくないです。
また、HTTPS(暗号化)にも対応できません。

方法2 ngixとgunicornを用いる方法

手間はかかりますが、ngixのリバースプロキシサーバを使って、gunicornソケットにルーティングする方法があります。今回、この方法を使うことで、HTTPSにも対応することができました。

1 まずアプリケーション用のサーバ名を取得します(名前解決が可能なドメイン名)
  ドメイン名をサービス名として使います。
  このとき、既存のサービス名を使わないようにしましょう。
  私は、別途Djangoで構築したWebアプリをngixで公開していたこともあり、
  コンフリクトして公開できませんでした。

2 サービスファイル、ソケットファイル、ngixの設定ファイル(シンボリックリンク含む)を作成

/etc/systemd/system/ に、gunicorn_appname.serviceファイル、gunicorn_appname.socketを作成

 /etc/nginx/sites-available にアプリの設定ファイル(myapp)を作成
 これを、シンボリックリンクで、sites-enableに登録する
 sites-enableに登録する際は、フルパスで行うこと。

設定ファイルの書き方が別途まとめています。

この時点で、gunicorn.socket, gunicorn.service, ngixをリスタートすることでサイトにアクセスできます

3 HTTPSに対応する

Let's Encrypt を使ってサイトをHTTPSに対応させます。インストール方法や設定方法は別途まとめていますが、ここでは、certbotがある前提でHTTPS化した時の画面コピーをサンプルでつけます。
 コマンドを使って、サイトのURLを指定して実行するだけです。

user@servername:/etc/nginx/sites-available$ sudo certbot --nginx -d sitename.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for irbank.believeriver.site
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/ngix_settings_file

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/ngix_settings_file

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://sitename.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=sitename.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/irbank.believeriver.site/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/irbank.believeriver.site/privkey.pem
   Your cert will expire on 2024-05-10. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

user@servername:/etc/nginx/sites-available$ ls