LoginSignup
2

More than 5 years have passed since last update.

Amazon Linux上にインストールしたGitLab CEにLet's Encryptを適用する方法

Last updated at Posted at 2017-04-06

対象環境

GitLab Community Edition 9.0.4
certbot 0.13.0(Let's EncryptのCLIクライアント)

原理

Let's Encryptの「webroot」モードを利用する。
webrootモードはサーバに .well-known ディレクトリが置かれていることを認証基準としてSSL証明書を発行する。
GitLabのログインを回避して .well-known をLet's Encryptに発見させ、SSL証明書を発行するのが本記事の目的である。

手順

※ GitLabはインストールが完了しており、既に稼働しているものとする。

1. GitLabが利用しているNginxの設定を変更する

GitLabのログインを回避して .well-known の存在を確認させるためにNginxのリダイレクト設定を変更する。
まずは設定ファイルを新規作成。

$ sudo vim /etc/gitlab/custom_gitlab_server_config.conf
/etc/gitlab/custom_gitlab_server_config.conf
location ^~ /.well-known {
    alias /var/letsencrypt/.well-known;
}

その後、設定ファイルを適用。

$ sudo vim /etc/gitlab/gitlab.rb
/etc/gitlab/gitlab.rb
nginx['custom_gitlab_server_config'] = "include /etc/gitlab/custom_gitlab_server_config.conf;"

最後に、Let's Encryptに発見させる用のディレクトリを作成しておく。
.well-known は作成しなくてよい。

$ sudo mkdir /var/letsencrypt

2. GitLabに設定を適用して再起動

$ sudo mkdir /var/letsencrypt

3. Let's Encryptをインストール

絶対に最後の --debug を忘れないように。
Amazon Linux限定の手法なので他のOSの場合は不要。

$ sudo mkdir /usr/local/letsencrypt
$ sudo chown `whoami` /usr/local/letsencrypt
$ cd /usr/local/letsencrypt
$ git clone https://github.com/letsencrypt/letsencrypt .
$ sudo ./letsencrypt-auto --debug

4. SSL証明書を取得

[gitlab.example.com] の部分はGitLabに適用しているドメインを指定する。

$ sudo ./letsencrypt-auto certonly --webroot --webroot-path /var/letsencrypt -d [gitlab.example.com]

エラーが出た場合

エラーの場合はこんな表示が出る。

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: gitlab.example.com
   Type:   unauthorized
   Detail: Invalid response from

Typeが unauthorized の場合

  • ログインが回避できていないので、1の手順が正しく完了しているか確認する

Typeが unconnected の場合

  • AWSのセキュリティグループを確認し、80番と443番のポートを開ける
  • 勢い余って /etc/gitlab/gitlab.rbexternal_url をhttpsにしていないか確認する(証明書取得まで禁止!)

5. SSL証明書が自動更新されるよう設定

$ sudo crontab -u root -e
00 05 01 * * /path/to/letsencrypt/letsencrypt-auto renew --force-renew && gitlab-ctl restart

5. SSL証明書をGitLabに適用

external_url をHTTPSに変更する。
[gitlab.example.com] の部分はGitLabに適用しているドメインを指定する。

redirect_http_to_https をtrueにしておくと、HTTPでアクセスされてもHTTPSにリダイレクトされる。
証明書の再発行の際はHTTPによる .well-known の確認が走らないので、HTTP接続を潰してしまってよい。
同じくAWSのセキュリティグループレベルでも80番ポートを潰してしまってよい。

$ sudo vim /etc/gitlab/gitlab.rb
/etc/gitlab/gitlab.rb
nginx['external_url']                = "https://[gitlab.example.com]"
nginx['redirect_http_to_https']      = true
nginx['ssl_certificate']             = "/etc/letsencrypt/live/[gitlab.example.com]/fullchain.pem"
nginx['ssl_certificate_key']         = "/etc/letsencrypt/live/[gitlab.example.com]/privkey.pem"

6. GitLabを再起動

$ sudo gitlab-ctl reconfigure

参考文献

GitLab Omnibus package の SSL 証明書を Let's Encrypt で取得する
http://qiita.com/yuuAn/items/09a434d3f6cffa31101e

アクセス制限のかかったGitLabをLet's Encryptをつかってhttps化する
https://gist.github.com/mamemomonga/a36a194f8a80ce5fa49bf950e092c604

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
2