Apache|秘密鍵や公開鍵を生成しHTTPSを有効化

Apache
この記事は約6分で読めます。

最近、仕事等で忙しかったので今日久しぶりに自宅サーバを触ったのですが、前回までなにをどこまでやっていたのか記憶がほんとに吹き飛んでいます(笑)。

いろいろ触っていてやっと思い出しました。Apacheの HTTPS有効化(HTTP over SSL/TLS)をやらねばいけなかったで、今日はその構築メモを記載します。

1.mod_sslがインストールされているか確認

まず、mod_ssl が自サーバに組み込まれているかを以下のコマンドで確認します。
# httpd -M | grep ssl

#

はい、組み込まれていませんね。。
前回、どこまで作業をやったのか全く思い出せないので最初のconfigureから始めます。。

prefixは指定しなくてもデフォルトが以下の指定した場所なので除外してもいいのですが、念のため指定。mod_sslモジュールをインストールします。

# ./configure –prefix=/usr/local/apache2 –enable-mods-shared=all

# make

# make install

※元々、Apacheをバイナリーでインストールされた場合は、以下のコマンドを実行してください。
# yum install mod_ssl

あと、httpd.conf内の LoadModule ssl_module modules/mod_ssl.so のコメントを解除しておくこと。

2.秘密鍵、公開鍵、サーバ証明書を作成します。

以下のopensslコマンドで、server.key(秘密鍵)を生成します。
パスフレーズを聞いてきますので、適宜入力します。
# openssl genrsa -aes128 1024 > server.key
Generating RSA private key, 1024 bit long modulus
……++++++
………………………………………++++++
e is 65537 (0x10001)
Enter pass phrase:    ※パスフレーズを入力します。
Verifying – Enter pass phrase:
#

# ls
server.key
#

server.keyが作成されたことを確認します。
続いて、server.csr(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) []:TOKYO
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:www.purpledice.jp
Email Address []:postmaster@purpledice.jp

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:      ※パスフレーズを入力します。
An optional company name []:    ※パスフレーズを入力します。
#

# ls
server.csr  server.key
#

server.csrが生成されたことを確認します。

続いて、server.crt(サーバ証明書)を生成します。
# openssl x509 -in server.csr -days 3650 -req -signkey server.key > server.crt
Signature ok
subject=/C=JP/ST=TOKYO/L=Default City/O=Default Company Ltd/CN=www.purpledice.jp/emailAddress=postmaster@purpledice.jp
Getting Private key
Enter pass phrase for server.key: ※パスフレーズを入力します。
#

3.続いて、Apacheの設定を修正します。

まず、/usr/local/apache2/conf/httpd.confの以下の箇所を有効にします。
<変更前>
# Secure (SSL/TLS) connections
# Include conf/extra/httpd-ssl.conf

<変更後>
# Secure (SSL/TLS) connections
Include conf/extra/httpd-ssl.conf

また、を以下の通り修正します。
・適宜修正
<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot “/usr/local/apache2/htdocs”
ServerName www.purpledice.jp:443
ServerAdmin postmaster@purpledice.jp
ErrorLog “/usr/local/apache2/logs/error_log”
TransferLog “/usr/local/apache2/logs/access_log”

・サーバ証明書を指定
SSLCertificateFile “/usr/local/apache2/conf/server.crt”

・秘密鍵の指定
SSLCertificateKeyFile “/usr/local/apache2/conf/server.key”

修正が完了したらApacheを再起動します。
# /usr/local/apache2/bin/apachectl restart

4.httpsのURLに接続し、HTTPS化されていることを確認します。

<補足>
Apache起動時にパスフレーズを入力しなくても起動するようにする。
# mv server.key server.key.org
# openssl rsa -in server.key.org >server.key
Enter pass phrase for server.key.org:   ※パスフレーズを入力します。
writing RSA key
#

あっ!Firewallで443portをopenすることも忘れずに!

コメント

タイトルとURLをコピーしました