LoginSignup
4
5

More than 5 years have passed since last update.

CentOS7に構築したGitLab8をSSL対応

Last updated at Posted at 2016-09-27

[概要]

SSLが無料で手に入る時代になったので、GitLabをSSL対応させる。

下記のQiitaの記事が参考にしてます。
Let's encrypt運用のベストプラクティス

GitLab Omnibus package の SSL 証明書を Let's Encrypt で取得する

[事前準備]

下記で構築したものを使います。
CentOS7にGitLab8を構築

[作業内容]

Let's Encrypt クライアントをクローン

gitが必要なので、インストール

$ sudo yum install git
$ cd /opt
$ sudo git clone https://github.com/certbot/certbot

Let's Encrypt クライアントで必要なパッケージをインストール

$ cd certbot/
$ sudo ./certbot-auto

Let's Encrypt 用の設定を読み込むNginxファイルを用意

sudo vi /etc/gitlab/gitlab.rb

下記を追加すると、 include 設定をNginxに加えてくれるようになります。

nginx['custom_gitlab_server_config'] = "include /opt/letsencrypt/etc/nginx.conf;"

include 設定は、下記のファイルに追記されるようになります。
/var/opt/gitlab/nginx/conf/gitlab-http.conf

Let's Encrypt 用の設定ファイル作成

### 設定ファイル置き場
$ sudo mkdir -p /opt/letsencrypt/etc

### ドキュメントルート作成
$ sudo mkdir /opt/letsencrypt/var

### Nginx の設定ファイル作成
$ sudo vi /opt/letsencrypt/etc/nginx.conf
/opt/letsencrypt/etc/nginx.conf
location ^~ /.well-known {
    alias /opt/letsencrypt/var/.well-known;
}

GitLabに設定を反映

$ sudo gitlab-ctl reconfigure

証明書取得のためのスクリプトを作成 1

下記を参考に適宜変更してください。

$ sudo vim /opt/letsencrypt.sh
#!/bin/bash

#証明書のドメイン(,区切りで複数のドメインも指定可能)
DOMAIN=example.com

#ドキュメントルート(上のドメインで接続可能である必要がある)
WEBROOT=/var/www/example.com/public_html

#メールアドレス(トラブル時にメールが届く)
EMAIL=info@example.com

/opt/certbot/certbot-auto certonly --webroot -w $WEBROOT -d $DOMAIN -m $EMAIL --agree-tos --non-interactive $*

スクリプトに実行権限付与

$ sudo chmod 755 letsencrypt.sh 

テスト実行

sudo ./letsencrypt.sh --test-cert
### --test-cert は、SSL/TLS サーバ証明書の取得時に、ステージングサーバを使用して、無効な証明書を取得します。

下記のように表示されれば OK です。

 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/gitlab/fullchain.pem. Your cert
   will expire on 2016-12-25. 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"

本実行

下記のコマンドで、本番用の証明書ファイルを取得する。
bash
$ sudo ./letsencrypt.sh --force-renewal

SSL設定

$ sudo vim /etc/gitlab/gitlab.rb

下記のように http:// の部分を https:// に変更

/etc/gitlab/gitlab.rb
external_url 'https://{ドメイン名}'

HTTPへのアクセスをHTTPSにリダイレクトする設定の有効化

/etc/gitlab/gitlab.rb
# nginx['redirect_http_to_https'] = true

証明書のファイルパスを設定する

/etc/gitlab/gitlab.rb
nginx['ssl_certificate'] = "/etc/letsencrypt/live/{ドメイン名}/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/{ドメイン名}/privkey.pem"

設定を反映

$ sudo gitlab-ctl reconfigure

[事後確認・作業]

[参考サイト]

4
5
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
4
5