Apache HTTP Server をインストールする

Apache HTTP Server は最も広く利用されている HTTP サーバ・ソフトウェアです。 OpenBSD でも、3.0版以降では、OS標準の機能として装備しています。 しかし、標準のものが持っていない機能を必要とする場合や、 異なるバージョンを使用したい場合には、別にインストールすることになります。 ここでは、mod_ssl と mod_perl を組み込んだものをインストールする手順を説明します。

Apache 1.3版系の最新版は、1.3.33 (2004/10/28 リリース) です。
mod_ssl の最新版は、2.8.22-1.3.33 (2004/10/30 リリース) です。
mod_perl の最新版は、1.29 (2003/10/07 リリース) です。

現時点で mod_perl が正式に対応している Apache は 1.3.XX 版のみです。 2.0.XX 版に対応した mod_perl(Version 1.99_XX)もありますが、 まだテスト中のものであり、運用環境で使用できるものではありません。

各種情報とソースの入手

  1. 開発者によるWebサイト
    http://httpd.apache.org/ ・・・ Apache HTTP Server
    http://www.modssl.org/ ・・・ mod_ssl
    http://perl.apache.org/ ・・・ mod_perl
  2. 日本語による情報
    http://www.apache.jp/ ・・・ JAPAN APACHE USERS GROUP
  3. Apache HTTP Server の最新リリース (tar archive のサイズ: 2,468,567 byte)
    http://www.apache.org/dist/httpd/Announcement.html ・・・ アナウンス文
    http://www.apache.org/dist/httpd/apache_1.3.33.tar.gz ・・・ tar archive 配布元
    ftp://ftp.t.ring.gr.jp/pub/net/apache/dist/httpd/apache_1.3.33.tar.gz
    この他、 「Apache をダウンロードしよう」 に他数のサーバが記載されています。
  4. mod_ssl の最新リリース (tar archive のサイズ: 754,606 byte)
    http://www.modssl.org/source/mod_ssl-2.8.22-1.3.33.tar.gz
  5. mod_perl の最新リリース (tar archive のサイズ: 378,877 byte)
    http://perl.apache.org/dist/mod_perl-1.29.tar.gz

準備

  1. グループID "www" を登録します。
    OpenBSD 3.0版以降であれば標準で登録されているはずので、それをそのまま利用します。 もし未登録の場合には、/etc/group ファイルに以下のような設定を追加します。
    www:*:67:
    
  2. ユーザID "www" を登録します。
    OpenBSD 3.0版以降であれば標準で登録されているはずので、それをそのまま利用します。 もし未登録の場合には、vipw コマンドで以下のような設定を追加します。
    www:*:67:67::0:0:HTTP server:/usr/local/apache:/sbin/nologin
    

コンパイル

  1. ソースを展開します。
    % tar xzvf …/apache_1.3.33.tar.gz
    % tar xzvf …/mod_ssl-2.8.22-1.3.33.tar.gz 
    % tar xzvf …/mod_perl-1.29.tar.gz 
    
  2. mod_ssl の環境設定をします。
    % cd mod_ssl-2.8.22-1.3.33/
    % ./configure --with-apache=../apache_1.3.33
    
  3. mod_perl の環境設定と全体のコンパイルをします。
    % cd ../mod_perl-1.29/
    % perl Makefile.PL USE_APACI=1 EVERYTHING=1 DO_HTTPD=1 SSL_BASE=/usr APACHE_PREF
    IX=/usr/local/apache APACHE_SRC=../apache_1.3.33/src APACI_ARGS='--runtimedir=/v
    ar/run,--logfiledir=/var/log/apache,--server-uid=www,--server-gid=www,--enable-m
    odule=ssl,--enable-module=rewrite,--enable-module=expires,--disable-module=userd
    ir,--disable-module=autoindex'
    % make
    
    perl Makefile.PL の行は折り返して表示していますが、実際には1行で入力します。
    ここで、Apache HTTP Server の configure オプションを APACI_ARGS に指定しています。 Apache の configure は 非常に多くのオプションを持っています。 Apache を構成する 「モジュール」 の何を組み込み、何を外すかの指定もここで行ないます。 なお、上記のオプションは当方でコンパイルした時の一例であり、 環境や目的に合わせた適切な指定をする必要があります。

