Top / etch / park2 / 10_apache2

test/sarge/apache2

etch用のapache2

etchのapache2はdebian的流儀になっているconfigなので、一見わかりにくいので整理しておく。

インストール

# apt-get install apache2-mpm-prefork
Reading package lists... Done
Building dependency tree... Done
The following extra packages will be installed:
  apache2-common apache2-utils libapr0 libexpat1 libpcre3 mime-support
Suggested packages:
  apache2-doc lynx www-browser
The following NEW packages will be installed:
  apache2-common apache2-mpm-prefork apache2-utils libapr0 libexpat1 libpcre3
  mime-support
0 upgraded, 7 newly installed, 0 to remove and 4 not upgraded.
Need to get 1505kB of archives.
After unpacking 4903kB of additional disk space will be used.

w3mのインストール

テスト用にw3mをインストールする。

# apt-get install w3m
Reading package lists... Done
Building dependency tree... Done
The following extra packages will be installed:
  libgc1c2
Suggested packages:
  w3m-img menu w3m-el migemo
The following NEW packages will be installed:
  libgc1c2 w3m
0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
Need to get 1218kB of archives.
After unpacking 2085kB of additional disk space will be used.

設定

そのままの状態ではIPアドレス指定でアクセスすると、/apache2-default/が見える状態で/var/www/が表示される。

telnetで確認する場合には、下記のようになる。

# telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
HOST: etch.local.kensuke.jp

HTTP/1.1 200 OK
Date: Mon, 09 Oct 2006 13:19:25 GMT
Server: Apache/2.0.55 (Debian) mod_ssl/2.0.55 OpenSSL/0.9.8c
Content-Length: 599
Content-Type: text/html; charset=UTF-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
<pre><img src="/icons/blank.gif" alt="Icon "> <a href="?C=N;O=D">Name</a>                    
<a href="?C=M;O=A">Last modified</a>      <a href="?C=S;O=A">Size</a>  
<a href="?C=D;O=A">Description</a><hr><img src="/icons/folder.gif" alt="[DIR]">  
<a href="apache2-default/">apache2-default/</a>        06-Aug-2006 06:26    -
<hr></pre>
<address>Apache/2.0.55 (Debian) mod_ssl/2.0.55 OpenSSL/0.9.8c Server at 
etch.local.kensuke.jp Port 80</address>
</body></html>

このままだと、どんなコンテンツの文字コードもUTF-8に強制的に指定されてしまうために、一部設定を変更する。

SSL化

まずは、テスト用に簡単に自己署名証明書を作成する。

# /usr/sbin/apache2-ssl-certificate

make-cert-sslで作成したものを下記のディレクトリにcopyします。

/etc/apache2/ssl/apache.pem

次に下記のようにdefaultの設定ファイルをcopyしたのちに、設定ファイルを変更します。

# cd /etc/apache2/sites-available
# cp default ssl
# vi ssl
# cat /etc/apache2/sites-available/ssl
NameVirtualHost *:443
<VirtualHost *:443>
       SSLEngine On
       SSLCertificateFile /etc/apache2/ssl/apache.pem
       ServerAdmin webmaster@localhost

       DocumentRoot /var/www
       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>
       <Directory /var/www/>
               Options Indexes FollowSymLinks MultiViews
               AllowOverride None
               Order allow,deny
               allow from all
               # Uncomment this directive is you want to see apache2's
               # default start page (in /apache2-default) when you go to /
               #RedirectMatch ^/$ /apache2-default/
       </Directory>

       ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
       <Directory "/usr/lib/cgi-bin">
               AllowOverride None
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all
       </Directory>

       ErrorLog /var/log/apache2/error.log

       # Possible values include: debug, info, notice, warn, error, crit,
       # alert, emerg.
       LogLevel warn

       CustomLog /var/log/apache2/access.log combined
       ServerSignature On

   Alias /doc/ "/usr/share/doc/"
   <Directory "/usr/share/doc/">
       Options Indexes MultiViews FollowSymLinks
       AllowOverride None
       Order deny,allow
       Deny from all
       Allow from 127.0.0.0/255.0.0.0 ::1/128
   </Directory>

