LoginSignup
11
10

More than 5 years have passed since last update.

さくらのVPS 標準OSの環境構築(6) Nginxリバースプロキシ環境でのGitLabの導入

Last updated at Posted at 2016-04-25

前回

さくらのVPS 標準OSの環境構築(5) Let's EncryptでSSL+HTTP/2対応

GitLabとは

GitLabは、Ruby on Rails製のGitHubクローン環境です。
クローズドなGitリポジトリ管理環境を導入することが出来ます。

注意

前回まででサーバー構築に関しては完了しており、今回は必要な方のみに向けた所謂おまけ的な記事になります。
Gitリポジトリ管理環境が必要なければ、無理に入れる必要は無いと思います。

また、GitLabRuby,Nginx,Unicorn,Rails,PostgreSQLなどのコンポーネントがセットでインストールされます。
まっさらな環境にインストールする場合は公式ドキュメント通りに手順を踏んで何の問題もないのですが、今回は既にNginxがリバースプロキシサーバーとして稼働している環境にインストールするため、通常とは違いNginxのみ既存のものを使うように手順を踏むことになります。

GitLabのインストール

依存パッケージに関しては、ここまでの手順でインストール済みだと思います。
GitLab公式の手順通りyumリポジトリを追加してインストールします。

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | bash
yum install gitlab-ce

GitLabの設定

GitLabの起動設定ファイルを編集します。
必要な部分のコメントアウトを外して、多少数値を変えます。

vi /etc/gitlab/gitlab.rb

URL設定

使用するURLは事前にドメイン設定側で正引きしておきましょう。

/etc/gitlab/gitlab.rb
- external_url 'http://example.com'
+ external_url 'https://git.example.com'

タイムゾーン設定

サーバーに設定しているタイムゾーンを指定します。

/etc/gitlab/gitlab.rb
- # gitlab_rails['time_zone'] = 'UTC'
+ gitlab_rails['time_zone'] = 'Asia/Tokyo'

メール送信設定(Gmail経由)

メール通知用の設定を行います。
SMTP設定を行い、Gmailからメールが送信されるように設定します。
Google Appsを利用したい場合も、この設定で大丈夫だと思います。

/etc/gitlab/gitlab.rb
- # gitlab_rails['gitlab_email_from'] = 'example@example.com'
+ gitlab_rails['gitlab_email_from'] = 'my.gmail@gmail.com'
:
- # gitlab_rails['smtp_enable'] = true
- # gitlab_rails['smtp_address'] = "smtp.server"
- # gitlab_rails['smtp_port'] = 456
- # gitlab_rails['smtp_user_name'] = "smtp user"
- # gitlab_rails['smtp_password'] = "smtp password"
- # gitlab_rails['smtp_domain'] = "example.com"
- # gitlab_rails['smtp_authentication'] = "login"
- # gitlab_rails['smtp_enable_starttls_auto'] = true
- # gitlab_rails['smtp_tls'] = false
- # gitlab_rails['smtp_openssl_verify_mode'] = 'none' # Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert', see http://api.rubyonrails.org/
+ gitlab_rails['smtp_enable'] = true
+ gitlab_rails['smtp_address'] = "smtp.gmail.com"
+ gitlab_rails['smtp_port'] = 587
+ gitlab_rails['smtp_user_name'] = "my.gmail@gmail.com"
+ gitlab_rails['smtp_password'] = "YOUR_GMAIL_PASSWORD"
+ gitlab_rails['smtp_domain'] = "smtp.gmail.com"
+ gitlab_rails['smtp_authentication'] = "login"
+ gitlab_rails['smtp_enable_starttls_auto'] = true
+ gitlab_rails['smtp_tls'] = false
+ gitlab_rails['smtp_openssl_verify_mode'] = 'peer' # Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert', see http://api.rubyonrails.org/

GitLabのPostgreSQL設定

Community EditionではPostgreSQL以外のデータベースには変更出来ません。

/etc/gitlab/gitlab.rb
- # postgresql['enable'] = true
- # postgresql['listen_address'] = nil
- # postgresql['port'] = 5432
- # postgresql['data_dir'] = "/var/opt/gitlab/postgresql/data"
- # postgresql['shared_buffers'] = "256MB" # recommend value is 1/4 of total RAM, up to 14GB.
+ postgresql['enable'] = true
+ postgresql['listen_address'] = nil
+ postgresql['port'] = 5432
+ postgresql['data_dir'] = "/var/opt/gitlab/postgresql/data"
+ postgresql['shared_buffers'] = "512MB" # recommend value is 1/4 of total RAM, up to 14GB.

GitLabのUnicorn設定

タイムアウト上限を引き上げます。

/etc/gitlab/gitlab.rb
- # unicorn['worker_timeout'] = 60
- # unicorn['worker_processes'] = 2
+ unicorn['worker_timeout'] = 120
+ unicorn['worker_processes'] = 2

