1. はじめに
ApacheにSSL自己証明書を設定した時に詰まった部分があったので、備忘録として残します。
2. 証明書の作成
まず、以下コマンドで証明書を発行。
openssl ecparam -name prime256v1 -genkey -out server.key
openssl req -new -key server.key > server.csr
openssl ca -in server.csr -out server.crt
作成した証明書、秘密鍵を移動させます。
mv server.crt /etc/httpd/conf/ssl.crt/server.crt
mv server.key /etc/httpd/conf/ssl.key/server.key
3. 設定の変更
証明書、秘密鍵の保存場所を指定します。
vi /etc/httpd/conf.d/ssl.conf
--snip--
<VirtualHost *:443>
--snip--
# Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate. If
# 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/conf/ssl.crt/server.crt
# 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/conf/ssl.key/server.key
--snip--
4. Apacheの再起動
再起動するとエラーが発生。
systemctl restart httpd.service
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
journalctl -xe を見ても何も情報がない。。。
/var/log/httpd/error_log を確認すると Permission 関連のエラーが。
どうやらSELINUXが悪さしているみたい。
/var/log/httpd/error_log
[Thu Apr 02 10:02:29.534751 2020] [ssl:emerg] [pid 19565] AH02312: Fatal error initialising mod_ssl, exiting.
[Thu Apr 02 10:02:33.453638 2020] [core:notice] [pid 19576] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Thu Apr 02 10:02:33.455370 2020] [suexec:notice] [pid 19576] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Thu Apr 02 10:02:33.455600 2020] [ssl:emerg] [pid 19576] (13)Permission denied: AH02201: Init: Can't open server certificate file /etc/httpd/conf/ssl.crt/wild_server.crt
調べてみるとSELinuxのセキュリティポリシーで各ファイルに適切なラベルが付与されていないとダメらしい。
ラベルは「ll -Z」コマンドで確認できる。
[root@149_centos ssl.key]# ll -Z
-rwxrwxrwx. hoge hoge unconfined_u:object_r:user_home_t:s0 wild_server.key
これを「restorecon」コマンドで適切なラベルを付け直す。
[root@149_centos ssl.key]# restorecon wild_server.key
[root@149_centos ssl.key]#
[root@149_centos ssl.key]# ll -Z
-rwxrwxrwx. hoge hoge unconfined_u:object_r:httpd_config_t:s0 wild_server.key
結果、「systemctl restart httpd.service」コマンドで正常に再起動しSSL化が出来た。