質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Flask

FlaskはPython用のマイクロフレームワークであり、Werkzeug・Jinja 2・good intentionsをベースにしています。

Apache

Apacheは、Apache HTTP Serverの略で、最も人気の高いWebサーバソフトウェアの一つです。安定性が高いオープンソースソフトウェアとして商用サイトから自宅サーバまで、多くのプラットフォーム向けに開発・配布されています。サーバーソフトウェアの不具合(NCSA httpd)を修正するパッチ(a patch)を集積、一つ独立したソフトウェアとして開発されました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1189閲覧

Apache上でFlaskアプリケーションを連携させようとするとHTTP 500エラーとなる

Natsuki902

総合スコア13

Flask

FlaskはPython用のマイクロフレームワークであり、Werkzeug・Jinja 2・good intentionsをベースにしています。

Apache

Apacheは、Apache HTTP Serverの略で、最も人気の高いWebサーバソフトウェアの一つです。安定性が高いオープンソースソフトウェアとして商用サイトから自宅サーバまで、多くのプラットフォーム向けに開発・配布されています。サーバーソフトウェアの不具合(NCSA httpd)を修正するパッチ(a patch)を集積、一つ独立したソフトウェアとして開発されました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2023/04/01 14:42

編集2023/04/01 14:47

実現したいこと

Apache上でFlaskアプリケーションを動作させるようにしたい。

  • http: //localhost/FlaskApp1/ というURLでアプリケーションを公開したい。

※自動ハイパーリンクにならないよう意図的に半角スペースを入れています。

前提

Pythonの勉強を初めて、Flaskアプリケーションというものを知りまして、app.runが出来るようになった
(ポート番号を指定しての単品アプリ起動が出来るようになった程度)ため、Apacheと連動させてみようと
試行錯誤を始めた状態です。

発生している問題・エラーメッセージ

[Sat Apr 01 23:18:58.424884 2023] [core:warn] [pid 11296:tid 408] AH00098: pid file C:/Apache24/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run? [Sat Apr 01 23:18:58.427033 2023] [mpm_winnt:notice] [pid 11296:tid 408] AH00455: Apache/2.4.56 (Win64) mod_wsgi/4.9.2 Python/3.11 configured -- resuming normal operations [Sat Apr 01 23:18:58.427033 2023] [mpm_winnt:notice] [pid 11296:tid 408] AH00456: Apache Lounge VS17 Server built: Mar 6 2023 10:53:37 [Sat Apr 01 23:18:58.427033 2023] [core:notice] [pid 11296:tid 408] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24' [Sat Apr 01 23:18:58.521944 2023] [mpm_winnt:notice] [pid 11296:tid 408] AH00418: Parent: Created child process 2456 [Sat Apr 01 23:18:59.913796 2023] [mpm_winnt:notice] [pid 2456:tid 412] AH00354: Child: Starting 64 worker threads. [Sat Apr 01 23:19:00.447126 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] mod_wsgi (pid=2456): Failed to exec Python script file 'C:/Apache24/flaskapps/yourapplication.wsgi'. [Sat Apr 01 23:19:00.447126 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] mod_wsgi (pid=2456): Exception occurred processing WSGI script 'C:/Apache24/flaskapps/yourapplication.wsgi'. [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] Traceback (most recent call last):\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:/Apache24/flaskapps/yourapplication.wsgi", line 3, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] from app import app as application\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Apache24/flaskapps\\app.py", line 1, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] from flask import Flask\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\flask\\__init__.py", line 4, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] from . import json as json\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\flask\\json\\__init__.py", line 8, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] from ..globals import current_app\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\flask\\globals.py", line 4, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] from werkzeug.local import LocalProxy\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\werkzeug\\__init__.py", line 1, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] from .serving import run_simple as run_simple\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\werkzeug\\serving.py", line 17, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] import socket\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] File "C:\\Program Files\\Python311\\Lib\\socket.py", line 51, in <module>\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] import _socket\r [Sat Apr 01 23:19:00.458004 2023] [wsgi:error] [pid 2456:tid 1340] [client ::1:51904] ModuleNotFoundError: No module named '_socket'\r [Sat Apr 01 23:19:00.972086 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] mod_wsgi (pid=2456): Failed to exec Python script file 'C:/Apache24/flaskapps/yourapplication.wsgi'., referer: http://localhost/ [Sat Apr 01 23:19:00.972086 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] mod_wsgi (pid=2456): Exception occurred processing WSGI script 'C:/Apache24/flaskapps/yourapplication.wsgi'., referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] Traceback (most recent call last):\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:/Apache24/flaskapps/yourapplication.wsgi", line 3, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] from app import app as application\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Apache24/flaskapps\\app.py", line 1, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] from flask import Flask\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\flask\\__init__.py", line 4, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] from . import json as json\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\flask\\json\\__init__.py", line 8, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] from ..globals import current_app\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\flask\\globals.py", line 4, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] from werkzeug.local import LocalProxy\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\werkzeug\\__init__.py", line 1, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] from .serving import run_simple as run_simple\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Users\\PCuser\\AppData\\Roaming\\Python\\Python311\\site-packages\\werkzeug\\serving.py", line 17, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] import socket\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] File "C:\\Program Files\\Python311\\Lib\\socket.py", line 51, in <module>\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] import _socket\r, referer: http://localhost/ [Sat Apr 01 23:19:00.973273 2023] [wsgi:error] [pid 2456:tid 1336] [client ::1:51905] ModuleNotFoundError: No module named '_socket'\r, referer: http://localhost/

