☆☆ 新着記事 ☆☆

2018年12月28日金曜日

複数のサブドメインを管理しているサーバで、wsgiを使う時のVirtual Hostの書き方

Pythonのフレーム・ワークであるDjangoやFlaskを動かすときには、wsgiを使うことが推奨されています。(各アプリで、同時処理を可能にするため。)

ところで、このwsgi (apacheを使っているので、mod_wsgi) を、複数のサブドメインを管理しているサーバーで使う時には、Virtual Hostの書き方に工夫が必要なようです。ネットで検索しても、ピタッとくる回答がないので、一つずつ書き方を検証していきます。

検証環境:Apache 2.4  + wsgi



(Testの構成)
* ドメイン・サブドメイン
 1)           mossymob.tk
   2) www.mossymob.tk
   3) uslife.mossymob.tk

(Global IP アドレス 1つで運用しまています。外部DNSは設定済み、とします。)



1.基本: mossymob.tk と www.mossymob.tk をaliasで管理。

mossymob.tk と www.mossymob.tkを動かすvirtualhost.confの記述。

[root@cnewgwb6 conf.d]# pwd
/etc/httpd/conf.d
[root@cnewgwb6 conf.d]# ls -a
.   autoindex.conf  README    userdir.conf      welcome.conf
..  manual.conf     ssl.conf  virtualhost.conf

LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so

<VirtualHost *:80>
  ServerName mossymob.tk
  ServerAlias www.mossymob.tk
  DocumentRoot /var/www/html
  WSGIScriptAlias / /var/www/html/myapp.wsgi
  <Directory "/var/www/html/">
    Options Includes ExecCGI FollowSymLinks
  </Directory>
</VirtualHost>

wwwつきも、mossymob.tk と同じ Document Root 、同じDirectory コンテナの内容なので、
結局、1つだけのドメインを管理しているのと同じですから、うまく機能します。

WSGIScriptには、以下の内容が書いてあります。
# coding: utf-8
import sys
sys.path.insert(0, '/var/www/html')
from app import app as application

**この段階で、wsgiを使わないシンプルな、staticのhtmlファイルが、wsgiファイルが設定されているフォルダに同居していると、機能するか検証します。

[root@cnewgwb6 html]# pwd
/var/www/html

[root@cnewgwb6 html]# ls -l
-rwxr--r-x 1 root root 173 Dec 28 13:22 app.py
-rwxr-xr-x 1 root root 206 Dec 28 17:37 hello.html
-rw-r--r-- 1 root root 103 Dec 28 12:53 myapp.wsgi

この場合、app.pyは動くが、hello.htmlはHTTP 404エラーになる。


では、htmlの配下に、更に、blog ディレクトリをつくって、そこに格納したら動くか?

[root@cnewgwb6 blog]# pwd
/var/www/html/blog
[root@cnewgwb6 blog]# ll
-rw-r--r-- 1 root root 206 Dec 28 17:53 hello.html

答えは
HTTP 404エラー

こういった場合、

<VirtualHost *:80>
   ServerName mossymob.tk
   ServerAlias www.mossymob.tk
   DocumentRoot /var/www/html
   Alias /blog  /var/www/html/blog
   WSGIScriptAlias / /var/www/html/myapp.wsgi
 (以下、省略)

と、赤の網掛けをしたAlias を記述することで、

wsgiを経由しないで、直接アクセスしたいstaticなファイルを指定

できるようです。




参考)mod_wsgi : Quick Configuration Guide
https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

Mounting At Root Of Site
If instead you want to mount a WSGI application at the root of a site, simply list ‘/’ as the mount point when configuring the WSGIScriptAlias directive:

WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi

Do note however that doing so will mean that any static files contained in the DocumentRoot will be hidden and requests against URLs pertaining to the static files will instead be processed by the WSGI application.
In this situation it becomes necessary to remap using the Alias directive, any URLs for static files to the directory containing them:

Alias /robots.txt /usr/local/www/documents/robots.txt
Alias /favicon.ico /usr/local/www/documents/favicon.ico
Alias /media/ /usr/local/www/documents/media/


2.2つ目のサブドメイン(uslife.mossymob.tk)をVirtual Hostに追加

さて、本題の、別のサブドメインのwsgi経由のflaskのアプリケーションや静的ファイルを、virtual hostで、どう管理するか。

上記のVirtual Hostの記述に、単純に以下のVirtualHostを追加すると、エラーになってhttpdが起動できなくなる。

<VirtualHost *:80>
     ServerName uslife.mossymob.tk
     DirectoryIndex index.html
     AddDefaultCharset UTF-8
     DocumentRoot /var/uslife/html/
     <Directory "/var/uslife/html/">
      AllowOverride All
      Options FollowSymLinks -Indexes
     </Directory>
 </VirtualHost>

[root@cnewgwb6 conf.d]# systemctl reload httpd
Job for httpd.service failed because the control process exited with error code.

-- A session with the ID 9 has been terminated.
Dec 28 16:44:39 cnewgwb6 polkitd[619]: Registered Authentication Agent for unix-process:13891:2146407 (system bus name :1.87
Dec 28 16:44:39 cnewgwb6 httpd[13898]: httpd: Syntax error on line 360 of /etc/httpd/conf/httpd.conf: Syntax error on line 1
Dec 28 16:44:39 cnewgwb6 systemd[1]: httpd.service: control process exited, code=exited status=1
Dec 28 16:44:39 cnewgwb6 systemd[1]: Reload failed for The Apache HTTP Server.

というエラー。

こういう場合は、デーモン・プロセスを指定するそうなので、下記がその内容。


LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
 <VirtualHost *:80>
   ServerName mossymob.tk
   ServerAlias www.mossymob.tk
   WSGIDaemonProcess www user=apache group=apache

   DocumentRoot /var/www/html
   Alias /blog  /var/www/html/blog
   WSGIScriptAlias / /var/www/html/myapp.wsgi process-group=www
   <Directory "/var/www/html/">
     Options Includes ExecCGI FollowSymLinks
   </Directory>
 </VirtualHost>

  <VirtualHost *:80>
   ServerName uslife.mossymob.tk
   WSGIDaemonProcess uslife user=apache group=apache

   DocumentRoot /var/uslife/html
   Alias /blog  /var/uslife/html/blog
   WSGIScriptAlias / /var/uslife/html/myapp.wsgi process-group=uslife
   <Directory "/var/uslife/html/">
     Options Includes ExecCGI FollowSymLinks
   Require all granted   ## 注意
   </Directory>
 </VirtualHost>
 

 ##注意)
 'Require all granted’ の記述がないと、以下のようなエラーになります
 [authz_core:error] [pid 23111] [client 175.132.xxxx.xxxx] AHxxxx: client denied by server configuration: /var/uslife/html/blog/hello.html
 flask.appも、blog配下のhtmlも読み込めません。 この記述を付け足す事で、どちらも表示できるように  なります。
  全く同一構成の、mossymob.tk / www.mossymob.tk のVirtual Hostの<Directory>コンテナーの中には、この記述は不要です。 (というか、なくても問題なく表示されている。)

 どうしてそうなるのか、今のところ、自分には理由は分かりません。
 (疲れたので、調べてもいません。)

 無事に、別のサブドメインの内容が、Virtual Hostの記述で表示できるようになったので、
 良し、とします。

 
 
 
おしまいです。

0 件のコメント:

コメントを投稿