midori

[Vagrant]複数サイトがSSLに対応した環境を構築する

Vagrant で 複数ホスト名(VirtualHost)とSSLにも対応したLAMP環境を作成しました。
コードは GitHub で公開しています。

SSLのサーバー設定は、SSL設定ファイル(ssl.conf)を準備し鍵を付けた暗号化しSSL証明書を発行してサーバーに設定します。

記事ではLAMP環境と複数サイトのSSLの設定(SSL自己証明書)を追加する方法を紹介します。

LAMP環境

tam-vagrant-master2
https://github.com/tam-inc/tam-vagrant-master2

GitHub から今回準備したVagrantをダウンロードすると、testdomain.local がすでに含まれています。
hostsファイルにドメインとprivate_network IPと紐づければすぐ利用できます。
https://testdomain.local/

SSLを有効化をする具体的な設定は下記で調整済です。
[CentOS]複数のSSL設定をする方法

下記の環境で動作確認済です。(2015/12 現在)
OS Windows 7
Vagrant 1.8.1
VirtualBox 5.0.12

サーバー構成
CentOS 6.5
PHP 5.5.31
mysql 5.5
その他 git composer mod_ssl

複数サイトのSSLの設定を追加する

Apacheは/etc/httpd/conf.d以下に配置された「.conf」ファイルをhttpd.confにインクルードします。
SSLはドメイン毎に必要なのでドメイン毎にファイルを作成します。
vagrant ssh でサーバーへroot ログインし証明書とSSL設定ファイル(ssl.conf)が5ステップで作成できます。

自己証明書(newdomain.localを作る)

step1)SSL証明書の鍵を作る

 
cd /etc/pki/tls/
openssl md5 /var/log/messages* >rand.dat
openssl genrsa -rand rand.dat -des3 2048 > ./private/newdomain.key
 57 semi-random bytes loaded
 Generating RSA private key, 2048 bit long modulus
 ..................+++
 .............................................................................................................+++
 e is 65537 (0x10001)
 Enter pass phrase: [パスワード入力]
 Verifying - Enter pass phrase:[再度パスワード入力]
 ```

ここでは、/etc/pki/tls/rand.dat に作成できればOKです。
パスワードはわすれないように。step2で使います。
(テスト環境の場合、パスワードを忘れなければこの/etc/pki/tls/rand.datを使いまわしで
次のドメインからは、step2から始めてもOKです。)

step2)鍵を使って証明書発行要求(Certificate Signing Request)を作成

※Enterで決定、次の入力へ移る

openssl req -new -key ./private/newdomain.key -out ./newdomain.csr
Enter pass phrase for /etc/pki/tls/private/testdomain.key:[上で入力したパスワード]
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) [XX]:JP
State or Province Name (full name) []:Osaka
Locality Name (eg, city) [Default City]:Kita-ku
Organization Name (eg, company) [Default Company Ltd]:TAM
Organizational Unit Name (eg, section) []:system
Common Name (eg, your name or your server's hostname) []:newdomain.local #サイトのドメインに合わせる
Email Address []:[何も入力しない]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: [何も入力しない]
An optional company name []:[何も入力しない]

入力は適当で大丈夫です。
※Common Name は 設定するサイトのドメインに合わせないといけません。

step3)証明書を発行

 
openssl req -x509 -days 3650 -in newdomain.csr -key private/newdomain.key -out ./newdomain.crt Enter pass phrase for private/newdomain.key:[秘密鍵のパスフレーズを入力]

step4)SSL設定ファイルを作る

1)ssl_testdomain.confをコピーしてドメイン変更します。

 
cp /etc/httpd/conf.d/ssl_testdomain.conf /etc/httpd/conf.d/ssl_newdomain.conf 
vi /etc/httpd/conf.d/ssl_newdomain.conf

2)次の個所を書き換えます。(ドメインを設定します。)
・証明書ファイルの指定
SSLCertificateFile /etc/pki/tls/newdomain.crt

・秘密鍵ファイルの指定
SSLCertificateKeyFile /etc/pki/tls/private/newdomain.key

・VirtualHostの指定
ServerName newdomain.local
DocumentRoot /vagrant/sites/newdomain/www

step5)起動の際に秘密鍵のパスフレーズを聞かれない設定

パスフレーズの聞かれない鍵を作成しSSL設定ファイルを再び編集します。

 
openssl rsa -in private/<code>newdomain</code>.key -out private/newdomain-nopass.key
vi /etc/httpd/conf.d/ssl_newdomain.conf

・秘密鍵ファイルの指定を変更
SSLCertificateKeyFile /etc/pki/tls/private/newdomain-nopass.key

・設定反映

 
service httpd restart

最後に

Vagrant up の途中で失敗する時はこんな時
 ・Vagrant と VirtualBox のどちらかだけバージョンアップした時
 ・invalid byte sequence in UTF-8 (ArgumentError) っていうエラー
   →bootstrap.sh に日本語が入っていて文字コードがSJISの時。
    コメントに日本語を使わないでSJISにするか、UTF-8にする。

皆様のご参考になりますように。

新しいウェブ体験を作ろう TAMのPWA開発
お問い合わせはこちら