2013年7月28日日曜日

カゴヤVPSのcent os6サーバにてバーチャルホストの設定手順

1つのIPアドレスで、複数のサイトを別々のホスト名で公開するには、名前ベースのバーチャルホストの設定が必要です。
この手順では1つのIPアドレスで、www.example.com と www.example.co.jp の2つのサイトを公開する場合を例にご案内します。
  1. rootアカウントでログインします。
    [root@v0000 ~]#

    プロンプト表示に続けて次のコマンドを入力します。
    [root@v0000 ~]# vi /etc/httpd/conf/httpd.conf
    • 「vi」と「/etc/httpd/conf/httpd.conf」の間はスペースが入ります。

    入力したらキーボードの Enterキーを押します。

  2. viエディタで[httpd.conf]の内容が表示されます。
    #
    # This is the main Apache server configuration file. It contains the
    # configuration directives that give the server its instructions.
    # See <URL:http://httpd.apache.org/docs/2.2/> for detailed information.
    # In particular, see
    # <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
    # for a discussion of each configuration directive.
    #
    #
    # 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:
    #  1. Directives that control the operation of the Apache server process as a
    #     whole (the 'global environment').
    #  2. Directives that define the parameters of the 'main' or 'default' server,
    #     which responds to requests that aren't handled by a virtual host.
    #     These directives also provide default values for the settings
    #     of all virtual hosts.
    #  3. Settings for virtual hosts, which allow Web requests to be sent to
    #     different IP addresses or hostnames and have them handled by the
    #     same Apache server process.

    ファイルの最終行に移動するため、次のコマンドを入力します。
    :$

    入力したらキーボードの Enterキーを押します。

  3. [httpd.conf]の最終行に移動します。
    #
    # Use name-based virtual hosting.
    #
    #NameVirtualHost *:80
    #
    # NOTE: NameVirtualHost cannot be used without a port specifier
    # (e.g. :80) if mod_ssl is being used, due to the nature of the
    # SSL protocol.
    #

    #
    # VirtualHost example:
    # Almost any Apache directive may go into a VirtualHost container.
    # The first VirtualHost section is used for requests without a known
    # server name.
    #
    #<VirtualHost *:80>
    #    ServerAdmin webmaster@dummy-host.example.com
    #    DocumentRoot /www/docs/dummy-host.example.com
    #    ServerName dummy-host.example.com
    #    ErrorLog logs/dummy-host.example.com-error_log
    #    CustomLog logs/dummy-host.example.com-access_log common
    #</VirtualHost>

    #NameVirtualHost *:80」という記述を探します。
    NameVirtualHost ディレクティブでは、名前ベースのバーチャルホストを利用してリクエストを受け付けるサーバのIPアドレスとポート番号を設定します。
    ■参考URL: Apache HTTP サーバ ドキュメント - NameVirtualHost ディレクティブ
        http://httpd.apache.org/docs/2.2/ja/mod/core.html#NameVirtualHost

    キーボードの「i」キーを押し、viエディタを入力モードにすると内容を編集できます。
    1つのIPアドレスと特定のポート番号(通常は80番)で複数サイトのアクセスを受け付ける場合、
    #NameVirtualHost *:80」という記述を下記の通り編集します。
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80

    • 行頭の「#」を削除します。


  4. [httpd.conf]の最下部にサイトごとに、バーチャルホストを設定します。
    www.example.com と www.example.co.jp の2つのサイトのバーチャルホストを設定する場合は、下記の記述を追記します。
    #    ErrorLog logs/dummy-host.example.com-error_log
    #    CustomLog logs/dummy-host.example.com-access_log common
    #</VirtualHost>
    <VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot /var/www/html
    </VirtualHost>
    <VirtualHost *:80>
        ServerName www.example.co.jp
        DocumentRoot /var/www/html/corp
    </VirtualHost>

    既にメインホストでサイトを公開していて、バーチャルホストを追加する場合、メインホストもバーチャルホストとして設定しなおす必要があります。
    ServerName ディレクティブDocumentRoot ディレクティブで設定していたメインホストの設定内容と同じ内容を、バーチャルホストに設定します。

    • ホスト名ごとに、<VirtualHost> ブロックを作成します。
    • <VirtualHost> ディレクティブの引数は NameVirtualHost ディレクティブの引数と同じにします。
    • ホスト名ごとに ServerName ディレクティブを設定します。
    • ホスト名ごとに DocumentRoot ディレクティブを設定します。
    • <VirtualHost> コンテナの中に他のディレクティブ(ErrorLog、CustomLog など)を追記することで、バーチャルホストごとに各種設定をすることもできます。

    ここで設定するホスト名は、DNSサーバーに問い合わせることでホスト名からIPアドレスが名前解決できる必要があります。

  5. httpd.conf の編集が完了したら、キーボードの Escキーを押し、viエディタをコマンドモードに戻します。
    編集内容を保存するため、次のコマンドを入力します。
    :w

    入力したらキーボードの Enterキーを押します。

  6. viエディタを終了するため、次のコマンドを入力します。
    :q

    入力したらキーボードの Enterキーを押します。

  7. 設定ファイルの編集内容を反映させるため、httpdを再起動します。
    プロンプト表示に続けて次のコマンドを入力します。
    [root@v0000 ~]# vi /etc/httpd/conf/httpd.conf
    [root@v0000 ~]# /etc/rc.d/init.d/httpd restart
    • 「/etc/rc.d/init.d/httpd」と「restart」の間はスペースが入ります。

    入力したらキーボードの Enterキーを押します。

  8. httpdの再起動が完了すると、下記のように表示されます。
    [root@v0000 ~]# vi /etc/httpd/conf/httpd.conf
    [root@v0000 ~]# /etc/rc.d/init.d/httpd restart
    httpd を停止中:                                            [  OK  ]
    httpd を起動中:                                            [  OK  ]
    [root@v0000 ~]#

    「httpd を起動中: [OK]」と表示されれば、正しく再読み込みできています。

0 件のコメント:

コメントを投稿