インストールと設定

  1. インストールします。
    この操作は mod_perl のソースディレクトリから行います。
    % su
    # make install
    # mkdir /var/log/apache
    
    Apache本体のインストール先は /usr/local/apache になります。
    Perlモジュールは /usr/local/libdata/perl5/site_perl/i386-openbsd の下に置かれます。
  2. SSLで使用する証明書をインストールします。
    正式に公開運用するサーバであれば、VeriSignなどの認証機関に署名してもらう必要があるのですが、 ここでは、個人で使うプライベートなサーバなので、自己署名の証明書を作成します。 ブラウザからアクセスした時に、証明書に問題があるという警告が出ますが、 それを無視すれば、SSLによる通信の暗号化は機能します。
    # cd /usr/local/apache/conf/
    #       <サーバの秘密鍵を生成します>
    # openssl genrsa -out ssl.key/server.key 1024
    Generating RSA private key, 1024 bit long modulus
    ................++++++
    ................++++++
    e is 65537 (0x10001)
    # ls -l ssl.key/server.key 
    -r--------  1 root  wheel  891 Aug 16 22:00 ssl.key/server.key
    #       <証明書署名要求を生成します>
    # openssl req -new -key ssl.key/server.key -out ssl.csr/server.csr
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) []:JP
    State or Province Name (full name) []:Tokyo
    Locality Name (eg, city) []:Nerima-ku
    Organization Name (eg, company) []:HRK Networks, Ltd
    Organizational Unit Name (eg, section) []:Webserver Team
    Common Name (eg, fully qualified host name) []:myserevr.my.domain
    Email Address []:webmaster@myserevr.my.domain
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
    # ls -l ssl.csr/server.csr
    -r--------  1 root  wheel  741 Aug 16 22:03 ssl.csr/server.csr
    #       <証明書に自分で署名します>
    # openssl x509 -req -days 366 -in ssl.csr/server.csr -signkey ssl.key/server.key
     -out ssl.crt/server.crt
    Signature ok
    subject=/C=JP/ST=Tokyo/L=Nerima-ku/O=HRK Networks, Ltd/OU=Webserver Team/CN=myse
    revr.my.domain/emailAddress=webmaster@myserevr.my.domain
    Getting Private key
    # ls -l ssl.crt/server.crt
    -r--------  1 root  wheel  1017 Aug 16 22:05 ssl.crt/server.crt
    # 
    
  3. 設定ファイル conf/httpd.conf を編集します。 詳細な説明がコメントとして書かれているので、それに従って設定します。 Webサイト 「Apache ディレクティブ」 にも詳細な仕様が書かれています。 このサンプルを後に添付するので、参考にしてください。 ただし、サンプルはテスト用サーバのもので一般への公開用ではありません。 公開するサーバの場合には、情報の漏洩などを起こさないよう、 十分に注意して設定する必要があります。
  4. 起動の設定を /etc/rc.local に記述します。
    echo -n ' apache'
    /usr/local/apache/bin/apachectl startssl
    
  5. 停止の設定を /etc/rc.shutdown に記述します。
    /usr/local/apache/bin/apachectl stop
    

