さくらのVPSのサイトをHTTPS化

2019年1月13日
3 分

さくらの VPS で運用しているサイト(Railsアプリ)を Let’s Encrypt で HTTPS 化した。言っとくけどこのブログじゃないよ。

HTTPのポートを開放

まず、HTTPS のポート 443 を開放する。/etc/sysconfig/iptables ファイルを編集。

[takatoh@tk2-254-36564 ~]$ cd /etc/sysconfig  
[takatoh@tk2-254-36564 sysconfig]$ sudo vim iptables

つぎの行を追加。

-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

iptables を再起動。

[takatoh@tk2-254-36564 sysconfig]$ sudo service iptables restart  
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]  
iptables: Flushing firewall rules:                         [  OK  ]  
iptables: Unloading modules:                               [  OK  ]  
iptables: Applying firewall rules:                         [  OK  ]  
iptables: Loading additional modules: ip_conntrack_ftp     [  OK  ]

certbot-auto のセットアップと証明書の自動生成

「certbot-auto」は Let’s Encrypt が提供しているクライントツールで、SSL証明書を取得したり延長したりできる。ここでは、最新版を取得する。

[takatoh@tk2-254-36564 bin]$ sudo wget https://dl.eff.org/certbot-auto  
--2019-01-13 09:41:36--  https://dl.eff.org/certbot-auto  
dl.eff.org をDNSに問いあわせています… 151.101.72.201, 2a04:4e42:36::201  
dl.eff.org|151.101.72.201|:443 に接続しています… 接続しました。  
HTTP による接続要求を送信しました、応答を待っています… 200 OK  
長さ: 63562 (62K) [application/octet-stream]  
`certbot-auto' に保存中  
  
100%[======================================>] 63,562      --.-K/s 時間 0.002s    
  
2019-01-13 09:41:39 (24.3 MB/s) - `certbot-auto' へ保存完了 [63562/63562]  
  
[takatoh@tk2-254-36564 bin]$ sudo chmod 700 /usr/bin/certbot-auto

いまインストールした certbot-auto を使って、証明書を自動生成する。

[takatoh@tk2-254-36564 ~]$ sudo certbot-auto certonly --webroot -w /var/www/lathercraft/public -d www.lathercraft.net --email lathercraft@gmail.com

途中、いろんなパッケージをインストールしたりするのでy(yes)を入力する。しばらくすると利用規約が表示されるので、A(agree)を入力。最後に↓こんなメッセージが出ればOK。

IMPORTANT NOTES:  
 - Congratulations! Your certificate and chain have been saved at:  
   /etc/letsencrypt/live/www.lathercraft.net/fullchain.pem  
   Your key file has been saved at:  
   /etc/letsencrypt/live/www.lathercraft.net/privkey.pem  
   Your cert will expire on 2019-04-12. 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  
  
 - We were unable to subscribe you the EFF mailing list because your  
   e-mail address appears to be invalid. You can try again later by  
   visiting https://act.eff.org.

……じゃないな、e-mail が invalid だと言ってる。あとから試せるとも言ってるので、とりあえず先に進む。この時点で証明書類はこんな感じになっている。

[takatoh@tk2-254-36564 ~]$ sudo ls -l /etc/letsencrypt/live/www.lathercraft.net/  
合計 4  
-rw-r--r-- 1 root root 692  113 09:53 2019 README  
lrwxrwxrwx 1 root root  43  113 09:53 2019 cert.pem -> ../../archive/www.lathercraft.net/cert1.pem  
lrwxrwxrwx 1 root root  44  113 09:53 2019 chain.pem -> ../../archive/www.lathercraft.net/chain1.pem  
lrwxrwxrwx 1 root root  48  113 09:53 2019 fullchain.pem -> ../../archive/www.lathercraft.net/fullchain1.pem  
lrwxrwxrwx 1 root root  46  113 09:53 2019 privkey.pem -> ../../archive/www.lathercraft.net/privkey1.pem

Nginxの設定

SSL 対応にする。こんなふうに書き換える。

    #listen      80;
    listen      443 ssl;

    ssl_certificate      /etc/letsencrypt/live/www.lathercraft.net/cert.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.lathercraft.net/privkey.pem;

Nginx を再起動。

[takatoh@tk2-254-36564 conf.d]$ sudo service nginx restart  
Stopping nginx:                                            [  OK  ]  
Starting nginx:                                            [  OK  ]

これでOKのはず。まだアプリが SSL 対応していないのでバナー画像に https でアクセスしてみたら、ちゃんと表示された。なのでここまではOKのようだ。

RailsアプリのSSL対応

これは簡単。config/envronments/production.rb ファイルのつぎの箇所を修正すればいい。

# config.force_ssl = true
config.force_ssl = true

コメントアウトしてあるのを外すだけだ。そして再起動。

[takatoh@tk2-254-36564 environments]$ sudo service unicorn_lathercraft restart  
reloaded OK

OK。大丈夫そうだ。

httpをhttpsにリダイレクト

最後に、http でのアクセスを https にリダイレクトする設定をする。Nginx の設定に戻ってつぎの設定を追加する。

server {
    listen       80;
    server_name  www.lathercraft.net;
    return       301 https://www.lathercraft.net;
}

Nginx を再起動して完了。

証明書の自動更新設定

Let’s Encrypt の証明書は有効期限が3ヶ月と短いので、自動更新するように設定する。更新は certbot-auto renew コマンドでできるので、これを root ユーザの cron に登録する。

[takatoh@tk2-254-36564 ~]$ sudo -s  
[root@tk2-254-36564 takatoh]# crontab -l  
no crontab for root  
[root@tk2-254-36564 takatoh]# crontab -e
0 4 * * 0 certbot-auto renew --post-hook "service unicorn_lathercraft restart" > /dev/null 2> /dev/null

これで毎週月曜日の午前4時に更新するようになる。

[追記]

上の、Nginx のリダイレクトの設定では、http でのアクセスはどんなパスでも全て https のルートにリダイレクトされてしまう。なので、つぎのように変更した。

server {
    listen       80;
    server_name  www.lathercraft.net;

    location ~ ^/(.*)$ {
        return 301 https://www.lathercraft.net/$1;
    }
}

これで各ページも https にリダイレクトされるようになった。