</VirtualHost>

port.confを変更してSSL用のportも使用できるようにする。

# diff -uN /etc/apache2/ports.conf.orig /etc/apache2/ports.conf
--- /etc/apache2/ports.conf.orig        2006-03-22 13:57:53.230800000 +0900
+++ /etc/apache2/ports.conf     2006-03-22 15:14:54.706483648 +0900
@@ -1 +1,2 @@
 Listen 80
+Listen 443

SSLの設定を有効にする。

# a2ensite ssl

SSLのモジュールがデフォルトではOFFになっているので、下記のようにして有効にする。

# a2enmod ssl
Module ssl installed; run /etc/init.d/apache2 force-reload to enable.

設定ファイルに誤りがないかを確認する。

# apache2ctl -t
Syntax OK

apache2の再読み込み

# /etc/init.d/apache2 force-reload

opensslを使用して、接続の確認をする。

# openssl s_client -connect localhost:https
(この後は、通常のtelnetと同じ)

virtualドメインのwebサイトの設定

以下は古いコンテンツです。変更後は下の項目にあります。

virtualドメインの作成で作った、各ドメインごとのhomeディレクトリに wwwというディレクトリを作成する。

a.comの場合

domainDocumentRootLogdir
a.com/home/a/www/htdocs/home/a/www/logs
b.com/home/b/www/htdocs/home/b/www/logs
c.com/home/c/www/htdocs/home/c/www/logs
# mkdir /home/a/www
# mkdir /home/a/www/htdocs
# mkdir /home/a/www/logs
# chown -R root:a /home/a/www/
# chmod g+w /home/a/www/htdocs/

各ドメイン用に設定ファイルを作成する。

# cat /etc/apache2/sites-available/a
<VirtualHost *>
 ServerAdmin webmaster@localhost
 ServerName a.com
 DocumentRoot /home/a/www/htdocs
 AddDefaultCharset off

 ErrorLog /home/a/www/logs/error.log
 # Possible values include: debug, info, notice, warn, error, crit,
 # alert, emerg.
 LogLevel warn
 CustomLog /home/a/www/logs/access.log combined
</VirtualHost>

a.comの設定ファイルを有効にする。

# a2ensite a

apacheの書式チェックを行う

# apache2ctl -t

apacheの設定の再読込

# /etc/init.d/apache2 force-reload

telnetで動作確認をする。

virtualドメインのwebサイトの設定 2

コンテンツが増えてきそうなので、/home/www 以下にドメイン名に近いディレクトリを作成してその中にhtdocsやlogsなどを作成してすべてのweb関連を /home/www内にまとめる。

a.comの場合

domainDocumentRootLogdir
a.com/home/www/a/htdocs/home/www/a/logs
b.com/home/www/b/htdocs/home/www/b/logs
c.com/home/www/c/htdocs/home/www/c/logs
# mkdir /home/www/a
# mkdir /home/www/a/htdocs
# mkdir /home/www/a/logs
# chown -R root:a /home/www/a/
# chmod g+w /home/www/a/htdocs/
# chmod g+s /home/www/a/htdocs/

各ドメイン用に設定ファイルを作成する。

# cat /etc/apache2/sites-available/a
<VirtualHost *>
 ServerAdmin webmaster@localhost
 ServerName a.com
 DocumentRoot /home/www/a/htdocs
 ErrorLog /home/www/a/logs/error.log
 # Possible values include: debug, info, notice, warn, error, crit,
 # alert, emerg.
 LogLevel warn
 CustomLog /home/www/a/logs/access.log combined
</VirtualHost>

a.comの設定ファイルを有効にする。

# a2ensite a

apacheの書式チェックを行う

# apache2ctl -t

apacheの設定の再読込

# /etc/init.d/apache2 force-reload

telnetで動作確認をする。

書き込みが必要な人は、各グループに所属させておく必要がある。

# ldapaddusertogroup test1 a

各ユーザーのページの作成

