2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Target WSGI script '/var/www/xxx/xxx.wsgi' cannot be loaded as python module というエラーが発生した場合の対処方法

Posted at

Pythonを使った簡単なアプリケーションを作成&公開する場合、WebサーバはApache、Webアプリケーションフレームワークはflaskという組み合わせはよくあると思います。
そして、Apacheとflaskを繋ぐWSGI(Web Server Gateway Interface)はmod_wsgiを使うことになります。

AWSでEC2のインスタンスを立て、Apacheをインストールし、flaskで書いたアプリを動かすぞ!mod_wsgiもインストールしたから問題なく動くはず!と思っていたのに、次のようなエラーが発生することがあります。

mod_wsgi (pid=8711): Target WSGI script '/var/www/xxx/xxx.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=8711): Exception occurred processing WSGI script '/var/www/xxx/xxx.wsgi'.

あれ、mod_wsgiをインストールしたのに何で?となりますが、結論から言うとyum install mod_wsgiでインストールしたmod_wsgiはバージョンが古くPython2系にしか対応していないためです(Cent OS系のOSの場合)。

ですので、yum install mod_wsgiでmod_wsgiをインストールした場合は、yum remove mod_wsgiでアンインストールする必要があります。その上で、pip install mod_wsgiのようにpip経由でインストールすればPython3系に対応したmod_wsgiをインストールできます。

例えば、venvを使ってPython3.7の仮想環境を作成している場合、上記のpipコマンドを実行すると

$VENVHOME/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

という場所にmod_wsgiの共有ライブラリファイルを作成されます。そして、Apacheのconfファイルに次のように記述すると、Python3に対応したmod_wsgiが実行されます。

LoadModule wsgi_module $VENVHOME/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

めでたし、めでたし。それにしても、Pythonのバージョンに関しては色々な所に落とし穴がありますね…。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?