Web ページの仕組み

3. Web ページの公開

必修

  1. apache
  2. 最低限 HTML
  3. Webページの公開
  4. 本日の課題

付録

  1. 特殊文字
  2. スタイルシート

[3.1] ファイルの置き場所

前のページでは,HTML を書くための最低限の解説をしました. しかし,それだけではウェブブラウザからそのHTML ファイルを公開・閲覧することはできません. 適切な場所に置くことでWebページを公開・閲覧することが可能となります. ただし,Web ページの公開に際し以下のことに注意してください.

[3.1.1] public_html ディレクトリ

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 を作成します.

$ cd ~
$ mkdir public_html
$ ls -l 

drwxr-xr-x  7 hogehoge hogehoge      4096  6月 25 16:40 public_html

もし,other に読み込み権限が無ければ,以下のように権限を与えましょう.

$ chmod o+r public_html

これで公開用ディレクトリの準備は完了です.このディレクトリの中に 公開したいファイルを置き,Webブラウザから適切なURL を入力することで, そのファイルにアクセスします.例えば,sample.html をpublic_html 以下に置き, それをWeb ブラウザからアクセスする場合を考えます. public_html ディレクトリ以下に「sample.html」を作成し,中身を編集します.

$ cd ~/public_html 
$ vi sample.html

以下のように入力します.

<!DOCTYPE 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 の番号を変えてみる).

[3.2] index.html

先ほどは公開用ディレクトリの作成とページの閲覧を行いました. 次はpublic_html ディレクトリ以下に「index.html」というファイルを作成します.

$ cd ~/public_html 
$ vi index.html

以下のように入力します.

<!DOCTYPE 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>

[3.3] ページへのアクセス制限をかける

これまでは誰にでも見られてよいページの公開方法の解説をしてきましたが, 特定の人にだけ公開したいページもあるはずです. ここではそんなときどうしたいかを解説します.

[3.3.1] .htaccess

アクセス制限をかけたいディレクトリにこのファイルを置きます. この名前は「apache2.conf」というファイルの中で設定されています.

$ lv /etc/apache2/apache2.conf

# 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 and /usr/share/doc/apache2-common/README.Debian.gz about
# Debian specific hints.
#
#
# Summary of how the Apache 2 configuration works in Debian:
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's
# default Apache2 installation attempts to make adding and removing modules,
# virtual hosts, and extra configuration directives as flexible as possible, in
# order to make automating the changes and administering the server as easy as
# possible.

# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
…
(略)
…

#
# 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>
…
(略)
…

[3.3.2] アクセス制限のかけ方

アクセス制限をするディレクトリ(secret) を作成し,その中に閲覧されたくないファイル(secret.html) を格納します.

$ mkdir ~/public_html/secret
$ vi ~/public_html/secret/secret.html

<!DOCTYPE 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 を閲覧することができます.

[3.4] ソフトウェアレベルでのアクセス制限

先ほどは,.htaccess というファイルを置くことで 特定のページへのアクセスを制限しましたが, 例えば, Dos攻撃を仕掛けてくる特定の計算機があった場合, その計算機だけをアクセス禁止にしたい!と, 思うことがあるかもしれません . そういう場合の対策として, 特定のIPアドレスに対してアクセス制限 を行うことができます. 設定ファイルは /etc/apache2/sites-available/default です.

#  vi /etc/apache2/sites-available/default 

<VirtualHost *:80>
       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
                Deny from 192.168.16.1XX "XX"は, 拒否したい情報実験機の番号   
…
(略)
…

と書き換えます.

隣の情報実験機を拒否した場合は, 隣の情報実験機からブラウザのURLに以下のように入力します.

http://192.168.16.1XX/

XX は拒否の設定を行った情報実験機の番号です.

irworks


次ページへ

情報実験第十回のページへ戻る

最終更新日: 2014/06/25 渡辺 健介 改訂 Copyright © 2000-2014 inex