Debian 10 (buster) - Web サーバ Nginx で SSL 接続!

Updated:


Debian GNU/Linux 10 (buster) に構築した Web サーバ Nginx で SSL 接続するための設定についての記録です。

以前古いバージョンでの作業時に残していた記録を参考に作業を行い、今回更新した作業記録を貼付する形式の内容となっています。
(当然ながら、興味がなければスルーしてください)

0. 前提条件

  • Debian GNU/Linux 10 (buster) での作業を想定。
  • クライアント側は LMDE 3 (Linux Mint Debian Edition 3; 64bit) を想定。
  • サーバ用途なので、作業は基本的に全て一般ユーザから root になって行う。
  • Web(HTTP)サーバ Nginx が「Debian 10 (buster) - Web サーバ Nginx 構築(Nginx 公式リポジトリ使用)!」の方法で導入済みであることを想定。
  • 今回は SSL サーバ証明書は自分で作成するが、後日、Let’s Encrypt で無料で証明書を取得するようにする。
  • root ユーザでの作業を想定。

1. SSL 証明書の作成

openssl 未インストールならインストール。

# apt -y install openssl

秘密鍵生成。

# cd /etc/ssl/private
# 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:  # <= 確認再入力

秘密鍵からパスフレーズを削除。

# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:              # <= パスフレーズ
writing RSA key

# openssl req -new -days 3650 -key server.key -out server.csr
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) [AU]:JP                                    # <= 国の略名
State or Province Name (full name) [Some-State]:Shimane                 # <= 都道府県名
Locality Name (eg, city) []:Matsue                                      # <= 市区町村名
Organization Name (eg, company) [Internet Widgits Pty Ltd]:mk-mode.com  # <= サイト名(何でもよい)
Organizational Unit Name (eg, section) []:                              # <= 部署名(空でよい)
Common Name (e.g. server FQDN or YOUR name) []:www.mk-mode.com          # <= ホスト名
Email Address []: webmaster@mk-mode.com                                 # <= 管理者メールアドレス
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:                                                # <= 追加属性・空エンターでよい
An optional company name []:                                            # <= 追加属性・空エンターでよい

# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
Signature ok
subject=/C=JP/ST=Hiroshima/L=Hiroshima/O=GTS/OU=Server World/CN=www.server.world/emailAddress=xxx@server.world
Getting Private key

権限選定。

# chmod 400 server.*

2. Nginx 設定ファイルの編集

以下のように、Nginx 設定ファイルを作成する。
(Nginx のインストール方法やバージョンによっては設定ファイルの構成が異なるので注意。以下は「Debian 10 (buster) - Web サーバ Nginx 構築(Nginx 公式リポジトリ使用)!」の方法でインストールした場合)

File: /etc/nginx/conf.d/ssl.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# HTTPS server
#
server {
    listen       443 ssl;
    server_name  localhost;

    #ssl_certificate      cert.pem;
    ssl_certificate      /etc/ssl/private/server.crt;
    #ssl_certificate_key  cert.key;
    ssl_certificate_key  /etc/ssl/private/server.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   html;
        index  index.html index.htm;
    }
}
  • 設定ファイルの文法をチェックするには nginx -t を実行する

3. Nginx の再起動

# systemctl restart nginx

4. ファイアウォール(ufw)の設定

ポート TCP 443 を開放する。

# ufw allow 443/tcp
Rule added

# ufw status
    :
443/tcp                    ALLOW       Anywhere
    :

6. 動作確認

ブラウザから https://<サーバ名orIPアドレス>/ にアクセスし、セキュリティに関するページが表示されたら適宜インストールする。
ブラウザにより異なるが、Firefox なら「危険を理解した上で接続するには」をクリック後「例外追加」ボタンをクリックする。そして開いたウィンドウ内で「セキュリティ例外を追加」ボタンをクリックする。
認証に成功すると以下のように表示される。

7. 参考サイト


以上。





 

Sponsored Link

 

Comments