LoginSignup
41
47

More than 5 years have passed since last update.

GitlabのPortを変更したうえでサブディレクトリで運用する

Last updated at Posted at 2015-11-09

2016/05/24追記

久々にドキュメントを確認したら、Gitlab Omnibus Package Ver. 8.5以降では/etc/gitlab/gitlab.rbを

/etc/gitlab/gitlab.rb
#before
external_url 'http://example.com'

#after
external_url 'http://example.com/gitlab'

のように書き換えるだけで良くなったようです。技術の進歩ってスゲー!!

前提

以下の記述はOmnibus Package Ver. 8.4以前もしくはソースからビルドした場合のものです。一応残しておきます。

扱わないこと

  • Apacheの導入
  • Gitlabの細かい運用上での設定
  • ファイアーウォールの設定

目的

  • Gitlabは初期設定だと80番のポートを使ってしまう。これはApacheと競合する。なので他のポート番号に振り分ける。
  • ポート番号がURLに見えているのがダサいので、サブドメイン/gitlabでアクセス出来るようにする
  • 公式が推奨している方法だと、設定ファイルを書き換えてgitlab-ctl reconfigureする度に手間が必要なので、それを無くす

Gitlabの導入

今回はOmnibus packageを使用する。

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

設定の記述をする

Gitlabの設定ファイルは/etc/gitlab/gitlab.rbに記述されているので、適宜修正する。
今回は80番の代わりとして8080番を使うことを想定する。

gitlab.rb
 # nginx['listen_port'] = nil #override...
 
 nginx['listen_port'] = 8080 #override...

テンプレートを書き換える

次の部分をそれぞれ書き換えていく

  • コメントアウト
/opt/gitlab/embedded/cookbooks/gitlab/templates/default/gitlab.yml.erb
 # relative_url_root: /gitlab
 
 relative_url_root: /gitlab
  • 末端に追記
/opt/gitlab/embedded/cookbooks/gitlab/templates/default/unicorn.rb.erb
 ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
  • 追記
/opt/gitlab/embedded/cookbooks/gitlab/templates/default/gitlab-shell-config.yml.erb
 gitlab_url:"<%=@api_url%>"
 
 gitlab_url:"<%=@api_url%>/gitlab"
  • 色々変更
/opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx-gitlab-http.conf.erb
 location / {
   ## Serve static files from defined root folder.
   ## @gitlab is a named location for the upstream fallback, see below.
   try_files $uri $uri/index.html $uri.html @gitlab;
 }
 
 location /gitlab {
   alias /opt/gitlab/embedded/service/gitlab-rails/public;
   try_files $uri $uri/index.html $uri.html @gitlab;
 }

 ===省略===

 ## Enable gzip compression as per rails guide:
 ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
 ## WARNING: If you are using relative urls remove the block below
 ## See config/application.rb under "Relative url support" for the list of
 ## other files that need to be changed for relative url support
 location ~ ^/(assets)/ {
   root /opt/gitlab/embedded/service/gitlab-rails/public;
   gzip_static on; # to serve pre-gzipped version
   expires max;
   add_header Cache-Control public;
 }
 
 ## Enable gzip compression as per rails guide:
 ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
 ## WARNING: If you are using relative urls remove the block below
 ## See config/application.rb under "Relative url support" for the list of
 ## other files that need to be changed for relative url support
 #location ~ ^/(assets)/ {
 #  root /opt/gitlab/embedded/service/gitlab-rails/public;
 #  gzip_static on; # to serve pre-gzipped version
 #  expires max;
 #  add_header Cache-Control public;
 #}
  • コメントアウトを外す(ここはテンプレートではない)
/opt/gitlab/embedded/service/gitlab-rails/config/application.rb
 # config.relative_url_root = "/gitlab"
 
 config.relative_url_root = "/gitlab"
  • 全部終わったらsudo gitlab-ctl reconfigure

Apacheの設定ファイルを作成

  • リバースプロキシ用の設定ファイルを作成
/etc/httpd/conf.d/gitlab.conf
<IfModule mod_proxy.c>
  ProxyRequests off
  ProxyPass /gitlab http://127.0.0.1:8080/gitlab
  ProxyPassReverse /gitlab http://127.0.0.1:8080/gitlab
  ProxyPass /assets http://127.0.0.1:8080/gitlab/assets
  ProxyPassReverse /assets http://127.0.0.1:8080/gitlab
</IfModule>

参考

  1. gitlabをreverse proxy下で動かしたりsubdirectory設定したり
  2. GitLabをサブディレクトリ(Subdirectory)でインストールしてWordPressと共存させる
  3. Gitlab を Apache と共存させてサブディレクトリで動かす
41
47
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
41
47