試したこと

まずは、調べてみた情報をそのままなぞって環境構築が正しく出来ていることを確認しようとしたところ、
Apacheを起動して、「http: //localhost/」へブラウザからアクセスしたら、上記エラーログがApacheのエラーログに
出力しています。

Pythonスクリプトの内容並びに手順は以下のURLの内容をそのままです。
https://qiita.com/NAKA_G/items/f34738df364af8cbd58e
※但し、Apacheのサービスインストールのみ行っていません。

経験が十分ではなく、試すべきことに気づいていない可能性もありますため、「こういうことを試した方がよい」などありましたら、
ご教示いただけますと幸いです。

補足情報(FW/ツールのバージョンなど)

Python:Python3.11
pipにてFlaskはじめ、単品での起動に必要な外部ライブラリは導入済み(単品起動後ブラウザから想定動作できることを確認済み)
Apache:httpd-2.4.56-win64-VS17

Flaskアプリ資材の置き場所
c:\Apache配下に「flaskapps」フォルダを作成し、上記URLで作成した「app.py」及び「wsgiファイル」を中に格納しています。

application.wsgi

1import sys 2sys.path.insert(0, 'C:/Apache24/flaskapps') 3from app import app as application

httpd.conf(追記部分)

1LoadFile "C:/Program Files/Python311/python311.dll" 2LoadModule wsgi_module "C:/Users/PCuser/AppData/Roaming/Python/Python311/site-packages/mod_wsgi/server/mod_wsgi.cp311-win_amd64.pyd" 3WSGIPythonHome "C:/Program Files/Python311" 4 5<VirtualHost *:80> 6 WSGIScriptAlias / "C:/Apache24/flaskapps/application.wsgi" 7 <Directory "C:/Apache24/flaskapps"> 8 Require all granted 9 </Directory> 10</VirtualHost> 11

app.py

1from flask import Flask 2 3# Flaskをappという変数で動かす宣言 4app = Flask(__name__) 5 6@app.route('/') 7def hello(): 8 name = "Flask Hello World" 9 return name 10 11# appの実行 12if __name__ == "__main__": 13 app.run()

単品で動作させた場合のログは以下の通りです。

* Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit 127.0.0.1 - - [01/Apr/2023 23:39:41] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [01/Apr/2023 23:39:41] "GET /favicon.ico HTTP/1.1" 404 -

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

このエラーは、FlaskアプリケーションがApache ●●●● で正常に実行できないことを示しています。これは、mod_wsgiモジュールのPythonインタプリタが間違っている、またはWSGIアプリケーションが正しく構成されていないことが原因である可能性があります。

解決するためには、次の手順を実行してください。

Apacheのエラーログを確認して、詳細なエラーメッセージを取得します。

Flaskアプリケーションが正しく動作することを確認してください。Pythonの仮想環境にFlaskをインストールし、アプリケーションを実行できることを確認してください。

FlaskアプリケーションをWSGIアプリケーションに変換し、Apacheで実行できるようにします。WSGIアプリケーションは、●●●● WSGIプロトコルに準拠している必要があります。

Apacheの設定ファイルを編集し、WSGIアプリケーションを設定します。この設定ファイルは、Apacheのhttpd.confファイルや、Apacheのサイト設定ファイル(.conf拡張子)になります。

Apacheを再起動して、設定変更を反映させます。

以上の手順を実行することで、Apache上でFlaskアプリケーションを正常に動作させることができます。また、問題が解決しない場合は、より詳細な情報を提供してください。

投稿2023/04/24 05:00

lunadam

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Natsuki902

2023/05/04 14:34

ご回答ありがとうございます。 本業の都合で確認が遅くなりました。 基礎部分の知識が絶対的に足りないようで、先述のログ以上に詳細な情報の取得が叶いませんでした。 「Apache backrooms game」という用語も解釈できず、、、 一旦今回はApahceのProxy機能を利用して対応し、WSGIアプリケーションとしての連携はToDoにして対応しようと思います。 (出来ている方もいるかつ、その方とはPythonのバージョンが異なる(記事のは古い)ので、 なにかしら読み替えないといけないものが読み替えられていない、読み取れていないor 記載されていない必須条件があるなどではないかなと推察しております。) '''httpd.confに以下を追記 RedirectMatch ^/dir$ http://127.0.0.1/dir/ ProxyPass /dir/ http://127.0.0.1:8081/ ProxyPassReverse /dir/ http://127.0.0.1:8081/ '''httpd.confに以下を追記(ここまで) その上で、作成したapp.pyをダブルクリックで起動し、ポート8081をlisten開始してからアクセス。 ※その上で、htmlから呼び出すcss/jsなどは上記URLでマッチする場所に置くか、記載に考慮を入れて対応。 →この内容で「別途作成していたFlaskアプリケーションのトップページ表示と、そこからの画面遷移1パターンが成功すること」を確認しました。 折角ご協力頂いたのにもかかわらず、別案(緩和策?)での対応としてしまい、申し訳ございません。 もっと基礎知識をつけた上で近い将来にリトライしようと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問