httpd.conf のサンプル

  1. テスト用サーバを想定したものです。 ポートは通常接続用に80番を、SSL接続用に443番を使用、 サーバを実行する UID/GID は www/www を使用、 公開するのディレクトリは /usr/local/apache/htdocs、 /cgi-bin/* へのアクセスは mod_perl により /usr/local/apache/perl/* のスクリプトを実行、 SSLによるアクセスでも通常のアクセスと同じものを見せるという設定です。
    ### Section 1: Global Environment
    ServerType standalone
    ServerRoot "/usr/local/apache"
    PidFile /var/run/httpd.pid
    ScoreBoardFile /var/run/httpd.scoreboard
    Timeout 300
    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 15
    MinSpareServers 3
    MaxSpareServers 5
    StartServers 5
    MaxClients 50
    MaxRequestsPerChild 0
    ServerTokens OS           ← ServerTokens として詳細な情報を出さないようにする。
                                 この設定の場合、"Apache 1.3.XX (Unix)" だけになる。
    
    ### Section 2: 'Main' server configuration
    Port 80
    <IfDefine SSL>
    Listen 80
    Listen 443
    </IfDefine>
    User www
    Group www
    ServerAdmin webmaster@myserevr.my.domain
    ServerName myserevr.my.domain
    
    DocumentRoot "/usr/local/apache/htdocs"
    <Directory />
        Options None
        AllowOverride None
        Order Allow,Deny
        Deny from all
    </Directory>
    <Directory "/usr/local/apache/htdocs">
        Options None
        AllowOverride None
        Order Allow,Deny
        Allow from all
    </Directory>
    <IfModule mod_dir.c>
        DirectoryIndex index.html
    </IfModule>
    AccessFileName .htaccess
    <Files ~ "^\.ht">
        Order Allow,Deny
        Deny from all
    </Files>
    
    UseCanonicalName On
    <IfModule mod_mime.c>
        TypesConfig /usr/local/apache/conf/mime.types
    </IfModule>
    DefaultType text/plain
    <IfModule mod_mime_magic.c>
        MIMEMagicFile /usr/local/apache/conf/magic
    </IfModule>
    
    HostnameLookups Off
    ErrorLog "|exec /usr/local/apache/bin/rotatelogs /var/log/apache/error_log.%Y%m%
    d 86400 540"                    ← 折り返して表示していますが、実際は1行です。
    LogLevel warn
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combine
    d                               ← 折り返して表示していますが、実際は1行です。
    CustomLog "|exec /usr/local/apache/bin/rotatelogs /var/log/apache/access_log.%Y%
    m%d 86400 540" combined         ← 折り返して表示していますが、実際は1行です。
    ServerSignature On
    
    <IfModule mod_alias.c>
        Alias /icons/ "/usr/local/apache/icons/"
        <Directory "/usr/local/apache/icons">
            Options Indexes MultiViews
            AllowOverride None
            Order Allow,Deny
            Allow from all
        </Directory>
        Alias /manual/ "/usr/local/apache/htdocs/manual/"
        <Directory "/usr/local/apache/htdocs/manual">
            Options Indexes FollowSymlinks MultiViews
            AllowOverride None
            Order Allow,Deny
            Allow from all
        </Directory>
    </IfModule>
    
    <IfModule mod_autoindex.c>
         ・・・ 省略(defaultのまま) ・・・
    </IfModule>
    
    <IfModule mod_mime.c>
         ・・・ 省略(以下を除きdefaultのまま) ・・・
        #AddEncoding x-compress .Z
        #AddEncoding x-gzip .gz .tgz
        AddType application/x-compress .Z
        AddType application/x-gzip .gz .tgz
    </IfModule>
    
    <IfModule mod_setenvif.c>
         ・・・ 省略(defaultのまま) ・・・
    </IfModule>
    
    <IfModule mod_perl.c>                   ← mod_perlの設定を追加します。
        PerlModule Apache::PerlRun
        Alias /cgi-bin/ "/usr/local/apache/perl/"
        <Directory "/usr/local/apache/perl/">
            SetHandler perl-script
            PerlHandler Apache::PerlRun
            PerlSendHeader On
            Options ExecCGI
            AllowOverride None
            Order Allow,Deny
            Allow from all
        </Directory>
    </IfModule>
    
    ### SSL Global Context
    <IfDefine SSL>
    AddType application/x-x509-ca-cert .crt
    AddType application/x-pkcs7-crl    .crl
    </IfDefine>
    
    <IfModule mod_ssl.c>
    SSLSessionCache         dbm:/var/run/ssl_scache
    SSLSessionCacheTimeout  300
    SSLMutex  file:/var/run/ssl_mutex
    SSLRandomSeed startup file:/dev/arandom 512
    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin
    </IfModule>
    
    <IfDefine SSL>
    <VirtualHost _default_:443>
    DocumentRoot "/usr/local/apache/htdocs"
    ServerName myserevr.my.domain
    ServerAdmin webmaster@myserevr.my.domain
    TransferLog "|exec /usr/local/apache/bin/rotatelogs /var/log/apache/ssl_access_l
    og.%Y%m%d 86400 540"            ← 折り返して表示していますが、実際は1行です。
    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile /usr/local/apache/conf/ssl.crt/server.crt
    SSLCertificateKeyFile /usr/local/apache/conf/ssl.key/server.key
    <Directory "/usr/local/apache/perl">
        SSLOptions +StdEnvVars
    </Directory>
    SetEnvIf User-Agent ".*MSIE.*" \
             nokeepalive ssl-unclean-shutdown \
             downgrade-1.0 force-response-1.0
    </VirtualHost>                                  
    </IfDefine>
    

mod_perl プログラムのサンプル

  1. 環境変数を一覧表示するプログラムです。
    上の httpd.conf を使用している環境であれば、 /usr/local/apache/perl/printenv というファイル名で作成し、chmod +x することで、 http://サーバ名/cgi-bin/printenv で呼び出すことができます。 Apache::PerlRun による実行なので、CGI-BINと同じインターフェースで動作します。
    #!/usr/bin/perl
    
    print("Content-type: text/plain; charset=EUC-JP\n\n");
    foreach(sort(keys(%ENV))){
        print("$_ = $ENV{$_}\n");
    }