各ユーザーごとのページを作成するには、各ユーザーの$HOMEディレクトリに public_html というディレクトリを作成する。(/etc/apache2/mods-available/userdir.conf)に設定がかかれている。

% mkdir public_html
% vi ~/public_html/index.html

index.htmlがはじめに表示されるので、適当にページを作成する。

apacheのuserdirを作成するモジュールをenableにする。(一度やればよい)

# a2enmod userdir

apacheの設定再読込を実行して、telnetで確認する。

# /etc/init.d/apache2 force-reload
$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /~a-1/ HTTP/1.1
HOST: etch.local.kensuke.jp

HTTP/1.1 200 OK
Date: Mon, 09 Oct 2006 14:17:32 GMT
Server: Apache/2.0.55 (Debian) mod_ssl/2.0.55 OpenSSL/0.9.8c
Last-Modified: Mon, 09 Oct 2006 14:17:19 GMT
ETag: "a5c69-d-7ac719c0"
Accept-Ranges: bytes
Content-Length: 13
Content-Type: text/html; charset=UTF-8

a-1 home dir

各バーチャルホストのユーザーでのURLが簡単になるように

下記のようにバーチャルホストの設定をすることにより、バーチャルホストのユーザーでも~usernameでアクセスが可能になる。

<VirtualHost *>
        (ここまでは既存の設定で、下記を追記する。)

        RewriteEngine on
        RewriteLog "/var/log/apache2/rewrite.log"
        RewriteLogLevel 9
        RewriteCond %{HTTP_HOST}        ^a\.com$
        RewriteRule ^/~([^/]+)/?(.*)    /~a-$1/public_html/$2 [L]

</VirtualHost>

参照URL:http://dog.intcul.tohoku.ac.jp/unix/accs-rest.html (特に.htaccess内で使用する場合に参考になる。)
http://japache.infoscience.co.jp/rewriteguide/

webdav

/usr/share/doc/apache2.2-common/examples/apache2/extra/httpd-dav.confをもとに下記のような /etc/apache2/conf.d/webdavを作成する。

# cat /etc/apache2/conf.d/webdav
#
# Distributed authoring and versioning (WebDAV)
#
# Required modules: mod_dav, mod_dav_fs, mod_setenvif, mod_alias
#                   mod_auth_digest, mod_authn_file
#

# The following example gives DAV write access to a directory called
# "uploads" under the ServerRoot directory.
#
# The User/Group specified in httpd.conf needs to have write permissions
# on the directory where the DavLockDB is placed and on any directory where
# "Dav On" is specified.

#DavLockDB "/var/DavLock"
DavLockDB "/tmp/DavLock"

Alias /uploads "/var/www/uploads"

<Directory "/var/www/uploads">
   Dav On

#    Order Allow,Deny
    Allow from all

#    AuthType Digest
#    AuthName DAV-upload
    # You can use the htdigest program to create the password database:
    #   htdigest -c "/user.passwd" DAV-upload admin
#    AuthUserFile "/user.passwd"

    # Allow universal read-access, but writes are restricted
    # to the admin user.
#    <LimitExcept GET OPTIONS>
#        require user admin
#    </LimitExcept>
</Directory>

#
# The following directives disable redirects on non-GET requests for
# a directory that does not include the trailing slash.  This fixes a
# problem with several clients that do not appropriately handle
# redirects for folders with DAV methods.
#
BrowserMatch "Microsoft Data Access Internet Publishing Provider" redirect-carefully
BrowserMatch "MS FrontPage" redirect-carefully
BrowserMatch "^WebDrive" redirect-carefully
BrowserMatch "^WebDAVFS/1.[0123]" redirect-carefully
BrowserMatch "^gnome-vfs/1.0" redirect-carefully
BrowserMatch "^XML Spy" redirect-carefully
BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully

explorerなどのdavクライアントから、http://IP.Address.x.x/davtest/にアクセスをしてみて、日本語のファイルやフォルダー名を作成する。その後再度読み込んで文字化けをしないか確認する。

apache2の動作権限で書き込まれるために、www-dataが書き込みができるようにしておく必要がある。

webdav(テスト中)

