思ったより簡単!ApacheでHTTPS接続ができるまで

最近のWebサービスでは、全ユーザに対して常時HTTPS接続を提供するものが増えてきました。
サーバにSSL設定を行うことで、Webサーバとブラウザ間の通信が暗号化され安全な通信を行うことができます。

今回は、LinuxサーバにApacheがインストールされ、HTTP接続ができる状態であるところから説明します。

検証環境

AWS(Amazon Web Service) - EC2

$ cat system-release
Amazon Linux AMI release 2011.02.1.1 (beta)

既にApacheはインストール済みの状態てす。

ApacheSSLモジュールをインストール

# yum -y install mod_ssl

opensslのバージョン確認

# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013

秘密鍵を生成

# cd /etc/pki/tls/certs/
# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.........+++
............+++
e is 65537 (0x10001)
Enter pass phrase for server.key:[←パスワード入力]
Verifying - Enter pass phrase for server.key:[←パスワード入力]

CSRを生成

# openssl req -new -key server.key > server.csr
Enter pass phrase for server.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) []: [←都道府県 ex. Miyagi]
Locality Name (eg, city) [Default City]: [←市区町村 ex. Sendai]
Organization Name (eg, company) [Default Company Ltd]: [←会社名]
Organizational Unit Name (eg, section) []: [←空でEnter]
Common Name (eg, your name or your server''s hostname) []: [←重要! コモンネーム]
Email Address []: [←メールアドレス ex. xxx@xxx.xx]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: [←空でEnter]
An optional company name []: [←空でEnter]

CSRを表示

# less server.csr 

-----BEGIN CERTIFICATE REQUEST-----
[]
-----END CERTIFICATE REQUEST-----

これをコピーしてSSL証明書の購入時に使用します。

RapidSSLを購入

今回は、SSLボックスで、RapidSSLを購入しました。

代理店がたくさんあり、どこで購入するか迷うところですが、安くてカード決済ができたのでSSLボックスにしました。

購入画面で、先程のCSRを入力(BEGIN CERTIFICATE REQUEST、END CERTIFICATE REQUESTの行も含む)します。
購入から5分程度で、RapidSSL証明書がメールで送られてきます。早い!

メールで送られてきた証明書をコピー

# pwd
/etc/pki/tls/certs
# vi server.crt
-----BEGIN CERTIFICATE-----
[]
-----END CERTIFICATE-----
パスフレーズ応答を削除
# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:[←パスワード入力]
writing RSA key
Apacheの設定
# vi /etc/httpd/conf.d/ssl.conf

[以下の行を追加します]
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/certs/server.key
ServerName bringup.csdev.co.jp:443