トップ > Ubuntu 8.04 によるサーバーの構築 >

Apache (Webサーバー) の構築

Apache のインストールと設定

  1. 以下のコマンドを実行して, Apache をインストールする。
    $ sudo apt-get install apache2 ssl-cert
  2. Google ウェブマスターツールなどを利用して,外部からこの Web サーバーにアクセスできるか確認する。

バーチャルホストの作成

 ns.shitomi.jp 及び mail.shitomi.jp の DocumentRoot に /var/www/errorを指定し, www.shitomi.jp, www.shitomi.net 及び www.shitomi.com の DocumentRoot に /var/www/www を指定します。

  1. /var/www/error ディレクトリ及び /var/www/www ディレクトリを作成する。
  2. 以下のコマンドを実行して,アカウント abc が /var/www/error 及び /var/www/www ディレクトリに書き込めるように,アクセス権限を変更する。
    $ sudo chown abc:root /var/www/error
    $ sudo chown abc.root /var/www/www
  3. SFTPにより,アカウント abc で /var/www/error 及び /var/www/www ディレクトリへのアップロードを行うことができることを確認する。
  4. /etc/apache2/sites-available/default を,以下のように作成する。
    
    NameVirtualHost *:80
    
    <VirtualHost *:80>
    	ServerAdmin postmaster@shitomi.jp
    	DocumentRoot /var/www/error
    	ServerName 61.205.232.19
    </VirtualHost>
    
    <VirtualHost *:80>
    	ServerAdmin postmaster@shitomi.jp
    	DocumentRoot /var/www/www
    	ServerName www.shitomi.jp
    
    	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    	<Directory "/usr/lib/cgi-bin">
    		AllowOverride None
    		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    		Order allow,deny
    		Allow from all
    	</Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
    	ServerAdmin postmaster@shitomi.net
    	DocumentRoot /var/www/www
    	ServerName www.shitomi.net
    </VirtualHost>
    
    <VirtualHost *:80>
    	ServerAdmin postmaster@shitomi.com
    	DocumentRoot /var/www/www
    	ServerName www.shitomi.com
    </VirtualHost>
    
    CustomLog /var/log/apache2/access.log combined
    
  5. 以下のコマンドを実行して, Apache を再起動する。
    $ sudo /etc/init.d/apache2 restart

サイト証明書署名要求と秘密鍵の作成

  1. 以下のコマンドを実行して, Web サーバーのサイト証明書署名要求 (csr.pem) と秘密鍵 (privkey.pem) を作成する。
    $ cd /tmp
    $ openssl req -new -out csr.pem
  2. 続いて,以下のように入力する。
    Using configuration from /usr/share/ssl/openssl.cnf
    Generating a 1024 bit RSA private key
    .......................++++++
    ............++++++
    unable to write 'random state'
    writing new private key to 'privkey.pem'
    Enter PEM pass phrase:[Passwordを入力]
    Verifying password - Enter PEM pass phrase:[Passwordを再入力]
    -----
    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]:JP
    State or Province Name (full name) [Some-State]:Miyagi
    Locality Name (eg, city) []:Sendai
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:Self
    Organizational Unit Name (eg, section) []:[リターンキーを押す]
    Common Name (eg, YOUR name) []:db.shitomi.jp
    Email Address []:[リターンキーを押す]

    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:[リターンキーを押す]
    An optional company name []:[リターンキーを押す]
  3. 以下のコマンドを実行することにより,秘密鍵 (privkey.pem) からパスフレーズを削除した秘密鍵 (apache.key) を作成する。
    $ sudo openssl rsa -in /tmp/privkey.pem -out /etc/ssl/private/apache.key

自己認証局 (プライベート CA ) の構築

  1. 自己認証局(プライベート CA )を構築するため,以下のコマンドを実行する。
    $ sudo rm -rf /usr/lib/ssl/misc/demoCA
    $ cd /usr/lib/ssl/misc/
    $ sudo ./CA.sh -newca
  2. 続いて,以下のように入力する。
    CA certificate filename (or enter to create)
    [リターンキーを押す]
    Making CA certificate ...
    Generating a 1024 bit RSA private key
    ..++++++
    ............++++++
    writing new private key to '../../CA/private/./cakey.pem'
    Enter PEM pass phrase:[パスワードを入力]
    Verifying password - 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]:JP
    State or Province Name (full name) [Some-State]:Miyagi
    Locality Name (eg, city) []:Sendai
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:Self
    Organizational Unit Name (eg, section) []:[リターンキーを押す]
    Common Name (eg, YOUR name) []:192.168.0.1
    Email Address []:[リターンキーを押す]

    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:[リターンキーを押す]
    An optional company name []:[リターンキーを押す]

構築した認証局 (CA) によるサーバー証明書の作成

  1. 以下のコマンドを実行して,構築した CA のルート証明書 (ca.cer) を作成する。
    $ sudo openssl x509 -inform pem -outform der -in /usr/lib/ssl/misc/demoCA/cacert.pem -out /tmp/ca.cer
  2. このルート証明書 (ca.cer) をクライアントにインポートする。
  3. 以下のコマンドを実行して,Webサーバーのサイト証明書署名要求 (csr.pem) から,サーバー証明書 (apache.crt) を作成する。
    $ cd /usr/lib/ssl/misc
    $ sudo openssl ca -out /etc/ssl/certs/apache.crt -infiles /tmp/csr.pem

SSL の利用のための Apache の設定

 443 番ポート (SSL) で Apache が利用できるようにします。

  1. /etc/apache2/ssl ディレクトリを作成する。
  2. /var/www/ssl ディレクトリを作成する。
  3. 以下のコマンドを実行して,アカウント abc が /var/www/ssl ディレクトリに書き込めるように,アクセス権限を変更する。
    $ sudo chown abc:root /var/www/ssl
  4. /etc/apache2/sites-available/ssl を,以下のように作成する。
    
    NameVirtualHost *:443
    
    <Virtualhost *:443>
        ServerAdmin webmaster@localhost
    
        SSLEngine On
        SSLCertificateFile /etc/ssl/certs/apache.crt
        SSLCertificateKeyFile /etc/ssl/private/apache.key
    
        DocumentRoot /var/www/ssl/
        <directory />
            Options FollowSymLinks
            AllowOverride None
        </directory>
    
        <directory /var/www/ssl/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
        </directory>
    </Virtualhost>
    
    CustomLog /var/log/apache2/access-ssl.log combined
    
  5. 以下のコマンドを実行して,モジュールとサイト設定を有効にする。
    $ sudo a2enmod ssl
    $ sudo a2ensite ssl
  6. 以下のコマンドを実行して, Apache を再起動する。
    $ sudo /etc/init.d/apache2 restart
  7. https://db.shitomi.jp にアクセスすることにより,構築した CA 及び Web サーバー (db.shitomi.jp) が,そのクライアントから認証されることを確認する。

(参考)商業認証局からのサーバー証明書の取得

  1. /tmp/csr.pem を,日本ベリサインなどの商業認証局に提出して, Apache のサーバー証明書を発行してもらい,これを apache.crt というファイル名にして, /etc/ssl/certs ディレクトリにコピーする。
  2. 以下のコマンドを実行して, Apache を再起動する。
    $ sudo /etc/init.d/apache2 restart
  3. https://db.shitomi.jp にアクセスすることにより, Apache の動作を確認する。

 このサイトに書かれていることが原因でトラブルが発生しても,私は責任を取ることができません。したがいまして,ここで述べられていることに関する実行は,自己責任でお願いいたします。なお,このサイトはリンクフリーで,リンク時の連絡も不要です。

サイト内関連リンク:なし