失敗談CentOS7+Apache2.4+mod_swgi+python3をやってみたら・・・

Pythonの勉強を最近しております。nginxのリバースプロキシでpyファイルをApacheで対応するというのやっていたのですが、その中であれ?これ失敗ではないか?というのになったので記録として残しておきます。
構成
構成は以下の通りです
- CentOS7
- nginx
- Apache 2.4.6
- python 3.6
- python 2.7
Pythonが2つあるのはなぜ?
Python2.7はシステムのデフォルトで入っている物になります。Python3.6はCentOSのシステム全体にpyenvのインストールから実行までにあるとおりpyenvでインストールした物になります。とりあえずやっていきます。
失敗の記事なので、このやり方をすると失敗します(多分)
このやり方は失敗のやりかたです。この通りにやれば失敗することになると思います。
Apacheの設定
今回はApacheがインストールされた状態とします。
mod_wsgiのインストール
yumとpipの入れかたとありますが、今回はyumでやります
[c]
[macan@instance-1 html]$ sudo yum install mod_wsgi
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.jaist.ac.jp
* epel: mirrors.cat.pdx.edu
* extras: ftp.jaist.ac.jp
* remi-safe: ftp.riken.jp
* updates: ftp.jaist.ac.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ mod_wsgi.x86_64 0:3.4-12.el7_0 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
==============================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==============================================================================================================================================================================================================================================
インストール中:
mod_wsgi x86_64 3.4-12.el7_0 base 76 k
トランザクションの要約
==============================================================================================================================================================================================================================================
インストール 1 パッケージ
総ダウンロード容量: 76 k
インストール容量: 197 k
Is this ok [y/d/N]: y
Downloading packages:
mod_wsgi-3.4-12.el7_0.x86_64.rpm | 76 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : mod_wsgi-3.4-12.el7_0.x86_64 1/1
検証中 : mod_wsgi-3.4-12.el7_0.x86_64 1/1
インストール:
mod_wsgi.x86_64 0:3.4-12.el7_0
完了しました!
[macan@instance-1 html]$
[/c]
インストール完了しました
confファイルの作成
confファイルの作成をします
[c]
[macan@instance-1 html]$ sudo vi etc/httpd/conf.d/wsgi.conf
<Directory /var/www/html/>
Options ExecCGI MultiViews Indexes
MultiViewsMatch Handlers
AddHandler wsgi-script .py
AddHandler wsgi-script .wsgi
DirectoryIndex index.html index.py app.wsgi
Order allow,deny
Allow from all
</Directory>
[/c]
ただの設定です。Apacheを起動します
[c]
[macan@instance-1 html]$ sudo service httpd
[/c]
Apache起動しました
Pythonファイル作成
pythonファイルを作成します。今回はindex.pyで作成をします
[python]
#coding:utf-8
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return ['こんにちは世界!']
[/python]
これで完了です。ブラウザでhttp://ドメイン名/index.pyで見られると思います。
何が問題か?
普通に表示できても、実はmod_wsgiがPython2.7で動いている可能性があります。
テンプレートエンジンでエラーがでた
テンプレートエンジンを使ってみたらエラーがでました
[c]
[Sat Jul 28 20:52:32.310483 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] mod_wsgi (pid=9780): Target WSGI script '/var/www/html/index.py' cannot be loaded as Python module.
[Sat Jul 28 20:52:32.314064 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] mod_wsgi (pid=9780): Exception occurred processing WSGI script '/var/www/html/index.py'.
[Sat Jul 28 20:52:32.314097 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] Traceback (most recent call last):
[Sat Jul 28 20:52:32.314117 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] File "/var/www/html/index.py", line 6, in <module>
[Sat Jul 28 20:52:32.314187 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] tpl = env.get_template('hello.tpl.html')
[Sat Jul 28 20:52:32.314230 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] File "/usr/lib64/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
[Sat Jul 28 20:52:32.320533 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] return self._load_template(name, self.make_globals(globals))
[Sat Jul 28 20:52:32.320558 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] File "/usr/lib64/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
[Sat Jul 28 20:52:32.320582 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] template = self.loader.load(self, name, globals)
[Sat Jul 28 20:52:32.320589 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] File "/usr/lib64/python2.7/site-packages/jinja2/loaders.py", line 113, in load
[Sat Jul 28 20:52:32.321279 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] source, filename, uptodate = self.get_source(environment, name)
[Sat Jul 28 20:52:32.321295 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] File "/usr/lib64/python2.7/site-packages/jinja2/loaders.py", line 187, in get_source
[Sat Jul 28 20:52:32.321338 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] raise TemplateNotFound(template)
[Sat Jul 28 20:52:32.322101 2018] [:error] [pid 9780:tid 140025171924736] [client 127.0.0.1:60222] TemplateNotFound: hello.tpl.html
[/c]
これはPythonのテンプレートエンジンJinja2をインストールして設定したのにエラーがでたため、ログをみてみました。/usr/lib64/python2.7/site-packages/と2.7とでてます。
pipでインストールし直す
yum は消して今度はpipでインストールしてみます
[c]
[macan@instance-1 html]$ sudo pip install mod-wsgi
[macan@instance-1 html]$ sudo pip show mod-wsgi
Name: mod-wsgi
Version: 4.6.4
Summary: Installer for Apache/mod_wsgi.
Home-page: http://www.modwsgi.org/
Author: Graham Dumpleton
Author-email: Graham.Dumpleton@gmail.com
License: Apache License, Version 2.0
Location: /usr/lib64/python2.7/site-packages
Requires:
Required-by:
[macan@instance-1 html]$
[/c]
pipでインストールしてみても、やはり2.7に紐付いてます・・・
mod_wsgiとPythonを3に紐づけないといけないのではないか?
もしかしたら、mod_wsgiとpython3が紐付いていないのかも知れません。。これが原因で動かないのか・・・
余談
やはり2に紐付いてコードは動いてました。3にすると、日本語だと以下のエラーが起きます
[c]
[Sun Jul 29 11:02:26.199993 2018] [wsgi:error] [pid 14129:tid 139823311849216] [client 127.0.0.1:43416] mod_wsgi (pid=14129): Exception occurred processing WSGI script '/var/www/html/index.py'.
[Sun Jul 29 11:02:26.200106 2018] [wsgi:error] [pid 14129:tid 139823311849216] [client 127.0.0.1:43416] TypeError: sequence of byte string values expected, value of type str found
[/c]
恐らくpython3+wsgiなどのサイトで紹介されているとこのいくつかは2.7に紐付いている可能性もあります。
追記
英語だと問題無く表示されました。Python3+mod_wsgiが上手く紐付いているかもしれません。ただ、、、日本語にするとエラーになりました
個人支援・寄付について
サイトラボでは個人支援・寄付を受けております。ご協力いただける方はお願いいたします。当サイトではビットコインで受け付けております。
- ビットコイン:3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS