| 3. Web ページの公開 | 
|---|
| 必修 | 
| 付録 | 
前のページでは,HTML を書くための最低限の解説をしました. しかし,それだけではウェブブラウザからそのHTML ファイルを公開・閲覧することはできません. 適切な場所に置くことでWebページを公開・閲覧することが可能となります. ただし,Web ページの公開に際し以下のことに注意してください.
Web サーバ構築のためのソフトウェア「apache」の設定ファイルの一つである 「userdir.conf」を見てみましょう.
| 
$ lv /etc/apache2/mods-available/userdir.conf
<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root
        <Directory /home/*/public_html>
                AllowOverride FileInfo AuthConfig Limit Indexes
                Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
                <Limit GET POST OPTIONS>
                        Order allow,deny
                        Allow from all
                </Limit>
                <LimitExcept GET POST OPTIONS>
                        Order deny,allow
                        Deny from all
                </LimitExcept>
        </Directory>
</IfModule>
 | 
ここに,「UserDir public_html」と記述されています. この行は,public_html ディレクトリ以下に置いたHTML ファイルは 公開します,ということを意味します. つまりpublic_html が公開用ディレクトリです. この設定ファイル中のuserdir モジュールを有効にするため には以下のコマンドを実行します.
| # a2enmod userdir | 
もし,このモジュールを無効にしたい場合は,
| # a2dismod userdir | 
を実行します. (これ以降の作業を行う際にはモジュールを有効にしてください.). 最後に, 設定をapache に反映させるためにapache を再起動します.
| # /etc/init.d/apache2 restart | 
モジュールを有効にしたら次は実際にpublic_html を作成して ページを公開してみましょう. まず皆さんのホームディレクトリにpublic_html を作成し, other に読み込み権限があるかを確認し,もしない場合は権限を与えましょう.
| $ cd ~ $ mkdir public_html $ ls -l $ chmod o+r public_html (もし権限がなかったらコマンドを打つ) | 
これで公開用ディレクトリの準備は完了です.このディレクトリの中に 公開したいファイルを置き,Webブラウザから適切なURL を入力することで, そのファイルにアクセスします.例えば,sample.html をpublic_html 以下に置き, それをWeb ブラウザからアクセスする場合を考えます. public_html ディレクトリ以下に「sample.html」を作成し,中身を編集します.
| $ cd ~/public_html $ vi sample.html 以下のように入力します. <html> <head><title>sample</title></head> <body> <h1>This is sample page!!</h1> </body> </html> | 
ファイルの作成が終了したら,ブラウザ上からアクセスしてみましょう. URL 欄に「http://192.168.16.1XX/~hogehoge/sample.html (XX は情報実験機番号,hogehoge はアカウント名)」を入力します. ここでパスの指定の際に「public_html」は書かない(省略されている)ことに注意しましょう. 無事に「This is sample page!!」と表示されれば成功です. 時間があれば他の情報実験機のディレクトリにもアクセスしてみてください (上記のXX の番号を変えてみる).
先ほどは公開用ディレクトリの作成とページの閲覧を行いました. 次はpublic_html ディレクトリ以下に「index.html」というファイルを作成します.
| $ cd ~/public_html $ vi index.html 以下のように入力します. <html> <head><title>index</title></head> <body> <h1>This is index page!!</h1> </body> </html> | 
ここで,ブラウザのURL 欄に「http://192.168.16.1XX/~hogehoge/ (XX は情報実験機番号,hogehoge はアカウント名)」を入力するとどうなるでしょうか? index.html の中身が表示されたはずです. このように,public_html 以下のファイルにアクセスする際に,ファイルの指定をしないと, 表示されるファイルは「index.html」となります. この設定は「dir.conf」に記述されています (このモジュールはデフォルトで有効になっている?,未確認.).
| 
$ lv /etc/apache2/mods-available/dir.conf
<IfModule mod_dir.c>
          DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
 | 
これまでは誰にでも見られてよいページの公開方法の解説をしてきましたが, 特定の人にだけ公開したいページもあるはずです. ここではそんなときどうしたいかを解説します.
アクセス制限をかけたいディレクトリにこのファイルを置きます. この名前は「apache2.conf」というファイルの中で設定されています.
| 
$ lv /etc/apache2/apache2.conf
#
# Based upon the NCSA server configuration files originally by Rob McCool.
#
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.2/ for detailed information about
# the directives.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.
#
# The configuration directives are grouped into three basic sections:
…
(略)
…
#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#
AccessFileName .htaccess
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>
 | 
アクセス制限をするディレクトリ(secret) を作成し,その中に閲覧されたくないファイル(secret.html) を格納します.
| $ mkdir ~/public_html/secret $ vi ~/public_html/secret/secret.html <html> <head><title>secret</title></head> <body> <h1>私の趣味は XX です.</h1> </body> </html> | 
まずはアクセス可能とするユーザ名とパスワードを管理するファイル(passwdfile)を作成します. 以下は,登録ユーザ名がherohero の場合です.herohero は自分のアカウント名でなくてもかまいません.
| $ htpasswd -c /home/hogehoge/passwdfile herohero Adding password for herohero. New password:[パスワードを入力] Re-type new password:[もう一度パスワードを入力] | 
こうしてpasswdfile が作成されます. 次に,制限をかけたいディレクトリに,「.htaccess」というファイルを置きます.
| $ vi ~/public_html/secret/.htaccess AuthType Basic AuthName The-T-Directory AuthUserFile /home/hogehoge/passwdfile <Limit GET POST> require user herohero ← アクセスを許可するユーザ名。 </Limit> | 
これでsecret.html にアクセスすると,ユーザ名とパスワードが聞かれます. ここで,先ほど設定したユーザ名(herohero)とパスワードを入力するとsecret.html を閲覧することができます.
先ほどは,.htaccess というファイルを置くことで 特定のページへのアクセスを制限しましたが, 例えば, Dos攻撃を仕掛けてくる特定の計算機があった場合, その計算機だけをアクセス禁止にしたい!と, 思うことがあるかもしれません . そういう場合の対策として, 特定のIPアドレスに対してアクセス制限 を行うことができます. 設定ファイルは /etc/apache2/sites-available/default です.
| 
#  vi /etc/apache2/sites-available/default 
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from ALL
                Deny from 192.168.16.1XX "XX"は, 拒否したい情報実験機の番号   
 | 
と書き換えます.
隣の情報実験機を拒否した場合は, 隣の情報実験機からブラウザのURLに以下のように入力します.
http://192.168.16.1XX/
XX は拒否の設定を行った情報実験機の番号です.
| 最終更新日: 2013/07/03(三上 峻) | Copyright © 2000-2013 inex |