今回は、UbuntuサーバーにLet's Encryptをインストールする方法をまとめました。
コンテンツ
インストール前の事前準備
まず、インストールに必要な依存パッケージを設定します。
mod_sslを有効にする
mod_sslが有効になっていない場合には、
# a2enmod ssl
で有効化しておきます。
Gitをインストール
Gitからソースをクローンするので、
インストールしていない場合には、
# sudo apt-get install -y git
でGitをインストールしておきます。
これで事前準備は完了したので、
ここからがLet’s Encryptのインストールの開始です。
証明書のインストール
GitからLet’s Encryptのソースをクローン
■作業用のディレクトリに移動
# cd /usr/local
■Gitからリポジトリをクローン
# sudo git clone https://github.com/letsencrypt/letsencrypt /usr/local/letsencrypt
証明書を発行
# ./letsencrypt-auto certonly --webroot --webroot-path /var/www/html/ -d sample.com -m info@sample.com
もし、エイリアス「www」あり・なしの両方に対応する場合には、「-d」を2つ設定して下さい
↓
↓その場合のコマンドはこんな感じ
↓
# ./letsencrypt-auto certonly --webroot --webroot-path /var/www/html/ -d sample.com -d www.sample.com -m info@sample.com
正常に処理が完了すると、以下のようなメッセージが出ます
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for sample.com
http-01 challenge for www.sample.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/sample.com-0001/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/sample.com-0001/privkey.pem
Your cert will expire on 2018-12-24. To obtain a new or tweaked
version of this certificate in the future, simply run
letsencrypt-auto again. To non-interactively renew *all* of your
certificates, run "letsencrypt-auto renew"
- 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
証明書が発行されているかを確認
# ll /etc/letsencrypt/live/sample.com-0001/
drwxr-xr-x 2 root root 4096 Sep 6 19:24 ./
drwx------ 4 root root 4096 Sep 6 19:24 ../
lrwxrwxrwx 1 root root 41 Sep 6 19:24 cert.pem -> ../../archive/sample.com-0001/cert1.pem
lrwxrwxrwx 1 root root 42 Sep 6 19:24 chain.pem -> ../../archive/sample.com-0001/chain1.pem
lrwxrwxrwx 1 root root 46 Sep 6 19:24 fullchain.pem -> ../../archive/sample.com-0001/fullchain1.pem
lrwxrwxrwx 1 root root 44 Sep 6 19:24 privkey.pem -> ../../archive/sample.com-0001/privkey1.pem
-rw-r--r-- 1 root root 682 Sep 6 19:24 README
「cert.pem」「chain.pem」「fullchain.pem」「privkey.pem」の44つのファイルが作成されていれば、
問題なく発行されています。
Apacheから証明書を参照できるように設定
証明書の発行が完了したら、
Apacheから証明書を参照できるように読み込みの設定を行います。
Apacheのconfを修正
# sudo vi /etc/apache2/apache2.conf
でconfファイルを開いて以下を追記します
<VirtualHost 162.133.263.158:443>
ServerName sample.com
ServerAlias www.sample.com
DocumentRoot /var/www/html
CustomLog /var/log/apache2/ssl.sample.com-access_log combined
ErrorLog /var/log/apache2/ssl.sample.com-error_log
AllowOverride All
Require all granted
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/sample.com-0001/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/sample.com-0001/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/sample.com-0001/fullchain.pem
</VirtualHost>
ここで、Apacheを再起動します。
# sudo service apache2 start
ブラウザからの確認
実際に「https://sample.com」と「https://www.sample.com」にアクセスしてみて、
正常に画面が表示されるかを確認して下さい。
問題なく表示されていれば、
これでインストールまで完了です。
自動更新設定
Let's Encryptは、90日毎に更新が必要になります。
が、毎回、手動で更新を行うのは手間になるので、Cronで自動更新をスケジューリングしておきます。
自動更新設定の前
自動更新設定の前に、Cronで設定するコマンドの実行テスト
# sudo /usr/local/letsencrypt/letsencrypt-auto renew --post-hook "sudo service apache2 restart"
※インストールしたばかりなので、以下のようなメッセージが出ればOK
The following certs are not due for renewal yet:
/etc/letsencrypt/live/sample.com-0001/fullchain.pem expires on 2018-12-24 (skipped)
No renewals were attempted.
No hooks were run.
Additionally, the following renewal configurations were invalid:
/etc/letsencrypt/renewal/www.sample.com.conf (parsefail)
/etc/letsencrypt/renewal/sample.com.conf (parsefail)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 renew failure(s), 2 parse failure(s)
証明書自動更新の設定
「/etc/cron.d/」の中に「letsencrypt」というファイルを作成して、
スケジュールを設定することにします。
# sudo vi /etc/cron.d/letsencrypt
で、以下を記述します。
30 12 * * 1 root /usr/local/letsencrypt/letsencrypt-auto renew --deploy-hook 2>&1 "sudo service apache2 restart" >&1 | mail -s "Let's Encrypt auto update" info@sample.com
※毎週月曜日の12:30に実行
※念のために、実行結果を指定のメールアドレスに届くようにしています。
これで、設定は完了です。
この記事へのコメントはありません。