0
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】ディレクトリ作成プロンプトから

Posted at

ステップ コマンド
ファイル作成 cat > index.cgi
内容貼り付け Flaskコードを入力し、Ctrl + D で保存
実行権限付与 chmod 755 index.cgi

index.cgi を作成する

touch index.cgi

cat > index.cgi

#! /home/{}/miniconda3/envs/py39/bin/python
from wsgiref.handlers import CGIHandler
from app import app
CGIHandler().run(app)
chmod 755 index.cgi

cat > app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

chmod 600 app.py

ファイル 内容 パーミッション
app.py Flaskアプリ本体 600(または 644)
index.cgi CGI起動スクリプト 755

🛠️ .htaccess の基本的な内容(CGI用)
以下のように書けば、index.cgi を正しく動かすための設定になります:

Options +ExecCGI


AddHandler cgi-script .cgi



DirectoryIndex index.cgi

🔍 各行の意味
設定 説明
Options +ExecCGI このディレクトリで CGI を実行可能にする
AddHandler cgi-script .cgi .cgi 拡張子のファイルを CGI スクリプトとして扱う
DirectoryIndex index.cgi デフォルトで表示するファイルを index.cgi にする

cat > .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

# HTTP → HTTPS リダイレクト
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# すべてのリクエストを index.cgi に渡す
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.cgi/$1 [L]
</IfModule>


chmod 644 .htaccess

🎯 まとめ
設置場所 RewriteRule の書き方
/app4/ ディレクトリ内 index.cgi/$1 ← ✅ 変更すべき
ルートディレクトリ /app4/index.cgi/$1 ← そのままでOK

項目 状態
app.py に Flask アプリあり ✅ OK
index.cgi に CGIHandler 記述あり ✅ OK
.htaccess に RewriteRule(相対パス) ✅ OK
Conda環境 py39 に Flask インストール済み ✅ OK
index.cgi の shebang が py39 の Python パス ✅ OK

0
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
0
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?