下記のように、まずwebdav用のモジュールを有効にする。

# a2enmod dav
Module dav installed; run /etc/init.d/apache2 force-reload to enable.
# a2enmod dav_fs
Module dav_fs installed; run /etc/init.d/apache2 force-reload to enable.

下記のような設定ファイルを作成して、/tmpに書き込みできるかを確認することにする。

# cat /etc/apache2/conf.d/webdav
Alias /davtest "/tmp"
 <Location /davtest>
   DAV on
 </Location>

設定ファイルが正しいかを確認する。

# apache2ctl -t
Syntax OK

apacheの設定を再読み込み

# /etc/init.d/apache2 force-reload

explorerなどのdavクライアントから、http://IP.Address.x.x/davtest/にアクセスをしてみて、日本語のファイルやフォルダー名を作成する。その後再度読み込んで文字化けをしないか確認する。

apache2の動作権限で書き込まれるために、www-dataが書き込みができるようにしておく必要がある。

最終的には、下記のようにsites-available/defaultに書き足すことによって、各ユーザー毎のdav領域を作成することができる。

<Directory "/home/*/public_html/dav/">
  DAV on
</Directory>

http://IP.Address.x.x/~username/dav にDAVクライアントからアクセスをする。 このままだと誰でも書き込めてしまうので、BASIC認証と組み合わせる。

public_htmlディレクトリのpermission

public_htmlのディレクトリパーミッションについては、実は755でなくとも良い。701がついていればapacheからは表示される。

ただし、各コンテンツは、www-dataからreadできるようになっていないと、apacheで表示することはできない。

BASIC認証の設定

BASIC認証をユーザー領域で行う。

まずは、apacheの設定ファイルを変更し、public_html以下でAuth Configを使用できるようにする。

etchのapache2の標準の設定では何もしなくても使用できるようであるが、sourceからインストールした時などは下記のようにして、設定を加える。

UserDir public_html

の設定が書いてあるそばに、下記のような設定をする。

# diff -uN httpd.conf.old httpd.conf
--- httpd.conf.old   Fri Nov 26 16:17:34 2004
+++ httpd.conf  Fri Nov 26 17:56:13 2004
@@ -424,6 +424,10 @@
 #    </LimitExcept>
 #</Directory>

