LoginSignup
3
2

More than 5 years have passed since last update.

SSL無料証明書: Let's Encrypt

Last updated at Posted at 2017-10-08

無料でSSL対応したかったので、Let's Encryptを使用した時のメモです

証明書の公式サイト

環境

OS: Amazon Linux Ami
WebServer: Nginx

前提条件

① 下記で自分で取得したドメインの名前解決ができているか確認
http://mgt.jp/t/dns

② サーバーのhttpのポート80 とhttpsのポート443が空いていること。

証明書の設定

移動
$ cd /usr/local/
リポジトリをクローン
$ sudo git clone https://github.com/certbot/certbot
移動
$ cd certbot
実行
# sudo ./letsencrypt-auto --debug
実行
$ sudo ./certbot-auto -debug
Nginxを停止
$ sudo service nginx stop
SSL証明書を発行。 この後にnginxの1番を選び。自分のドメインを入れる
$ sudo ./certbot-auto certonly
nginxを再起動
$ sudo service nginx restart

Nginxの設定

自分の場合はserver.confに直接書いてあるので、ここを編集するが、 site-avaiable に書いてる人が普通そう。
$ sudo vi /etc/nginx/conf.d/server.conf

server.conf
server {
     # コメントアウトする
     # listen 80;

     # SSLのPORTを追加
     listen 443 ssl;
     # 自分のドメインを追加 example.com
     server_name example.com;
     # 先程インストールされた照明書のfullchainがここにあるはずなので追加
     ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
     # 先程インストールされた照明書のprivkeyがここにあるはずなので追加
     ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_max_temp_file_size 0;
     location / {
        proxy_pass http://localhost:8000;
     }
}
# httpにアクセスした時にhttpsにリダイレクトされるように以下を追加する
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

準備ができたので、ngnixをリスタート! これで完了!
$ sudo service ngninx restart

証明書の自動更新

cronが起動しているかを確認

$ /etc/rc.d/init.d/crond status
crond (pid xxx) を実行中...

cronに登録
$ sudo vi /etc/cron.d/letsencrypt

/etc/cron.d/letsencrypt
# 月1で証明書を自動更新
0 1 1 * * root /usr/local/certbot/certbot-auto renew --force-renewal && service nginx restart
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2