[Memo2018][ITPASS]Apache のインストールと設定 Let's encrypt による証明書の設定

SSL 用の証明書の作成

openssl の設定を変更

/etc/ssl/openssl.cnf について以下の二点の変更を加えた.

  • [usr_cert] セクションにある nsCertType=server のコメントアウトを外した.
  • [v3_ca] セクションにある nsCertType=sslCA,emailCA のコメントアウトを外した.

Let's encrypt による証明書の設定

以前はプライベート証明書を用いていたが, 現在は Let's encript (https://letsencrypt.org/) を利用した 証明書の設定を行った.

Let's Encrypt 総合ポータル (https://letsencrypt.jp/) の Let's Encrypt の使い方 (https://letsencrypt.jp/usage/) に従って作業した.

作業用ディレクトリの作成

作業ディレクトリを作成する.

$ sudo -s
# cd /usr/local/src
# mkdir certbot
# cd certbot

certbot のダウンロード

Let's Encrypt では, Debian 用に certbot のパッケージが用意 されている. しかし, このパッケージは apache2 の Debian パッ ケージに依存しているため, apache をソースから手動で make している itpass サーバでは使えない. そのため, https://certbot.eff.org/ から, 下のようにして certbot をダウンロードしてインストールする.

"Software" に apache を選択.
"System" に "Other UNIX" を選択.

当該ページの説明に従って, 下のように作業した.

# wget https://dl.eff.org/certbot-auto
# chmod a+x certbot-auto

証明書の発行

ダウンロードした certbot-auto を使って itpass.scitec.kobe-u.ac.jp 用の 証明書を発行した.

# ./certbot-auto --webroot certonly \
  --email itpadmin@itpass.scitec.kobe-u.ac.jp \
  --webroot-path /home/itpass/public_html \
  -d tako-itpass.scitec.kobe-u.ac.jp

同様に, epa.scitec.kobe-u.ac.jp, aoe.scitec.kobe-u.ac.jp 用の証明書を発行する.

# ./certbot-auto --webroot certonly \
  --email itpadmin@itpass.scitec.kobe-u.ac.jp \
  --webroot-path /home/epalab/public_html \
  -d epa.scitec.kobe-u.ac.jp
# ./certbot-auto --webroot certonly \
  --email itpadmin@itpass.scitec.kobe-u.ac.jp \
  --webroot-path /home/aoelab/public_html \
  -d aoe.scitec.kobe-u.ac.jp

どれも /home 以下のディレクトリが存在せず, エラーが出たので /home 以下に/itpass/public_html , /aoelab/public_html , /epalab/public_html を作成した.

もう一度 ./certbot-auto を itpass aoe epa に対して行うと, 下のように聞かれるた.

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must

agree in order to register with the ACME server at

https://acme-v01.api.letsencrypt.org/directory

-------------------------------------------------------------------------------
(A)gree/(C)ancel:

A とした.

さらに下のように聞かれた.

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.

-------------------------------------------------------------------------------
(Y)es/(N)o:

N とした

なお, certbot-auto のオプションの意味は下の通り.

  • --webroot 既に動作中のウェブサーバ上で, ウェブサーバを停止せずに証明書を発行する.
  • certonly apache の設定ファイルを自動で書き換えずに証明書を取得するのみ. 証明書は /etc/letsencrypt に置かれる.
  • --webroot-path RootPath の設定
  • --email 初回実行時にメールアドレスを尋ねられなくするためのオプション. つけなくて良かったような気がする.

apache への証明書の設定

/usr/local/apache2/conf/extra/httpd-ssl.conf を編集して発行した証明書を設定する.

# vi /usr/local/apache2/conf/extra/httpd-ssl.conf

編集箇所は下の通り.

ServerName itpass.scitec.kobe-u.ac.jp:443 の項目内で下のように書き換えた.

SSLCertificateFile "/etc/letsencrypt/live/itpass.scitec.kobe-u.ac.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/itpass.scitec.kobe-u.ac.jp/privkey.pem"

ServerName aoe.scitec.kobe-u.ac.jp:442 の項目内で下のように書き換えた.

SSLCertificateFile "/etc/letsencrypt/live/aoe.scitec.kobe-u.ac.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/aoe.scitec.kobe-u.ac.jp/privkey.pem"

ServerName epa.scitec.kobe-u.ac.jp:443 の項目内で下のように書き換えた.

SSLCertificateFile "/etc/letsencrypt/live/epa.scitec.kobe-u.ac.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/epa.scitec.kobe-u.ac.jp/privkey.pem"

証明書更新のテスト

Let's encrypt の証明書の有効期限は 90 日である. このため, certbot-auto には簡単に証明書を更新する方法が存在する. その方法を設定する.

まずは下のようにして更新をテストした.

# ./certbot-auto renew --dry-run

更新されたかを確認するため /var/log/letsencrypt/letsencrypt.log の中身を確認した.

証明書の更新を定期的に実行するために cron のためのスクリプトを設定する. 下の内容の /etc/cron.local/daily/240_letsencrypt_renew を作成した.

#!/bin/sh
#
#= Renew certificate
#
MAINTAINERS="Yoshiyuki O. Takahashi"
UPDATE='2018-04-25'
VERSION='1.0'
REFERENCES='none'

COMMAND='/usr/local/src/certbot/certbot-auto'
DRYRUN='--dry-run'
DRYRUN=''

$COMMAND renew $DRYRUN

実行権限を与えた.

下のように動作テストを行い, 問題なく動作することを確認した. (ただし, この時点では証明書が新しいため更新されなかった.)

# /etc/cron.local/daily/240_letsencrypt_renew

参考ページ

動作チェック

  • apache を起動
    • apache は /usr/local/apache2/bin/apachectl で起動/停止する

      # /usr/local/apache2/bin/apachectl -k start
    • 起動したかどうかを以下のコマンドで確認した.

      # ps aux | grep http

      ここで apache が起動できていなかったため, 画面左端に www-data と表示は出なかった.

    • apache のエラーログ /var/log/httpd-error.log を確認すると, 末尾に

      [Tue Nov 06 17:19:09.874475 2018] [ssl:emerg] [pid 1734:tid 
      140226041945728] AH02311: Fatal error initialising mod_ssl, exiting. 
      See /var/log/httpd-ssl-error.log for more information

      と記述されていた.

SSL 用の証明書の作成 (参照 : 2016 年度サーバ構築ログ)

従来の方法で SSL 証明書を作成することにした.

プライベート CA の作成

apache の config ディレクトリに入り, 次のように証明書を格納するディレク トリを作成した.

# cd /usr/local/apache2/conf
# mkdir ca
# cd ./ca/
# mkdir {itpass,epa,aoe のそれぞれの名称で作成}

以下の作業は ServerName : itpass.scitec.kobe-u.ac.jp, epa.scitec.kobe-u.ac.jp, aoe.scitec.kobe-u.ac.jp のそれぞれについて行った. サーバ名を適宜読みかえ, 3 回実行する.

< 3 回実行した作業 ここから>

秘密鍵を生成した.

# openssl genrsa -out itpass.key 2048

公開鍵を生成した(SHA-2(256)形式).

# openssl req -new -sha256 -key itpass.key -out itpass.csr

Country Name (2 letter code) [AU]: JP
State or Province Name (full name) [Some-State]: Hyogo
Locality Name (eg, city) []: Kobe
Organization Name (eg, company) [Internet Widgits Pty Ltd]: Kobe
University
Organizational Unit Name (eg, section) []: ITPASS (epa, aoe のときはそれぞれ "EPA lab.", "AOE lab." として登録)
Common Name (eg, YOUR name) : itpass.scitec.kobe-u.ac.jp (ここは
itpass の他に, epa, aoe の分をそれぞれ作成する)
Email Address: itpadmin_at_itpass.scitec.kobe-u.ac.jp (ここは同じ)
A challenge password: (空で Enter)
A optional company name: (空で Enter)

自己署名証明書を作成.

# openssl x509 -req -in itpass.csr -signkey itpass.key -out itpass.crt

生成された鍵や証明書をディレクトリに格納した.

< 3 回実行した作業 ここまで>

ここまでを itpass, epa, aoe のそれぞれについて計 3 回行った.

apache への証明書の設定

/usr/local/apache2/conf/extra/httpd-ssl.conf を編集して発行した証明書を設定する.

# vi /usr/local/apache2/conf/extra/httpd-ssl.conf

編集箇所は下の通り.

ServerName itpass.scitec.kobe-u.ac.jp:443 の項目内で下のように書き換えた.

SSLCertificateFile "/usr/local/apache2/conf/ca/itpass/itpass.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/ca/itpass/itpass.key"

ServerName aoe.scitec.kobe-u.ac.jp:442 の項目内で下のように書き換えた.

SSLCertificateFile "/usr/local/apache2/conf/ca/aoe/aoe.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/ca/aoe/aoe.crt"

ServerName epa.scitec.kobe-u.ac.jp:443 の項目内で下のように書き換えた.

SSLCertificateFile "/usr/local/apache2/conf/ca/epa/epa.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/ca/epa/epa.crt"

動作チェック

  • apache を起動させる

    # /usr/local/apache2/bin/apachectl -k start
    • 起動したかどうかを以下のコマンドで確認した.

      # ps aux | grep http

      画面左端に www-data と表示され, 起動できていることを確認した.

  • ~itpass/public_html/ に test という名前のディレクトリを作成し, その中に test.txt というファイルを作成した.
  • ブラウザで test.txt が正常に表示されるかどうかを確認する.
    • URL は http://tako-itpass.scitec.kobe-u.ac.jp/test/test.txt, https://tako-itpass.scitec.kobe-u.ac.jp/test/test.txt
      • "http://..." と "https://..." の両方に関して行った.
  • apache を停止

    # /usr/local/apache2/bin/apachectl -k graceful-stop
    • 停止したかどうかを確認した.
    • apache のエラーログ /var/log/httpd-error.log を確認し, エラーなどが出ていないことを確認した.

起動スクリプトの用意

  • サーバの起動時に apache が起動させるための起動スクリプトを用意する.
    • Debian 8 から導入された systemd による起動設定を行った.

具体的には, <URL:http://www.smiyasaka.com/CentOS7.0_server21.html> を参考に, /etc/systemd/system に apache2.service を作成した.

chikuwa2@tako-itpass /etc/systemd/system
% cat apache2.service
[Unit]
Description=httpd-2.4.37 daemon

[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start

[Install]
WantedBy=multi-user.target
  • 起動テストを行い, 起動を確認した.

    $ sudo systemctl start apache2
    $ ps aux | grep httpd
  • システム起動時に apache が立ち上がるようにするために追加設定を行った.

    $ sudo systemctl enable apache2
  • 再起動して apache が起動するかを確認した.
    • 起動していることを確認した.

Let's encrypt による証明書の設定

サーバ交換作業後に Let's encrypt による証明書の設定をしなおした.

Let's Encrypt 総合ポータル (https://letsencrypt.jp/) の Let's Encrypt の使い方 (https://letsencrypt.jp/usage/) に従って作業する.

作業用ディレクトリの作成

作業ディレクトリを作成する.

$ sudo -s
# cd /usr/local/src
# mkdir certbot
# cd certbot

すでにサーバ再構築時に完了していた作業については, 行わなかった.

certbot のダウンロード

Let's Encrypt では, Debian 用に certbot のパッケージが用意 されている. しかし, このパッケージは apache2 の Debian パッ ケージに依存しているため, apache をソースから手動で make している itpass サーバでは使えない. そのため, https://certbot.eff.org/ から, 下のようにして certbot をダウンロードしてインストールする.

"Software" に apache を選択.
"System" に "Other UNIX" を選択.

当該ページの説明に従って, 下のように作業する.

# wget https://dl.eff.org/certbot-auto
# chmod a+x certbot-auto

証明書の発行

ダウンロードした certbot-auto を使って itpass.scitec.kobe-u.ac.jp 用の 証明書を発行する.

# ./certbot-auto --webroot certonly \
  --email itpadmin@itpass.scitec.kobe-u.ac.jp \
  --webroot-path /home/itpass/public_html \
  -d itpass.scitec.kobe-u.ac.jp

なお, certbot-auto のオプションの意味は下の通り.

  • --webroot 既に動作中のウェブサーバ上で, ウェブサーバを停止せずに証明書を発行する.
  • certonly apache の設定ファイルを自動で書き換えずに証明書を取得するのみ. 証明書は /etc/letsencrypt に置かれる.
  • --webroot-path RootPath の設定
  • --email 初回実行時にメールアドレスを尋ねられなくするためのオプション. つけなくて良かったような気がする.

同様に, epa.scitec.kobe-u.ac.jp, aoe.scitec.kobe-u.ac.jp 用の証明書を発行する.

# ./certbot-auto --webroot certonly \
  --email itpadmin@itpass.scitec.kobe-u.ac.jp \
  --webroot-path /home/epalab/public_html \
  -d epa.scitec.kobe-u.ac.jp
# ./certbot-auto --webroot certonly \
  --email itpadmin@itpass.scitec.kobe-u.ac.jp \
  --webroot-path /home/aoelab/public_html \
  -d aoe.scitec.kobe-u.ac.jp

apache への証明書の設定

/usr/local/apache2/conf/extra/httpd-ssl.conf を編集して発行した証明書を設定した.

# vi /usr/local/apache2/conf/extra/httpd-ssl.conf

編集箇所は下の通り.

ServerName itpass.scitec.kobe-u.ac.jp:443 の項目内で下のように書き換えた.

SSLCertificateFile "/etc/letsencrypt/live/itpass.scitec.kobe-u.ac.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/itpass.scitec.kobe-u.ac.jp/privkey.pem"

ServerName aoe.scitec.kobe-u.ac.jp:442 の項目内で下のように書き換えた.

SSLCertificateFile "/etc/letsencrypt/live/aoe.scitec.kobe-u.ac.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/aoe.scitec.kobe-u.ac.jp/privkey.pem"

ServerName epa.scitec.kobe-u.ac.jp:443 の項目内で下のように書き換えた.

SSLCertificateFile "/etc/letsencrypt/live/epa.scitec.kobe-u.ac.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/epa.scitec.kobe-u.ac.jp/privkey.pem"

証明書更新のテスト

Let's encrypt の証明書の有効期限は 90 日である. このため, certbot-auto には簡単に証明書を更新する方法が存在する. その方法を設定する.

まずは下のようにして更新をテストする.

# ./certbot-auto renew --dry-run

これを定期的に実行するために cron のためのスクリプトを設定する. 下の内容の /etc/cron.local/daily/240_letsencrypt_renew を作成した.

#!/bin/sh
#
#= Renew certificate
#
MAINTAINERS="Yoshiyuki O. Takahashi"
UPDATE='2018-04-25'
VERSION='1.0'
REFERENCES='none'

COMMAND='/usr/local/src/certbot/certbot-auto'
DRYRUN='--dry-run'
DRYRUN=''

$COMMAND renew $DRYRUN

実行権限を与えた.

下のように動作テストを行い, 問題なく動作することを確認した. (ただし, この時点では証明書が新しいため更新されなかった.)

# /etc/cron.local/daily/240_letsencrypt_renew

動作チェック

  • apache を停止

    # /usr/local/apache2/bin/apachectl -k graceful-stop
    • 停止したかどうかを確認する.
  • apache を起動
    • apache は /usr/local/apache2/bin/apachectl で起動/停止する

      # /usr/local/apache2/bin/apachectl -k start
    • 起動したかどうかを以下のコマンドで確認した.

      # ps aux | grep http

      画面左端に www-data と表示されている行があることを確認した.

  • ブラウザで https://itpass.scitec.kobe-u.ac.jp にアクセスし, ページを正常に見ることができることを確認した.
Last modified:2018/12/04 17:58:50
Keyword(s):
References:[[ITPASS2018]2018年度サーバ構築ログ]