さくらVPSで kobalab.net を運用しているのですが、昨今はhttpsに対応していないと何かと不便です*1。SSL証明書の価格も安くなってきているのでSSLを有効にしました。
かつて mod_ssl の設定 - koba::blog、mod_sslのインストール - koba::blog の要領で「インチキ証明書」を作ってApacheのSSLを有効にしたことはあるのですが、今回はマジメにやります。
さくらVPSの CentOS 7 のデフォルトでは mod_ssl がインストールされないので、追加でインストールします。
$ sudo yum -y install mod_ssl
以下のファイルがインストールされました。
$ rpm -ql mod_ssl /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.modules.d/00-ssl.conf /usr/lib64/httpd/modules/mod_ssl.so /usr/libexec/httpd-ssl-pass-dialog /var/cache/httpd/ssl
商品・価格一覧 | さくらのSSL から認証局を選びます。個人で運用するなら一番安い JPRSのドメイン認証型 で充分です。
認証局にSSL証明書をもらうためには、自分の秘密鍵を作成し、その秘密鍵から CSR(Certificate Signing Request)を作成し、提出する必要があります。JPRSの場合は 設定マニュアル | JPRS に手順がありますのでこれに従います*2。
$ openssl genrsa -aes256 2048 > key.pem
昔は DES を使ったものですが、AES256 が指定されているので従います。パスフレーズの入力を求められるので、適当に入れておきましょう。
$ openssl req -new -key key.pem -out csr.pem -sha256
パスフレーズの入力を求められるので、先ほどのを入力します。
続いてサーバー識別名(DN)情報の入力を求められるので、適切に入力します。一番大切なのは Common Name です。これを間違えると意味がないので、対象となるサーバの FQDN を正しく入力しましょう。Email Address、A challenge password、An optional company name は入力してはいけません。
Country Name (2 letter code) [XX]:JP State or Province Name (full name) []:Tokyo Locality Name (eg, city) [Default City]:Hino-shi Organization Name (eg, company) [Default Company Ltd]:kobalab Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []:kobalab.net Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
できあがった csr.pem を使って SSLサーバ証明書お申し込み|さくらインターネット から申請します。
JPRSの証明書の場合、中間CA証明書もインストールしておかないと Firefox でエラーになります*3。証明書・中間CA証明書について | JPRS からタイプ(DV/OV)と有効期間が適切なものを選んでダウンロードしておきましょう。
申請の際に本人確認(申請者がサイト管理者であることの確認)のために、認証ファイルのアップロードを要求されます。さくらからメールで通知が来るのでその手順に従い認証ファイルを取得し、サーバの所定の位置にアップロードします*4。認証局では指定したファイルが指定した場所に置かれていることで本人確認するようです。
認証ファイルが確認され認証局での作業が完了すると、さくらからメールで通知がきますので、それに従いSSL証明書をダウンロードします*5。
必要なファイルはそろったので、Apacheに設定します。ファイルは以下に配置しました*6。
秘密鍵 | /etc/httpd/ssl/key.pem |
---|---|
中間CA証明書 | /etc/httpd/ssl/chain.pem |
SSL証明書 | /etc/httpd/ssl/cert.pem |
mod_ssl インストールの際に /etc/httpd/conf.modules.d/00-ssl.conf で設定済みです。
mod_ssl インストールの際に /etc/httpd/conf.d/ssl.conf が設置されるので、これを修正します。
===================================================================
RCS file: ssl.conf,v
retrieving revision 1.1
diff -u -r1.1 ssl.conf
--- ssl.conf 2020/08/16 09:32:06 1.1
+++ ssl.conf 2020/08/16 16:01:58
@@ -57,7 +57,7 @@
# General setup for the virtual host, inherited from global configuration
#DocumentRoot "/var/www/html"
-#ServerName www.example.com:443
+ServerName kobalab.net:443
# Use separate log files for the SSL virtual host; note that LogLevel
# is not inherited from httpd.conf.
@@ -97,14 +97,14 @@
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that a kill -HUP will prompt again. A new
# certificate can be generated using the genkey(1) command.
-SSLCertificateFile /etc/pki/tls/certs/localhost.crt
+SSLCertificateFile /etc/httpd/ssl/cert.pem
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
-SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
+SSLCertificateKeyFile /etc/httpd/ssl/key.pem
# Server Certificate Chain:
# Point SSLCertificateChainFile at a file containing the
@@ -113,7 +113,7 @@
# the referenced file can be the same as SSLCertificateFile
# when the CA certificates are directly appended to the server
# certificate for convinience.
-#SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt
+SSLCertificateChainFile /etc/httpd/ssl/chain.pem
# Certificate Authority (CA):
# Set the CA certificate verification path where to find CA
@@ -215,3 +215,8 @@
</VirtualHost>
+<IfModule mod_rewrite.c>
+RewriteEngine On
+RewriteCond %{HTTPS} off
+RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
+</IfModule>
ServerName
に FQDN を設定mod_rewrite
で非httpsへのアクセスを強制的にhttpsへリダイレクトの3つを設定しています。
デフォルトではSSLのポート(443)は公開されていないので、以下のコマンドで公開します。
$ sudo firewall-cmd --permanent --add-service=https $ sudo firewall-cmd --reload
Apacheを再起動します。
$ sudo systemctl restart httpd.service
秘密鍵のパスフレーズを求められるので入力しましょう。
Apache 起動の際にパスフレーズが必要だとサーバ再起動の際などに不便です。以下のコマンドでパスフレーズを取り除くことができます。
$ sudo openssl rsa -in key.pem -out key.pem