2024.03.07
イベントの感想募集でGoogleフォーム使ってみたら便利だった
2017.09.06
インフラ【AmazonLinux+Nginx】Let’s Encrypt設置

JTです。
AmazonLinux+NginxのWebサイトにLet’s EncryptでSSL証明書設置したので、メモを残します。
何かの参考になれば幸いです。
Let’s Encryptで証明書発行(ドメイン使用権認証)用のファイルを設置するドキュメントルートが必要です。
ここでは、既存のvhostについて、Let’s encrypt が webroot 更新で使う部分だけを別のディレクトリに設定しています。
|
1 2 |
mkdir /srv/www/letsencrypt_webroot chown nginx:nginx /srv/www/letsencrypt_webroot |
※nginx conf(例)
vi /etc/nginx/conf.d/hogehoge.com.conf
以下を追記する
|
1 2 3 |
location /.well-known/ { root /srv/www/letsencrypt_webroot; } |
nginx書式テスト&反映
|
1 2 |
nginx -t service nginx reload |
1. AWSのセキュリティグループを設定
→サーバに適用されているセキュリティグループのインバウンドのHTTP(80)、HTTPS(443)を許可(0.0.0.0/0)にしておきます。
2. certbotのダウンロード&配置&権限設定
|
1 2 |
curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto chmod 700 /usr/bin/certbot-auto |
3. 証明書の発行
cerbot-autoのコマンドで証明書が自動生成されます。
※今回はhogehoge.comとwww.hogehoge.comの、www有無それぞれで発行します。
|
1 2 3 4 5 6 7 |
certbot-auto certonly \ # 証明書の作成 --webroot \ # 既存のウェブサーバを使うモードを選択 -w /srv/www/letsencrypt_webroot \ # ドキュメント・ルートのパス -d hogehoge.com \ # 認証するドメイン名1 -d www.hogehoge.com \ # 認証するドメイン名2 --email <メール>@<アドレス>\ # メールアドレス登録(証明書期限切れの通知用) --debug # AmazonLinuxではこれが必要 |
→ ここで不足している必須モジュールがインストールされます。
※ –debugについて:
これをつけないと、AmazonLinuxでは以下のメッセージが出て進められないため、追加します。
|
1 2 3 4 5 |
FATAL: Amazon Linux support is very experimental at present... if you would like to work on improving it, please ensure you have backups and then run this script again with the --debug flag! Alternatively, you can install OS dependencies yourself and run this script again with --no-bootstrap. |
※そのほかcertbot-autoのオプションについては下端にメモしておきました。
■以下のエラーが出た場合の対応メモ
|
1 |
/usr/bin/certbot-auto: line 700: virtualenv: command not found |
→ virtualenvは「/usr/bin/virtualenv-2.7」以下に存在したので、update-alternativesでリンクを張る
|
1 2 3 4 5 6 7 |
# update-alternatives設定 update-alternatives --install /usr/bin/pip pip /usr/bin/pip-2.7 27 update-alternatives --install /usr/bin/virtualenv virtualenv /usr/bin/virtualenv-2.7 27 # 確認 update-alternatives --display pip update-alternatives --display virtualenv |
以下対話式で進める
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
------------------------------------------------------------------------------- Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree in order to register with the ACME server at https://acme-staging.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: Y (省略) IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/hogehoge.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/hogehoge.com/privkey.pem Your cert will expire on 2017-12-04. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *allof your certificates, run "certbot-auto renew" (省略) |
4. 生成された証明書の確認
以下のディレクトリに証明書が生成されます。
|
1 |
/etc/letsencrypt/live/hogehoge.com/ |
5.nginx側に証明書を設定
|
1 2 3 4 5 6 7 |
server { listen 443 ssl; ssl on; (省略) ssl_certificate /etc/letsencrypt/live/hogehoge.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/hogehoge.com/privkey.pem; (省略) |
nginx書式テスト&反映
|
1 2 |
nginx -t service nginx reload |
6.確認
https://hogehoge.com/
https://www.hogehoge.com/
→SSL通信できていることを確認
有効期限も確認
|
1 |
openssl s_client -connect "hogehoge":443 < /dev/null 2> /dev/null | openssl x509 -text | grep "Not" |
7.証明書の自動更新
# certbot-auto renew コマンドで更新します
# crontabに設置する例
crontab -e
|
1 |
50 3 * * 0 /usr/bin/certbot-auto renew --post-hook "/etc/init.d/nginx reload" > /path_to_log/certbot-auto.log 2>&1 |
参照:https://letsencrypt.jp/command/
certonly
→SSL/TLS サーバ証明書の取得のみを行います。
–webroot
→ウェブサーバの DocumentRoot ディレクトリ以下に認証用のファイルを設置することでドメイン使用権者の認証を行って、SSL/TLS サーバ証明書を取得します。
→Certbot クライアントを実行するコンピュータでウェブサーバ(httpd)が動作中であり、ウェブサーバを停止することなく SSL/TLS 証明書を発行したい場合には、Webroot プラグインを利用する必要があるとのこと(https://letsencrypt.jp/docs/using.html*webroot)
–webroot-path WEBROOT_PATH もしくは -w WEBROOT_PATH
→”public_html” や “webroot” のパスを指定します。
-d DOMAIN もしくは –domains DOMAIN もしくは –domain DOMAIN
→SSL/TLS サーバ証明書の取得を申請するドメイン名を指定します。
–post-hook POST_HOOK
証明書を取得・更新する試みが終わった後にシェルで実行するコマンドを指定します。
これは、証明書を取得・更新する試みが行われた場合のみ実行されます。
http://qiita.com/HeRo/items/f9eb8d8a08d4d5b63ee9
http://qiita.com/MashMorgan/items/56498f276c54406b1928
http://tnamao.hatenablog.com/entry/2016/03/15/014004
https://blog.doizaki.com/entry/2016/02/07/152141
http://knowledge.sakura.ad.jp/knowledge/5573/
【記事への感想募集中!】
記事への感想・ご意見がありましたら、ぜひフォームからご投稿ください!【テクノデジタルではエンジニア/デザイナーを積極採用中です!】
下記項目に1つでも当てはまる方は是非、詳細ページへ!Qangaroo(カンガルー)
【テクノデジタルのインフラサービス】
当社では、多数のサービスの開発実績を活かし、
アプリケーションのパフォーマンスを最大限に引き出すインフラ設計・構築を行います。
AWSなどへのクラウド移行、既存インフラの監視・運用保守も承りますので、ぜひご相談ください。
詳細は下記ページをご覧ください。
最近の記事
タグ検索
SNS共有