Webサーバ apache

Apacheはいわずと知れたウェブサービスを提供するソフトウェアです.今では最新版として2.0が出ています.インストール後,デーモンを立ち
上げ,127.0.0.1にアクセスするとすぐに「It
worked!」などという画面がすぐに出てくれます.ただ,外部に公開するにはいくつか設定を変更しなくてはなりません.まず、
/etc/apache2/httpd.confの変更するべき一部を抜粋して紹介します.

# emerge apache2

でインストールして、設定です。emerge apacheとすると、apache1がインストールされるので注意してください。ここはお好みです。

#サーバの管理者のメールアドレス
ServerAdmin webmaster@tamochan.com
#サーバの名前
ServerName www.tamochan.com
#http://www.tamochan.comのルートディレクトリ.まずはじめに参照するディレクトリ.
DocumentRoot "/home/webmaster/public_html"    

#/home/*/public_htmlの設定.CGIやSSIが動くようにしてある.
<directory home/*/public_html>
    AllowOverride FileInfo AuthConfig Limit
    #CGIの実行を許可
    Options MultiViews Indexes SymLinksIfOwnerMatch ExecCGI
  #SSIの実行を許可
    Options +Includes
    <Limit GET POST OPTIONS PROPFIND>
    Order allow,deny
    Allow from all
    </Limit>
    <LimitExcept GET POST OPTIONS PROPFIND>
      Order deny,allow
      Deny from all
    </LimitExcept>
</Directory>

#CGIのファイルを設定   
AddHandler cgi-script .cgi
#SSIのファイルを設定
AddType text/html .shtml
AddHandler server-parsed .shtml

#790行くらい。
DefaultLanguage ja

#815行くらい。AddLanguageがたくさん並んでるが、jaを一番上に。
AddLanguage ja .ja

#850行くらい。jaを先頭に。
LanguagePriority ja en ca cs da de el eo es et fr he hr it ko
ltz nl nn no pl pt pt-BR

#870行くらい。ISO-8859-1をコメントアウト。
#AddDefaultCharset ISO-8859-1
#Charsetはhtmlファイルを優先にする。
AddDefaultCharset Off

#一番最後。バーチャルホストの設定は/etc/apache2/vhosts.d/vhosts.confに書く。
#
# Gentoo VHosts
#
# For Gentoo we include External Virtual Hosts Files.
# Please see vhosts.d/00_default_vhost.conf for the default virtual host.
#
Include /etc/apache2/vhosts.d/vhosts.conf

次に、後述するバーチャルホストの設定を行います。/etc/apache2/vhosts.d/vhosts.confで、

#バーチャルホストの設定
#まずはNameVirtualHostにサーバのIPを設定
NameVirtualHost 192.168.0.22
#もし,http://tanpopo.tamochan.comでアクセスしてきた場合
#ドキュメントルートは/home/tanpopo/public_htmlに設定
<VirtualHost 192.168.0.22>
    ServerAdmin tanpopo@tamochan.com
    DocumentRoot /home/tanpopo/public_html
    ServerName tanpopo.tamochan.com
    #ログの出力先
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log common
</VirtualHost>

CGIやSSIの設定が少々面倒くさいといったところでしょうか.ユーザに好評なのがヴァーチャルホストの設定.ユーザのホームページがhttp:
//***.tamochan.comのような,チルダなしのアドレスで公開できます.ヴァーチャルホストの設定後は,違う名前でもアクセスできるよう
に,/var/named/domain(正引きファイル)に,たとえば次の一行を足しておきます.

tanpopo	IN	CNAME	dns.tamochan.com.

また、phpの設定を追加するときは、/etc/apache2/httpd.confで、

#phpモジュールを読む
LoadModule php4_module modules/libphp4.so
#.phpの動作を宣言
AddType application/x-httpd-php .php
#index.phpがデフォルトページになるように
DirectoryIndex index.html index.html.var index.php

SSLを使いたい場合は、まずapache2がSSL対応になっていることが前提です。/etc/make.confのUSEフラグにsslを追加してコンパイルしなおす必要があります。

今回は、サーバが自前で認証局になる方法を紹介します。/etc/apache2/sslなるディレクトリを作成し、そこにサーバの秘密鍵と証明書
要求(CSR)を作成します。/etc/apache2/sslに移動して、まずはserver.keyというサーバの秘密鍵を作ります。

# openssl genrsa -des3 -rand /var/log/messages -out server.key 1024
10441 semi-random bytes loaded
Generating RSA private key, 1024 bit long modulus
.......................................................++++++
.......++++++
e is 65537 (0x10001)
Enter PEM pass phrase (パスワード入力)
Verifying password - Enter PEM pass phrase: (再度パスワード入力)

次にCSRの作成です。server.csrというファイルで作成します。

$ openssl req -new -key server.key -out server.csr
Using configuration from /etc/ssl/openssl.cnf
Enter PEM pass phrase:(サーバの秘密鍵で入れたパスワード)
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) [AU]:(日本ならと入力)
State or Province Name (full name) [Some-State]:(自分の住所などを入力)
Locality Name (eg, city) []: (入力)
Organization Name (eg, company) [Internet Widgits Pty Ltd]:  (入力)
Organizational Unit Name (eg, section) []: (入力)
Common Name (eg, YOUR name) []: (入力)
Email Address []:  (入力)

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:(ただEnterを押す)
An optional company name []:(ただEnterを押す)

次にapache2の設定です。/etc/apache2/modules.d/41_mod_ssl.default-vhost.conf で、

・・・・
<IfModule mod_ssl.c>
##
## SSL Virtual Host Context
##

#バーチャルホストの設定。https://でアクセスするとここにアクセスされる。
<VirtualHost 192.168.0.20:443>

#   General setup for the virtual host
DocumentRoot "/var/httpd/https"
ServerName www.tamochan.com:443
ServerAdmin webmaster@tamochan.com
ErrorLog /var/log/apache2/ssl_error_log
<IfModule mod_log_config.c>
        TransferLog logs/ssl_access_log
</IfModule>
・・・・
#CSRファイルのありか
SSLCertificateFile /etc/apache2/ssl/server.csr
#サーバ秘密鍵のありか
SSLCertificateKeyFile /etc/apache2/ssl/server.key

さらに、起動オプションの設定です。/etc/conf.d/apache2 で、ヴァーチャルホスト、SSL、SSLヴァーチャルホストを有効にする設定です。

APACHE2_OPTS="-D DEFAULT_VHOST -D SSL -D SSL_DEFAULT_VHOST"

設定後、rc-updateを用いて、デーモンが起動時にも自動的に起動するようにします。

dns # rc-update add apache2 default

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です