60歳からITエンジニアを目指す無謀なブログ

60歳で定年し、職種を変更してIT技術者を目指すブログです。

ロリポップレンタルサーバでFlask, GunicornレスポンシブWebの公開方法

ロリポップレンタルサーバを使用する前提です。(マネージドクラウドではありません)

python3は既にインストールされているのでインストールはFlaskだけで良いです。

コマンド pip install flask でホームディレクトリの配下に .loacal/bin/flask

がインストールされるのでこれにPATHを通しておきます。

 

次にWebページアクセスされるDocument_Root環境下を以下のように準備します。

-rw----r--      hacca.jp-LolipopUser 207  1月 24 19:16 .bash_profile

-rw----r--      hacca.jp- LolipopUser 138  2月 13 19:22 .htaccess

-rw-------     hacca.jp-LolipopUser 263  2月 13 19:28 helloFlask.py

-rwx------     hacca.jp-LolipopUser 114  2月 13 19:24 index.cgi

drwx---r-x    hacca.jp-LolipopUser  33  2月 13 19:15 templates

        |templateディレクトリ配下に

         -rw----r--  LolipopUser 107  2月 13 19:14 render.html

                              を置く

 

.htaccess の中身ーーーーーーーーーーーーーーー

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ /ここはindex.cgiまでのPATH/index.cgi/$1 [QSA,L]

 

index.cgiの中身ーーーーーーーーーーーーーーーー

#!/usr/local/bin/python3

from wsgiref.handlers import CGIHandler

from helloFlask import app

CGIHandler().run(app)

 

helloFlask.pyの中身ーーーーーーーーーーーーーー

from flask import Flask , render_template

app = Flask(__name__)

 

@app.route('/')

def hello():

    return '<h1>Hello World</h1>'

 

@app.route('/render')

def index():

    return render_template('render.html')

 

if __name__ == '__main__':

    app.run(debug=True)

 

render.htmlの中身ーーーーーーーーーーーーーーー

<!DOCTYPE html>

<html>

    <head>

    </head>

    <body>

    <h1>render template</h1>

    </body>

</html>

 

 

gunicorn 起動コマンド

$ gunicorn -w 1 -b 0.0.0.0:8080 helloFlask:app

[2023-02-13 19:38:11 +0900] [224470] [INFO] Starting gunicorn 20.1.0

[2023-02-13 19:38:11 +0900] [224470] [INFO] Listening at: http://0.0.0.0:8080 (224470)

[2023-02-13 19:38:11 +0900] [224470] [INFO] Using worker: sync

[2023-02-13 19:38:11 +0900] [224481] [INFO] Booting worker with pid: 224481