この疑問に回答します。
ここ数年で、Let’s Encryptを利用することに抵抗感がなくなってきたのですかね。情報が増えてきています。今回は王道です。Apacheを利用してSSL化させる手順をまとめます。
Let’s Encrypt(レッツ・エンクリプト)とは
Let’s Encrypt は、フリーで自動化されたオープンな認証局 (certificate authority; CA) です。Internet Security Research Group; (ISRG) がサービスを提供しています。
Let’s Encryptは、名だたるIT企業がスポンサーになっており、安心して利用できるサービスです。
Let’s Encrypt の方針は次のとおりで活動しています。
- 無料: ドメイン名を持っている人なら誰でも、Let’s Encrypt を利用して費用なしで信頼される証明書を取得することができる。
- 自動化: ウェブサーバー上で Let’s Encrypt と通信できるソフトウェアを動かすことで、証明書の取得や使用するためのセキュアな設定、更新の作業を簡単に行える。
- セキュア: Let’s Encrypt は、それ自体が発展的な TLS セキュリティのベスト・プラクティスを実践するプラットフォームとして提供される。これは、CA サイドとサイトのオペレータがサーバーを適切にセキュアにすることによって成り立つ。
- 透明性: 発行または無効化されたすべての証明書は、パブリックに記録され、だれでも検証に利用することができる。
- オープン: 自動的な発行・更新のプロトコルを、今後オープンスタンダードとして発行し、誰でも採用できるようにする。
- 協力的: インターネットのプロトコル自体と同じように、Let’s Encrypt はコミュニティに利益を与えるための協力的な努力のもとに成り立っており、いかなる組織のコントロールも受けない。
Apache インストール
Apacheのインストールは、こちらの記事に詳細にまとめています。セキュリティ設定もあるので確認してください。
Apache本体、利用頻度が高い各モジュール、キャッシュによる高速化モジュールなどをインストールします。
まずは、Apache本体をインストールします。
# dnf install httpd
私はPHPで動作プログラムをよく利用するのでPHPに関するジュールをインストールします。
# dnf install php php-opcache php-pecl-apcu php-mbstring php-common php-mysqlnd php-json php-devel php-pdo
サイトをSSL化(https)を利用する場合は、SSL通信モジュール(mod_ssl)をインストールする必要があります。
# dnf install mod_ssl
これぐらいのモジュールをインストールしておけば、大体は事足ります。あとはApacheサービスの起動と、自動起動設定を行います。
# systemctl start httpd # systemctl enable httpd
Let’s Encryptをインストール
Gitをインストール
Let’s Encrypt はGitで管理されています。動かすための必要パッケージをインストールをします。
# dnf -y install git
私は Let’s Encrypt を/etc 配下にインストールします。
# cd /etc # git clone https://github.com/letsencrypt/letsencrypt.git
Let’s Encryptを利用しSSL証明書を発行する
既にWebサーバ環境が整っているのでオプション「webroot」を利用して実行します。ドキュメントルート直下に「.well-known/」というディレクトリが作成され必要な処理が実行されます。
「w」オプションにドキュメントルートのディレクトリを指定します。
# cd /etc/letsencrypt # ./certbot-auto certonly --webroot \ -w /var/www/html/ -d [mydomain] \ -m [email protected] \
Bootstrapping dependencies for RedHat-based OSes that will use Python3... (you can skip this with --no-bootstrap) dnf is /usr/bin/dnf dnf is hashed (/usr/bin/dnf) (中略) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A ← Aを押す - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, 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 our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y ← Yを押す Obtaining a new certificate Performing the following challenges: http-01 challenge for sfonline.atimes.co.jp Using the webroot path /var/www/html for all unmatched domains. Waiting for verification... Cleaning up challenges Subscribe to the EFF mailing list (email: [email protected]). IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/[mydomain]/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/[mydomain]/privkey.pem Your cert will expire on 2021-01-17. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
「Congratulations!」が表示されると完了です。これで3か月有効なSSL証明書が作成されました。
/etc/letsencrypt/live/[mydomain]/fullchain.pem
/etc/letsencrypt/live/[mydomain]/privkey.pem
後は、 Let’s Encrypt にて作成されたSSL証明書をApacheに適応させます。
ApacheにLet’s Encrypt 製のSSL証明書を適応する
作成したSSL証明書をApacheに組み込みます。
# cd /etc/httpd/conf.d # cp ssl.conf ssl.conf.org # vi ssl.conf
SSLCertificateFile、SSLCertificateKeyFile を探して書き換えます。
# サーバー証明書 #SSLCertificateFile /etc/pki/tls/certs/localhost.crt ← 初期値をコメントアウト SSLCertificateFile /etc/letsencrypt/live/[mydomain]/fullchain.pem # 秘密鍵のファイル #SSLCertificateKeyFile /etc/pki/tls/private/localhost.key ← 初期値をコメントアウト SSLCertificateKeyFile /etc/letsencrypt/live/[mydomain]/privkey.pem
Apacheを再起動(又はリロード)して反映させます。
# 再起動の場合 # systemctl reload httpd # リロードの場合 # systemctl reload httpd
Apacheを再起動させると、サービス断が発生するのでリロードする方が良いかもしれません。
自動更新するLet’s Encrypt 製のSSL証明書の作り方
Let’s Encrypt 製のSSL証明書は有効期限が3か月しかありません。直ぐに有効期限が来るので更新プロセスを自動化させる必要があります。
もちろん、手動で更新することも出来ますが大変です。自動化をおススメします。
証明書の更新はとても簡単です。有効期限の残り日が一定以下になったら、アップデート要求をコマンドから送付すると自動で更新処理が実行されます。
有効期限が一定以上ある場合にアップデート要求を行ってもスキップされます。
更新スクリプトの作成
任意のディレクトリに自動更新に利用するスクリプトを作成します。
私の場合は、/root に作っていしまいます。
# vi letsencrypt.sh
証明書を更新するコマンドをスクリプトに記述します。
#!/bin/sh # 更新コマンド /etc/letsencrypt/letsencrypt-auto renew # Apache に反映 systemctl reload httpd
作製したら、ファイルに実行権を与えます。
# chmod +x letsencrypt.sh
更新スクリプトをCronに登録
更新スクリプトを Cron に登録して自動実行させます。
# vi /etc/cron.d/letsencrypt
1日1回実行されるように設定します。更新タイミングは、適当で大丈夫です。
10 4 * * * root /root/letsencrypt.sh
これで自動更新プログラムが毎日実施されます。有効期限が長ければ処理がスキップされるので Let’s Encrypt サーバーに負荷をかけることはありません。
以上、「CentOS8 + Apache + Let’s Encrypt、無料でサイトをSSL化(https)する」という記事でした。
コメント