Flaskをlolipop上で動かす(スタンダード契約)

  • 2025-09-23
  • 2025-09-23
  • web
  • 3view
  • 0件

参考にさせていただいたサイト

ロリポップサーバーにPythonのpipを導入する流れ

https://creepfablic.site/2019/06/10/lolipop-pipcommand/

CGI、SSI、PHP、SENDMAILについて

https://lolipop.jp/manual/hp/cgi/

lolipopの状況を確認(私の場合)

  • 契約はスタンダード
  • 割り当てられたサーバは「std009」 ※sshで接続したときのパスに含まれる値で確認できる

はじめに

  • lolipop上にflaskの実行環境が構築できるのか試した
  • CGI経由で実行
  • lolipopのマネージドクラウドではFlaskのやり方が書いてあるが参考になるかはわからない(スタンダード契約とは状況は異なると思う)
  • 私はwindows10で作業を実施している
  • 各種コマンドの実行はSSHで接続して実施した(PuTTY-ranvis)
  • Flaskのファイルの設置とパーミッションの設定はWinSCPで実施した
  • テキストエディターはサクラエディタを使用した

Flaskの実行環境整備

初期状態で入っているpythonのバージョンを調べる

[pepper.jp-xxxxxxxx@std009 ~]$ python --version
Python 2.7.5
[pepper.jp-xxxxxxxx@std009 ~]$ python3 --version
Python 3.7.13

pipのインストール

apt-get install でシステムにpipを入れることができないため、自分のユーザー領域にpipを入れることで対応する
flaskやdjangoもユーザー領域にインストールすることで実行することができる

pipインストール用のスクリプトをダウンロード

[pepper.jp-xxxxxxxx@std009 ~]$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2596k  100 2596k    0     0  2247k      0  0:00:01  0:00:01 --:--:-- 2248k

pipをインストール