+<Directory /home/*/public_html>
+    AllowOverride AuthConfig
+</Directory>
+
 <Directory /usr/local/apache2/htdocs/webmail>
        AllowOverride Limit
 </Directory>

認証をかけたいディレクトリに下記のような .htaccessファイルを設置する。

AuthType Basic
AuthName ""
AuthUserFile /home/a/.htpasswd
<Limit GET POST>
require valid-user
</Limit>

AuthNameには、browserに表示させる文言を書くことができる。日本語を入れると文字化けする模様。
AuthUserFileは、FullPathで パスワードファイルを指定する。
パスワードファイルは、Document Root以下には配置しないこと。

パスワードファイルを作成する場合には、下記のようにして作成する。

# /usr/local/apache2/bin/htpasswd -cm /home/a/.htpasswd [ユーザー名]

Digest認証の設定

基本的には、BASIC認証と違わない感じだが、下記のような特徴がある。

  • HTTP/1.1対応のブラウザでのみ利用可能
  • 認証情報はMD5で暗号化され、チャレンジ・レスポンス型で認証されるので、セッション開設ごとに値が変わるので暗号解読が困難となり、パケット盗聴されても解読はかなり困難
  • IE6では使えず、firefoxだと問題なさそう。(Vista IE7では使用できそう)

サーバー側の設定は、BASIC認証と同様に、AuthConfigが許可されている必要がある。 他に、

LoadModule auth_digest_module modules/mod_auth_digest.so

が有効になっていること。etchでは、

# a2enmod auth_digest

とすることで設定される。

ユーザー領域では、認証をかけたいディレクトリに下記のような .htaccessファイルを設置する。

apache2.0とapache2.2の場合でパスワードファイルの指定方法が変更になっているので注意。

(apache2.0の場合)

AuthType Digest
AuthName realm
AuthDigestFile /home/a/.htpasswd_dig
<Limit GET POST>
require valid-user
</Limit>

(apache2.2の場合)

AuthType Digest
AuthName realm
#AuthDigestFile /var/www/test/digest/.htpasswd_dig
AuthUserFile /var/www/test/digest/.htpasswd_dig
<Limit GET POST>
require valid-user
</Limit>

ダイジェスト認証のパスワードファイルは下記のように作成する。

# /usr/local/apache2/bin/htdigest -c /home/a/.htpasswd_dig realm [ユーザー名]

PAMでの認証(テスト中 - だめみたい)

どうしても、PAMを使用しての認証を行う必要があるために、下記のようにしてPAMでの認証が可能なように設定する。

# apt-get install libapache2-mod-auth-pam
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
  libapache2-mod-auth-pam
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 8862B of archives.
After unpacking 106kB of additional disk space will be used.
# adduser www-data shadow

ldapでの認証

authnz_ldapモジュールを使用する。

# a2enmod authnz_ldap
# /etc/init.d/apache2 restart

http://www.g-ninja.net/freebsd/ldapapache22.html のようにするとできる。

apache2のインストール直後に、

  • /var/www/index.htmlを作成する。
  • /etc/apache2/sites-available/defaultを下記のように編集する。(AllowOverride)の部分
       <Directory /var/www/>
               Options Indexes FollowSymLinks MultiViews
               #AllowOverride None
               AllowOverride AuthConfig
  • a2enmod authnz_ldap とする。
  • 下記のようなhtaccessを作成する。
    # cat /var/www/.htaccess
    AuthType Basic
    AuthName "test"
    AuthBasicProvider ldap
    AuthLDAPURL ldap://127.0.0.1/ou=People,dc=nodomain?uid
    Require ldap-attribute objectClass=posixAccount

これで、w3m http://127.0.0.1/index.htmlとアクセスをして、ldapのユーザー名とパスワードでログインできるかを確認する。

設定のチューニング

etch的な書式を考えると、チューニングの設定項目はapache2.confをいじるのではなく、 conf.dの下にoptimizeというファイルを置いて変更していく(のが良さそうである)。

apacheのバージョン表示停止

serverのバージョンとかまで、出ているのが気になるので、下記の設定をして出力されないようにした。

# cat /etc/apache2/conf.d/optimize
ServerTokens Prod

(apache2.confに書かれているものをコメントアウト)

# cat /etc/apache2/apache2.conf
#ServerTokens Full

default_siteの設定

default_siteを下記のようなポリシーで変更していく。

  • Document Root以下は Indexは不要。
  • シンボリックリンクも使用しない(はず)
  • cgiもない(はず)
# diff -uN /etc/apache2/sites-available/default.orig /etc/apache2/sites-available/default
--- /etc/apache2/sites-available/default.orig   2006-08-06 06:46:12.000000000 +0900
+++ /etc/apache2/sites-available/default        2006-10-14 08:06:53.000000000 +0900
@@ -1,6 +1,6 @@
 NameVirtualHost *
 <VirtualHost *>
-  ServerAdmin webmaster@localhost
+  ServerAdmin webmaster@etch.local.kensuke.jp

   DocumentRoot /var/www
   <Directory />
@@ -8,21 +8,10 @@
        AllowOverride None
   </Directory>
   <Directory /var/www/>
-       Options Indexes FollowSymLinks MultiViews
+       Options MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
-       # Uncomment this directive is you want to see apache2's
-       # default start page (in /apache2-default) when you go to /
-       #RedirectMatch ^/$ /apache2-default/
-  </Directory>
-
-  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
-  <Directory "/usr/lib/cgi-bin">
-       AllowOverride None
-       Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
-       Order allow,deny
-       Allow from all
   </Directory>

   ErrorLog /var/log/apache2/error.log

webdavを使用したコンテンツの書き換え

http://www.aconus.com/~oyaji/faq/apache_webdav.htm


トップ   一覧 単語検索   最終更新のRSS
Last-modified: 2007-03-22 (木) 11:28:46 (1772d)