GitLab CEをCentOS 7にインストールし、Apacheを使ってrelative URLで運用する


今回はGitLabを使ったシステムを構築するためのテストとして、テストサーバーにGitLab CEをインストールし、サブディレクトリで運用する方法を記録したいと思います。

恐らく実装方法がいくつかあって作業に手間取ったところがあるので、私がやったことをまとめたいと思います。

前提

CentOS 7で現在Apache 2.4.6を使って運用しているWebサーバー(hoge.com)にgitlab-ce 11.11.3をインストールします。
現在hoge.comにはコンテンツが多数あり、DNSでのCNAME登録が自分の手では出来ないので、サブディレクトリ(/gitlab)で呼べるようにしたいです。つまり、

http://www.hoge.com/gitlab

でGitLabに飛べるようにしたいです(今回はHTTPSではなくHTTPでやることにします)。
GitLabではデフォルトではnginxを使うのですが、今回ではnginxをOffにして、Apacheから動かすようにします。

GitLab CE本体のインストール

こちらを参考に、Community Editionをインストールします。
(公式のinstallation guideの通りにやったら間違ってEnterprise Editionをインストールしてしまいましたのでご注意ください。Community Editionはgitlab-eeをgitlab-ceに置換すれば良いです)
また、SSH, firewall, postfixの設定は既に行っていたので割愛します。

# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# EXTERNAL_URL="http://www.hoge.com/gitlab" yum install -y gitlab-ce 

relative URLで利用するための準備

GitLabのconfiguration optionsのDocs
を参考にします。(これはオプションのようですが)次を実行します。

# gitlab-ctl stop unicorn
# gitlab-ctl stop sidekiq

また、External URLはインストール時に指定したのでURLの設定自体はOKです。

GitLab config fileの編集

元々このサーバーにはgitが入っていて、User名が干渉してしまったため、usernameを変えます。
そして、ここでnginxをdisableして、Apacheで運用するための設定も合わせて書きます。

/etc/gitlab/gitlab.rb
user['username'] = "gitlab"
user['group'] = "git"

gitlab_workhorse['enable'] = true
gitlab_workhorse['listen_network'] = "tcp"
gitlab_workhorse['listen_umask'] = 000
gitlab_workhorse['listen_addr'] = "127.0.0.1:8181"
gitlab_workhorse['auth_backend'] = "http://localhost:8080"
nginx['enable'] = false

編集し終えたら、GitLabの再構築・再起動を行います。

# gitlab-ctl reconfigure
# gitlab-ctl restart

Apacheの設定

GitLabのRepositoryに、Apache 2.4でHTTP運用するためのconf fileが落ちているのでこれを利用します。
また、relative URL用にちょこっと書き直します。

/etc/httpd/conf.d/gitlab.conf
<VirtualHost *:80>
  ServerName www.hoge.com
  ServerSignature Off

  ProxyPreserveHost On

  AllowEncodedSlashes NoDecode

  <Location /gitlab>
    Require all granted

    ProxyPassReverse http://127.0.0.1:8181
    ProxyPassReverse http://www.hoge.com/gitlab

  RewriteEngine on

  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads/.*
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

    </Location>
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public

  ErrorDocument 404 /404.html
  ErrorDocument 422 /422.html
  ErrorDocument 500 /500.html
  ErrorDocument 502 /502.html
  ErrorDocument 503 /503.html

  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded

  ErrorLog /var/log/httpd/error.log
  CustomLog /var/log/httpd/forwarded.log common_forwarded
  CustomLog /var/log/httpd/access.log combined env=!dontlog

</VirtualHost>

書き終えたら、Apacheの再起動をかけます。

# systemctl restart httpd.service

これで、www.hoge.com/gitlabにアクセスすると無事にGitLabが見えます。