## Advanced settings
- # unicorn['listen'] = '127.0.0.1'
- # unicorn['port'] = 8080
- # unicorn['socket'] = '/var/opt/gitlab/gitlab-rails/sockets/gitlab.socket'
- # unicorn['pidfile'] = '/opt/gitlab/var/unicorn/unicorn.pid'
- # unicorn['tcp_nopush'] = true
- # unicorn['backlog_socket'] = 1024
+ unicorn['listen'] = '127.0.0.1'
+ unicorn['port'] = 60080
+ unicorn['socket'] = '/var/opt/gitlab/gitlab-rails/sockets/gitlab.socket'
+ unicorn['pidfile'] = '/opt/gitlab/var/unicorn/unicorn.pid'
+ unicorn['tcp_nopush'] = true
+ unicorn['backlog_socket'] = 1024

GitLabのRedis設定

/etc/gitlab/gitlab.rb
- # redis['enable'] = true
- # redis['username'] = "gitlab-redis"
- # redis['uid'] = nil
- # redis['gid'] = nil
+ redis['enable'] = true
+ redis['username'] = "gitlab-redis"
+ redis['uid'] = nil
+ redis['gid'] = nil

GitLabのNginx設定

最重要
GitLab内蔵のNginxを使用しないようにします。
この変更をしないまま誤って起動してしまうと、既に起動しているNginxがかなりおかしくなります。

/etc/gitlab/gitlab.rb
- # nginx['enable'] = true
+ nginx['enable'] = false

Nginxの設定変更

リバースプロキシとして既に起動しているNginxの設定を変更します。
proxy_passのポート番号はUnicorn設定のunicorn['port']で指定した値になります。

vi /etc/nginx/conf.d/default.conf

/etc/nginx/conf.d/default.conf
+ server {
+     listen      80;
+     server_name git.example.com;
+     return      301 https://$host$request_uri;
+ }
+ server {
+     listen      443 ssl http2;
+     server_name git.example.com;
+ 
+     ssl                 on;
+     ssl_certificate     /etc/letsencrypt/live/git.example.com/fullchain.pem;
+     ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;
+ 
+     ssl_session_cache   shared:SSL:3m;
+     ssl_buffer_size     8k;
+     ssl_session_timeout 10m;
+ 
+     ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
+     ssl_ciphers         AESGCM:HIGH:!aNULL:!MD5;
+     ssl_prefer_server_ciphers on;
+ 
+     access_log /var/log/nginx/gitlab.access.log;
+     error_log  /var/log/nginx/gitlab.error.log;
+ 
+     location = /robots.txt  { access_log off; log_not_found off; }
+     location = /favicon.ico { access_log off; log_not_found off; }
+ 
+     location ^~ /.well-known/ {
+          root /letsencrypt-dummy-webroot;
+     }
+ 
+     location / {
+         proxy_set_header Host               $host;
+         proxy_set_header X-Real-IP          $remote_addr;
+         proxy_set_header X-Forwarded-Proto  https;
+         proxy_set_header X-Forwarded-Host   $host;
+         proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
+ 
+         proxy_pass http://127.0.0.1:60080;
+         proxy_redirect http:// https://;
+ 
+         proxy_connect_timeout 120;
+         proxy_read_timeout    120;
+         proxy_send_timeout    120;
+ 
+         proxy_max_temp_file_size 0;
+     }
+ }

上記の設定はSSLの発行・設定まで完了した最終的な記述になります。
SSL証明書発行のために、まずhttpアクセスで証明書を発行し、その後SSLを設定するまでの流れは前回までの記事を参考にしてください。
またSSLの詳細設定はあくまで一例になります。セキュリティ強度の向上は各々が調査のうえで行ってください。

設定が完了したらNginxを再起動します。

service nginx restart

GitLabの設定の反映と起動

内蔵コンポーネントの設定なども処理されるので、初期起動には時間がかかります。

gitlab-ctl reconfigure

実行結果
Starting Chef Client, version 12.6.0
 : (省略)
Running handlers complete
Chef Client finished, ***/*** resources updated in *** minutes *** seconds
gitlab Reconfigured!

起動が完了したらexternal_urlで設定したURLでアクセスしてみましょう。
正常に表示されればインストール成功です。(多分)
あとは公式の手順通りにセットアップを行ってください。
インストール直後の初期ユーザーは下記になります。

ID: root
Pass: 5iveL!fe

まとめ

おまけを含め、今回自分のVPSで設定した内容を大体書き出し終えることができました。
ここまでご一読いただきありがとうございます。

改めますが、サーバー構築初心者のため、当方は本記事を参考にセットアップしたことによって被った損害・損失に対し、いかなる場合でも一切の責任を負いませんのでご了承ください。

多分間違いやセオリー外のことも多々あると思いますので、発見いただいた方はコメントや編集リクエストなどでご指摘いただけると幸いです。

11
10
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
11
10