0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SSL/TLS(https)を利用してWEBサーバーを運用する

Last updated at Posted at 2020-06-01

この設定ができるまで数時間かかってしまった。
途中まではそこそこ順調でした。

# 認証局を作成
cd /etc/pki/tls/misc/
ls
CA c_hash c_info c_issuer c_name
# CA.shを実行し、対話的に作成
./CA -newca
CA certificate filename (or enter to create)

Making CA certificate ...
Generating a 2048 bit RSA private key
.......................+++
..........................+++
# 中略

            X509v3 Basic Constraints: 
                CA:TRUE
Certificate is to be certified until Jun  1 10:58:08 2023 GMT (1095 days)

Write out database with 1 new entries
Data Base Updated
# 各ディレクトリ内に認証局の秘密鍵と自己署名証明書が作られている
ls /etc/pki/CA/private
# 秘密鍵cakey.pem
cakey.pem
ls /etc/pki/CA
# 自己署名証明書cacert.pem
cacert.pem certs index.txt index.txt.old private careq.pem crl index.txt.attr newcerts
# HTTPSサーバ構築する為のサーバ秘密鍵を作成する
openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...............................+++
....+++
e is 65537 (0x10001)
# 認証局に証明書の発行を要求する為のCSR(証明書発行要求書)を作成する
openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
# 略

ls
CA c_hash c_info c_issuer c_name server.csr server.key
# 認証局が署名を行い、サーバ証明書を作成する
openssl ca -out server.crt -infile /etc/pki/CA/server.csr
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Check that the request matches the signature
Signature ok

# 中略

Certificate is to be certified until Jun  1 11:08:13 2021 GMT (365 days)
Sign the certificate? [y/n]:y
# エラーメッセージが出力された
failed to update database
TXT_DB error number 2
# メッセージをググってみると古いindex.txtを削除し、新たに作成する必要があるとの事
ls /etc/pki/CA
cacert.pem certs index.txt index.txt.old private server.csr careq.pem crl index.txt.attr newcerts
rm /etc/pki/CA/index.*
rm: remove regular file ‘/etc/pki/CA/index.txt’? y
rm: remove regular file ‘/etc/pki/CA/index.txt.attr’? y
rm: remove regular empty file ‘/etc/pki/CA/index.txt.old’? y
touch /etc/pki/CA/index.txt
# サーバ証明書の作成を再実行
openssl ca -out server.crt -infiles /etc/pki/CA/server.csr
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Check that the request matches the signature
Signature ok

# 中略

Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
# 今度は無事作成できた
Write out database with 1 new entries
Data Base Updated

ls
CA c_hash c_info c_issuer c_name server.crt server.key
# サーバ証明書とサーバ秘密鍵をapache指定のディレクトリに移動する
mkdir /etc/httpd/conf/ssl.crt
mkdir /etc/httpd/conf/ssl.key
mv server.crt /etc/httpd/conf/ssl.crt/server.crt
mv server.key /etc/httpd/conf/ssl.crt/server.key

次はapacheの設定ファイル

/etc/httpd/conf/httpd.conf
#  80番ポートをコメントアウトする
# Listen 80
# ここから下部を追記(いらない部分もありそうだが参考書に書いてあるまま)
Listen 443
<VirtualHost _default_:443>
DocumentRoot "/var/www/html"
ServerName host02.knowd.co.jp:443
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key
</VirtualHost>

書き換えた設定ファイルをチェックする

apachectl configtest
httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: Cannot load modules/mod_ssl.so into server: /etc/httpd/modules/mod_ssl.so: cannot open shared object file: No such file or directory
# mod_ssl.soモジュールがない、みたいなエラーがでたのでインストールする
yum install mod_ssl.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ty1.mirror.newmediaexpress.com
 
# 中略

Installed:
  mod_ssl.x86_64 1:2.4.6-93.el7.centos                                          

Complete!

設定も一通り終わったし、さあapacheを立ち上げようとしたここから数時間ハマる。

systemctl start httpd
# 設定後のここのエラー
 (98)Address already in use: AH00072: make_sock: could not bind to addres

httpsで使用するポート(443)が既に使用されているというメッセージのようです。
で、使用しているプロセスを調べてみる

lsof -i :443

使用しているプロセスは存在しない・・・

でググり続けて数時間後、SSLの設定ファイルに原因があるかも・・・?

/etc/httpd/conf.d/ssl.conf
# コメントアウトする(ここのListenディレクティブがhttpd.confのものと重複しているから?)
# Listen 443 https

systemctl start httpd

立ち上がった!!

追記
書き終わって数分後、ふと疑問に思った。

これってアパッチの設定ファイルのListenディレクティブに443を追記しなかったら良かったのでは・・・?

試してみる・・・・・・  やはり!!
参考書通りに追記したが他にもいらない記述は?
試してみる・・・・・・

/etc/httpd/conf/httpd.conf
# これだけで良かった模様
SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key

更に言えば httpd.confには何も追記せずに ssl.confのSSLCertificateFileとSSLCertificateKeyFileディレクティブの内容を変更するだけでも良かった模様です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?