[pepper.jp-xxxxxxxx@std009 ~]$ python3 get-pip.py --user
Collecting pip
  Downloading pip-22.0.4-py3-none-any.whl (2.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 8.7 MB/s eta 0:00:00
Collecting wheel
  Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel, pip
  WARNING: The script wheel is installed in '/home/users/2/pepper.jp-xxxxxxxx/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts pip, pip3 and pip3.7 are installed in '/home/users/2/pepper.jp-xxxxxxxx/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.0.4 wheel-0.37.1

pipのPATHを通す

▲通す前の確認
[pepper.jp-xxxxxxxx@std009 ~]$ echo $PATH
/usr/local/bin:/usr/bin

▲PATHに追加するため.bash_profileを作成
[pepper.jp-xxxxxxxx@std009 ~]$ vi .bash_profile
	PATH=$PATH:/home/users/2/pepper.jp-xxxxxxxx/.local/bin
	export PATH

▲.bash_profileを再実行して現在のシェルに反映させる
[pepper.jp-xxxxxxxx@std009 ~]$ source .bash_profile

▲PATHに追加されたか確認
[pepper.jp-xxxxxxxx@std009 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/home/users/2/pepper.jp-xxxxxxxx/.local/bin

pipが使えるようになったか確認

[pepper.jp-xxxxxxxx@std009 ~]$ pip3 list
Package                Version
---------------------- -------
mysql-connector-python 8.0.28
mysqlclient            2.1.0
pip                    22.0.4
protobuf               3.19.4
PyMySQL                1.0.2
setuptools             47.1.0
wheel                  0.37.1

flaskを入れる(入れ方が2通りあったが、たぶん1つ目で大丈夫なはず)

インストール

▲1つ目
[pepper.jp-xxxxxxxx@std009 ~]$ pip3 install flask
Defaulting to user installation because normal site-packages is not writeable
Collecting flask
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.6/95.6 KB 28.0 MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 KB 33.3 MB/s eta 0:00:00
Collecting click>=7.1.2
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 KB 25.5 MB/s eta 0:00:00
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 289.2/289.2 KB 57.1 MB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.1-py3-none-any.whl (15 kB)
Collecting importlib-metadata
  Downloading importlib_metadata-4.11.3-py3-none-any.whl (18 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting zipp>=0.5
  Downloading zipp-3.7.0-py3-none-any.whl (5.3 kB)
Collecting typing-extensions>=3.6.4
  Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Installing collected packages: zipp, Werkzeug, typing-extensions, MarkupSafe, itsdangerous, Jinja2, importlib-metadata, click, flask
Successfully installed Jinja2-3.0.3 MarkupSafe-2.1.1 Werkzeug-2.0.3 click-8.0.4 flask-2.0.3 importlib-metadata-4.11.3 itsdangerous-2.1.1 typing-extensions-4.1.1 zipp-3.7.0

▲2つ目
[pepper.jp-xxxxxxxx@std009 ~]$ python3 -m pip install flask --user
Requirement already satisfied: flask in ./.local/lib/python3.7/site-packages (2.0.3)
Requirement already satisfied: click>=7.1.2 in ./.local/lib/python3.7/site-packages (from flask) (8.0.4)
Requirement already satisfied: Jinja2>=3.0 in ./.local/lib/python3.7/site-packages (from flask) (3.0.3)
Requirement already satisfied: itsdangerous>=2.0 in ./.local/lib/python3.7/site-packages (from flask) (2.1.1)
Requirement already satisfied: Werkzeug>=2.0 in ./.local/lib/python3.7/site-packages (from flask) (2.0.3)
Requirement already satisfied: importlib-metadata in ./.local/lib/python3.7/site-packages (from click>=7.1.2->flask) (4.11.3)
Requirement already satisfied: MarkupSafe>=2.0 in ./.local/lib/python3.7/site-packages (from Jinja2>=3.0->flask) (2.1.1)
Requirement already satisfied: typing-extensions>=3.6.4 in ./.local/lib/python3.7/site-packages (from importlib-metadata->click>=7.1.2->flask) (4.1.1)
Requirement already satisfied: zipp>=0.5 in ./.local/lib/python3.7/site-packages (from importlib-metadata->click>=7.1.2->flask) (3.7.0)

flaskが入ったか確認

[pepper.jp-xxxxxxxx@std009 ~]$ pip3 list
Package                Version
---------------------- -------
click                  8.0.4
Flask                  2.0.3
importlib-metadata     4.11.3
itsdangerous           2.1.1
Jinja2                 3.0.3
MarkupSafe             2.1.1
mysql-connector-python 8.0.28
mysqlclient            2.1.0
pip                    22.0.4
protobuf               3.19.4
PyMySQL                1.0.2
setuptools             47.1.0
typing_extensions      4.1.1
Werkzeug               2.0.3
wheel                  0.37.1
zipp                   3.7.0

flaskの情報を確認

[pepper.jp-xxxxxxxx@std009 ~]$ pip3 show Flask
Name: Flask
Version: 2.0.3
Summary: A simple framework for building complex web applications.
Home-page: https://palletsprojects.com/p/flask
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: /home/users/2/pepper.jp-xxxxxxxx/.local/lib/python3.7/site-packages
Requires: click, itsdangerous, Jinja2, Werkzeug
Required-by:

.local/lib/python3.7/site-packages に入っていることが確認できる

flaskのファイルを設置

フォルダ構成

左からファイル名、パーミッション、文字コード、改行コード
/home/users/2/pepper.jp-xxxxxxxx/web/yyyy/flask-test
  index.cgi      0700    utf-8   LF
  helloFlask.py  0600    utf-8   LF
  .htaccess      0604    utf-8   CRLF
  templates      0705
    render.html  0604    utf-8   CRLF

これから作成するwebアプリ用のフォルダと作成するファイルです
winscpやFTPなどで自分のフォルダに設置してください

index.cgi (0700 utf-8 LF)

#!/usr/local/bin/python3		#!/usr/local/bin/python3.7でもいけた
from wsgiref.handlers import CGIHandler
from helloFlask import app
CGIHandler().run(app)

helloFlask.py (0600 utf-8 LF)

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)

.htaccess (0604 utf-8 CRLF)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /yyyy/flask-test/index.cgi/$1 [QSA,L]

templatesフォルダ (0705)

render.html (0604 utf-8 CRLF)

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
    <h1>render template</h1>
    </body>
</html>

実行アドレスと結果

ブラウザを立ち上げてurlを打ち込んで動作確認してください

https://xxxxxxxx.pepper.jp/yyyy/flask-test/
https://xxxxxxxx.pepper.jp/yyyy/flask-test/index.cgi
	Hello World こんにちは!!!!!!!!!!!!!

https://xxxxxxxx.pepper.jp/yyyy/flask-test/render
	render template

https://xxxxxxxx.pepper.jp/yyyy/flask-test/helloFlask.py
	550エラー

実行エラーになる場合に確認するところ

  • ファイルの文字コードと改行コードを確認
  • ファイルやフォルダのパーミッションを確認
  • flaskが入っているか確認