どぶお/開発しよう!

GitLab 7.3.2のインストール -- CentOS 6.5  

2014-10-13 : 作成

概要  

以前から使用していたVPSの内容を更新したため、環境の再構築のためGitLabの最新版をインストールしてみましたので記録しておきます。 最近のバージョンはインストールがものすごく簡単になり、CentOS 6/7などならRPMからさっくりとインストールできます。

標準の状態では、nginxもインストールされ、apacheすら不要ですが、ここでは以下の設定で構築してみました。

経路はこんな感じ。

[Webブラウザ] -(HTTPS)-> [https://mygitlab.example.com/ - Apache]
                  -(Apacheリバースプロキシ)->-(HTTP)-> [http://localhost:8000/ - GitLab]

Apacheを通すことで(どぶお的には)フレキシブルな設定が可能です。

実現するためには以下の設定が必要です。

  1. GitLabのインストール
  2. Apacheのインストールおよびリバースプロキシの設定
  3. GitLabのテンプレートの修正(オプション)

ここでのGitLabの一部修正はGitLabのreconfigureスクリプトを実行する際のテンプレートを修正します。これはプロジェクトのリポジトリのURLの表示を修正するためのもので、実施しなくても動作には影響ありません(たぶん)。

注意点  

GitLabの設定は本来gitlab.ymlなどで行っていましたが、このバージョンではChef(よく知らないけど)を使っているようで、設定ファイル/etc/gitlab/gitlab.rbを初期変数として各設定ファイルをテンプレートから生成します。そのため、/var/opt/gitlab以下の設定ファイルを変更した後、gitlab-ctl reconfigureを行ってしまうと、すべて元に戻ってしまいます。従って、設定を変更する際はgitlab.rbで行う、と留意しておきましょう。

インストール  

yumでインストールしておくもの  

インストール  

インストールはドキュメント通りで、さくっと。

# wget https://downloads-packages.s3.amazonaws.com/centos-6.5/gitlab-7.3.2_omnibus-1.el6.x86_64.rpm
# service postfix start
# chkconfig postfix on
# rpm -ivh gitlab-7.3.2_omnibus-1.el6.x86_64.rpm

これだけで準備完了です。素晴らしい!

環境ファイルの作成と再構築  

今回はリバースプロキシを使うのでURLはlocalhost:8000、さらにプロジェクトページのリポジトリURLを修正するためにgitlab_ssh_hostおよびgitlab_repo_url(非標準;リバースプロキシを使う場合の設定。後述)を設定します。
また、リポジトリの保存先を標準(/var/opt/gitlab/git-data)以外にしたい場合も指定します。

/etc/gitlab/gitlab.rb
external_url 'http://localhost:8000'
gitlab_rails['gravatar_enabled'] = false  <-- gravatarを使わない設定
gitlab_rails['gitlab_repo_url'] = 'https://mygitlab.example.com'
gitlab_rails['gitlab_ssh_host'] = 'mygitlab.example.com'

# git-dataの場所を変更する場合
gitlab_shell['git_data_directory'] = '/home/git-data'
gitlab_rails['satellites_path'] = '/home/git-data/gitlab-satellites'
gitlab_rails['gitlab_shell_repos_path'] = '/home/git-data/repositories'

設定したら再構築します。

# gitlab-ctl reconfigure
...
Chef Client finished, 9/130 resources updated in 10.954270629 seconds
gitlab Reconfigured!  <-- 特にエラーが無ければこんな感じで終了します。

この時点でGitLabは起動しているはずなので、GitLabが起動しているサーバのポート8000番にアクセスしてGitLabのログイン画面が表示されれば成功です。

Apacheの設定  

ここではすでにApache + mod_sslの初期設定は済んでいるものとします。
GitLabはNginxを使っており、GitLab専用のサーバや特別なポートを使う場合はそれでいいのですが、例えば80番や443番を使う場合、他のWebサービスが使えなくなってしまい不便です。そのため、Apacheのリバースプロキシを使うことにします。
ポイントは以下です。

  1. GitLabはSSLモードで稼働していない
  2. https://mygitlab.example.com/はhttp://localhost:8000/のフロントエンドである

特に(1)のSSLモードについて意識しておかないと、ログイン時のページリダイレクトでhttpアクセスになってしまい、Not Foundエラーになってしまいます。

VirtualHostの設定  

例えばVirtualHostで以下のように設定します。

<VirtualHost *:443>
  # GitLab settings with Apache Reverse Proxy on 2014-10-13 by Dobuo
  SSLEngine on
  ServerName mygitlab.example.com:443
  ErrorLog "/var/log/httpd/mygitlab-error_log"
  TransferLog "/var/log/httpd/mygitlab-access_log"

  SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
  SSLCertificateFile "mygitlab-cert.pem"
  SSLCertificateKeyFile "mygitlab-seckey.pem"

  # Rewrite 'Location' header to 'https'
  Header edit Location ^http://mygitlab.example.com/ https://mygitlab.example.com/

  <Location />
    Options None
    Allow from all
    ProxyPass http://localhost:8000/
    ProxyPassReverse http://localhost:8000/
  </Location>
</VirtualHost>

赤色で示したところが重要な部分です。

この設定で、https://mygitlab.example.com/にアクセスした時にGitLabにアクセスできるようになるはずです。

これで設定はほぼ完了ですが、細かな点が残っています。それは、GitLab自体の設定ではhttp://localhost:8000となっているため、プロジェクトページのリポジトリアクセス先のSSHおよびHTTPの表示がhttp://localhost:8000になったまま、ということです。例えば以下のようになります。

http://localhost:8000/dobuo/biokids2014.git

気にしない、というのも一つの解ですが、余計な設定を見せないというのはサーバ管理の基本ですので以下の方法で正しい表示にします。

GitLabソースファイルの修正  

プロジェクトページのURLはgitlab.ymlのurl項目で設定可能なようです。
ただし、将来的に再構築(gitlab-ctl reconfigure)を行うことがあるなら、テンプレートファイルを修正しておく必要があります。そうでない場合は設定だけを行います。

パッチファイルを使う  

以下の修正内容をパッチファイルにまとめましたので、試してみて下さい。

# cd /opt/gitlab
# patch -p0 < gitlab-7.3.2-repo_url.patch
patching file embedded/cookbooks/gitlab/templates/default/gitlab.yml.erb
patching file embedded/service/gitlab-rails/app/mailers/notify.rb

内容は以下を参照して下さい。

gitlab.ymlの修正(再構築を行わない場合)  

/var/opt/gitlab/gitlab-rails/etc/gitlab.yml
19     # Otherwise, ssh host will be set to the `host:` value above
20     ssh_host: mygitlab.example.com    <-- 多分値が入ってる
21     url: https://mygitlab.example.com <-- 追加

この修正を行ったらGitLabを再起動して、表示が正しくなっていることを確認します。

# gitlab-ctl restart

根本的に解決しておきたい場合は以下の方法を試して下さい。

GitLabの設定ファイルテンプレートの修正  

gitlab-ctl reconfigureの際に参照されるgitlab.ymlのテンプレートファイルを修正しておきます。

/opt/gitlab/embedded/cookbooks/gitlab/templates/default/gitlab.yml.erb
19     # Otherwise, ssh host will be set to the `host:` value above
20     ssh_host: <%= @gitlab_ssh_host %>
21     url: <%= @gitlab_repo_url %>      <-- 追加

ここでgitlab_repo_urlの値が埋め込まれるようにしておくと、reconfigureしても、gitlab.ymlに反映されます。修正した再構築します。

# gitlab-ctl reconfigure

確認メール関連の修正  

ユーザー情報でメールアドレスの変更などを行った場合、メールアドレス確認URLが送信されます。しかし、これもhttp://localhost:8000/のURLになってしまうので、上記のrepo_urlが使用されるように以下のファイルを修正します。
赤色が追加した行、青色がコメントアウトした行です。

/opt/gitlab/embedded/service/gitlab-rails/app/mailers/notify.rb
13   add_template_helper MergeRequestsHelper
14
15   uri = URI(Gitlab.config.gitlab.url)
16   default_url_options[:host]     = uri.host
17   default_url_options[:protocol] = uri.scheme
18   default_url_options[:port]     = uri.port unless Gitlab.config.gitlab_on_standard_port?
19 #  default_url_options[:host]     = Gitlab.config.gitlab.host
20 #  default_url_options[:protocol] = Gitlab.config.gitlab.protocol
21 #  default_url_options[:port]     = Gitlab.config.gitlab.port unless Gitlab.config.gitlab_on_standard_port?
22   default_url_options[:script_name] = Gitlab.config.gitlab.relative_url_root
23
24   default from: Proc.new { default_sender_address.format }
25   default reply_to: "noreply@#{Gitlab.config.gitlab.host}"

修正したらGitLabを再起動します。

# gitlab-ctl restart

トラブル  

アーカイブがダウンロードできない  

問題
リポジトリのアーカイブをダウンロードしようとすると500エラーが出る
解法
/var/log/gitlab/gitlab-railsproduction.logを確認します。
Started GET "/dobuo/biokids2014/repository/archive.zip" for xxx.xxx.xxx.xxx at 2014-10-14 10:23:31 +0900
Processing by Projects::RepositoriesController#archive as ZIP
  Parameters: {"project_id"=>"dobuo/biokids2014"}
Completed 500 Internal Server Error in 300ms

Errno::ENOENT (No such file or directory @ rb_sysopen - /opt/gitlab/embedded/service/gitlab-rails/tmp/repositories/gitlab-archive-repo20141014-30705-1cpeq7w):
  app/models/repository.rb:150:in `method_missing'
  app/services/archive_repository_service.rb:12:in `execute'
  app/controllers/projects/repositories_controller.rb:17:in `archive'
これを見ると、No such file or directoryエラーが出ています。これを解決するために/opt/gitlab/embedded/service/gitlab-rails/tmp/repositoriesディレクトリを作成し、所有者を変更しておきます。
# mkdir /opt/gitlab/embedded/service/gitlab-rails/tmp/repositories
# chown git:root /opt/gitlab/embedded/service/gitlab-rails/tmp/repositories

You are not allowed to access some of the refs!エラーが出てpushできない  

問題
リポジトリにpushしようとすると以下のようなエラーが出る
% git push origin master
...
remote: GitLab: You are not allowed to access some of the refs!
To ssh://git@gitlab.example.com/username/project.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'ssh://git@gitlab.example.com/username/project.git'
解法
/var/opt/gitlab/gitlab-shell/config.ymlで指定しているrepos_pathとauth_pathにシンボリックリンクが含まれているために起こるようです。途中にシンボリックリンクが含まれないフルパスで指定します。
reconfigureで指定する場合は/etc/gitlab/gitlab.rbに
gitlab_shell['git_data_directory'] = '/home/git-data'
gitlab_rails['satellites_path'] = '/home/git-data/gitlab-satellites'
gitlab_rails['gitlab_shell_repos_path'] = '/home/git-data/repositories'
あたりで指定すればいいはず(未確認)。私は面倒なのでディレクトリの場所をこの設定で変更しました。

Push Error: RPC failed; result=22, HTTP code = 413  

原因
nginxのアップロードサイズの上限を超えた
解法
/etc/gitlab/gitlab.rbで指定することができます。
nginx['client_max_body_size'] = '512m'
こんな感じで指定すると512MBまでアップロードすることができます。

コメント  

コメントなどありましたらどうぞ。

添付ファイル: filegitlab-7.3.2-repo_url.patch 2168件 [詳細]