1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ロリポップレンタルサーバ+Flask+CGIでWebアプリ起動

1
Last updated at Posted at 2025-07-17

概要

  • Flaskライブラリで作成したWebアプリを「ロリポップ!レンタルサーバー」で
    稼働させる

フォルダ構成

.
├── [-rw----r--]  .htaccess
├── [-rw-------]  app.py
├── [-rwx------]  index.cgi
├── [drwx---r-x]  static
│    └── [-rw----r--]  favicon.ico
└── [drwx---r-x]  templates
     └── [-rw----r--]  index.html
  • 上記のパーミッションについて

Pythonコード(Flask)

app.py
from os.path import join
from flask import Flask, render_template, send_from_directory

app = Flask(__name__)


@app.route('/favicon.ico')
def favicon():
    # ./static/favicon.icoを送信
    return send_from_directory(join(app.root_path, 'static'), 'favicon.ico')


@app.route("/")
def index():
    # ./templates/index.htmlを送信
    return render_template('index.html')


if __name__ == "__main__":
    app.run(threaded=True)

CGIコード

index.cgi
#!/usr/local/bin/python3

from wsgiref.handlers import CGIHandler
# index.cgiファイルと同階層にあるapp.pyファイルを呼び出し
from app import app
# Flaskアプリ実行
CGIHandler().run(app)

.htaccessコード(Apacheサーバ向け)

.htaccess
RewriteEngine On

# HTTPからHTTPSへの強制リダイレクト
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# index.cgiを利用したリクエスト処理
# ・クライアントからのリクエスト(https://<ドメイン名>/<コンテキストパス>)を
#   ロリポップのApacheサーバが受けた際、常に「/index.cgi/<コンテキストパス>」の
#   実行を行うことでFlaskで作られたWebアプリを使って、クライアントにレスポンスを返す
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.cgi/$1 [QSA,L]

参考ページ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?