[[どぶお/開発しよう!]]
 #floatcontents
 * GitLab 7.3.2のインストール -- CentOS 6.5 [#d79b73e9]
 #anno1(2014-10-13 : 作成)
 
 ** 概要 [#r0f87bf6]
 - [[GitLab Community Edition>https://about.gitlab.com/gitlab-ce/]]
 
 ~以前から使用していたVPSの内容を更新したため、環境の再構築のためGitLabの最新版をインストールしてみましたので記録しておきます。
 最近のバージョンはインストールがものすごく簡単になり、CentOS 6/7などならRPMからさっくりとインストールできます。
 ~標準の状態では、nginxもインストールされ、apacheすら不要ですが、ここでは以下の設定で構築してみました。
 - アクセスURLは''https://mygitlab.example.com/''
 - Apacheでリバースプロキシを挟んでローカルに起動しているGitLabにアクセス
 - アクセスURLをサブディレクトリにするのは設定が面倒なのと推奨されていないのでやらない
 - アクセスURLは''HTTPS''だが、GitLabは''HTTP''で設定する
 
 ~経路はこんな感じ。
  [Webブラウザ] -(HTTPS)-> [https://mygitlab.example.com/ - Apache]
                    -(Apacheリバースプロキシ)->-(HTTP)-> [http://localhost:8000/ - GitLab]
 Apacheを通すことで(どぶお的には)フレキシブルな設定が可能です。
 
 ~実現するためには以下の設定が必要です。
 + GitLabのインストール
 + Apacheのインストールおよびリバースプロキシの設定
 + GitLabのテンプレートの修正(オプション)
 
 ここでのGitLabの一部修正はGitLabのreconfigureスクリプトを実行する際のテンプレートを修正します。これはプロジェクトのリポジトリのURLの表示を修正するためのもので、実施しなくても動作には影響ありません(たぶん)。
 
 ** 注意点 [#ad2c6658]
 GitLabの設定は本来gitlab.ymlなどで行っていましたが、このバージョンではChef(よく知らないけど)を使っているようで、設定ファイル/etc/gitlab/gitlab.rbを初期変数として各設定ファイルをテンプレートから生成します。そのため、/var/opt/gitlab以下の設定ファイルを変更した後、gitlab-ctl reconfigureを行ってしまうと、すべて元に戻ってしまいます。従って、設定を変更する際はgitlab.rbで行う、と留意しておきましょう。
 
 
 ** インストール [#a253d0ec]
 *** yumでインストールしておくもの [#k0bd423f]
 - openssh-server
 - postfix
 
 *** インストール [#s5b02706]
 インストールはドキュメント通りで、さくっと。
  # 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
 これだけで準備完了です。素晴らしい!
 
 *** 環境ファイルの作成と再構築 [#r7eb2c6c]
 今回はリバースプロキシを使うので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&#058;//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の設定 [#q5649da0]
 ここではすでにApache + mod_sslの初期設定は済んでいるものとします。~
 GitLabはNginxを使っており、GitLab専用のサーバや特別なポートを使う場合はそれでいいのですが、例えば80番や443番を使う場合、他のWebサービスが使えなくなってしまい不便です。そのため、Apacheのリバースプロキシを使うことにします。~
 ポイントは以下です。
 + &color(red){''GitLabはSSLモードで稼働していない''};
 + ''https&#058;//mygitlab.example.com/''はhttp&#058;//localhost:8000/のフロントエンドである
 
 特に(1)のSSLモードについて意識しておかないと、ログイン時のページリダイレクトでhttpアクセスになってしまい、Not Foundエラーになってしまいます。
 
 *** VirtualHostの設定 [#k241ede6]
 例えば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'
    &color(red){Header edit Location ^http&#058;//mygitlab.example.com/ https&#058;//mygitlab.example.com/};
  
    <Location />
      Options None
      Allow from all
      &color(red){ProxyPass http&#058;//localhost:8000/};
      &color(red){ProxyPassReverse http&#058;//localhost:8000/};
    </Location>
  </VirtualHost>
 
 赤色で示したところが重要な部分です。
 - Header edit...で、&color(red){リダイレクト時のhttpをhttpsに変換しています};
 - ProxyPassおよびProxyPassReverseでリバースプロキシの設定を行っています
 
 ~この設定で、https&#058;//mygitlab.example.com/にアクセスした時にGitLabにアクセスできるようになるはずです。
 
 ~これで設定はほぼ完了ですが、細かな点が残っています。それは、GitLab自体の設定ではhttp&#058;//localhost:8000となっているため、プロジェクトページのリポジトリアクセス先のSSHおよびHTTPの表示がhttp&#058;//localhost:8000になったまま、ということです。例えば以下のようになります。~
  http&#058;//localhost:8000/dobuo/biokids2014.git
 気にしない、というのも一つの解ですが、余計な設定を見せないというのはサーバ管理の基本ですので以下の方法で正しい表示にします。
 
 ** GitLabソースファイルの修正 [#pc2356c4]
 プロジェクトページのURLはgitlab.ymlの''url''項目で設定可能なようです。~
 ただし、将来的に再構築(gitlab-ctl reconfigure)を行うことがあるなら、テンプレートファイルを修正しておく必要があります。そうでない場合は設定だけを行います。
 
 *** パッチファイルを使う [#qf9493d9]
 以下の修正内容をパッチファイルにまとめましたので、試してみて下さい。
 - &ref(gitlab-7.3.2-repo_url.patch); -- repo_url対応パッチ(GitLab 7.3.2/CentOS 6)
 
  # 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の修正(再構築を行わない場合) [#h61cec7b]
 :/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&#058;//mygitlab.example.com <-- 追加
 
 この修正を行ったらGitLabを再起動して、表示が正しくなっていることを確認します。
  # gitlab-ctl restart
 
 根本的に解決しておきたい場合は以下の方法を試して下さい。
 
 *** GitLabの設定ファイルテンプレートの修正 [#j266d077]
 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
 
 *** 確認メール関連の修正 [#fadcb443]
 ユーザー情報でメールアドレスの変更などを行った場合、メールアドレス確認URLが送信されます。しかし、これも''http&#058;//localhost:8000/''のURLになってしまうので、上記のrepo_urlが使用されるように以下のファイルを修正します。~
 &color(red){赤色};が追加した行、&color(blue){青色};がコメントアウトした行です。
 :/opt/gitlab/embedded/service/gitlab-rails/app/mailers/notify.rb|
  13   add_template_helper MergeRequestsHelper
  14
  15   &color(red){uri = URI(Gitlab.config.gitlab.url)};
  16   &color(red){default_url_options[:host]     = uri.host};
  17   &color(red){default_url_options[:protocol] = uri.scheme};
  18   &color(red){default_url_options[:port]     = uri.port unless Gitlab.config.gitlab_on_standard_port?};
  19 &color(blue){#  default_url_options[:host]     = Gitlab.config.gitlab.host};
  20 &color(blue){#  default_url_options[:protocol] = Gitlab.config.gitlab.protocol};
  21 &color(blue){#  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
 
 ** トラブル [#gee21a87]
 *** アーカイブがダウンロードできない [#c1268950]
 :問題|リポジトリのアーカイブをダウンロードしようとすると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できない [#d9202816]
 :問題|リポジトリにpushしようとすると以下のようなエラーが出る
  % git push origin master
  ...
  &color(red){''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'
 あたりで指定すればいいはず(未確認)。私は面倒なのでディレクトリの場所をこの設定で変更しました。
 
 - 参考
 -- https://github.com/gitlabhq/gitlab-public-wiki/wiki/Trouble-Shooting-Guide#gitlab-shell
 
 *** Push Error: RPC failed; result=22, HTTP code = 413 [#td77ee11]
 :原因|nginxのアップロードサイズの上限を超えた
 :解法|
 /etc/gitlab/gitlab.rbで指定することができます。
  nginx['client_max_body_size'] = '512m'
 こんな感じで指定すると512MBまでアップロードすることができます。
 
 -参考
 -- http://stackoverflow.com/questions/7489813/github-push-error-rpc-failed-result-22-http-code-413
 
 ** コメント [#qc09fd29]
 コメントなどありましたらどうぞ。
 #comment