LoginSignup
112
108

More than 5 years have passed since last update.

CentOS 6.5にGitLab 6.8をインストールとSSL導入

Last updated at Posted at 2014-04-29

CentOS 6.5にGitLab 6.8をインストールしてSSL導入

環境は2014/4/25時点で全て最新のものを使用。
CentOS:6.5
GitLab:6.8
GIt:1.9.2
Ruby:2.1.0
nginx:1.6.0

以下のサイトを参考にさせて頂きました。
ありがとうございました。
gitlabhq/gitlabhq
https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/installation.md
$web->{note}; GitLab 5.0 を CentOS 6.4 にインストールする
http://n8.hatenablog.com/entry/2013/04/24/230340
dogmap.jp nginx で ssl 設定をする
http://dogmap.jp/2011/05/10/nginx-ssl/

Gitインストール

yumでも入れられるが最新版を欲しいのでsrcビルド。

# cd /tmp
# wget https://www.kernel.org/pub/software/scm/git/git-1.9.2.tar.gz
# tar zxvf git-1.9.2.tar.gz
# cd git-1.9.2
# ./configure --prefix=/usr/local
# make
# sudo make install

Gitの設定。

# git config --global user.name  "GitLab"
# git config --global user.email "gitlab@git.example.jp"

Rubyインストール

# cd /tmp
# wget http://ftp.ruby-lang.org/pub/ruby/ruby-2.1.0.tar.gz
# tar zxvf ruby-2.1.0.tar.gz
# cd ruby-2.1.0
# ./configure
# make
# sudo make install
# gem install bundler
# gem install charlock_holmes --version '0.6.9'

実行ユーザー作成

# useradd -c 'GitLab' -s /bin/bash git

GitLabから各ユーザーが登録する鍵はgitユーザのauthorized_keysに書き込まれる。
あらかじめ作成しておく必要がある。

# su - git
$ cd /home/git
$ mkdir .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
$ chmod 700 .ssh

GitLab shellのインストール

$ cd /home/git
$ git clone https://github.com/gitlabhq/gitlab-shell.git
$ cd gitlab-shell/
$ git checkout v1.9.4
$ git checkout -b v1.9.4
$ cp config.yml.example config.yml
$ vi config.yml
$ diff config.yml{.example,}
5c5
< gitlab_url: "http://localhost/"
---
> gitlab_url: "http://git.example.jp/"
$ ./bin/install

データベース作成

# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# yum --enablerepo=remi install mysql mysql-server mysql-devel mysql-libs

MySQLの設定変更。
外部からはDBにログインできないように設定。

# vi /etc/my.cnf

[mysqld]
bind-address = 127.0.0.1
# /etc/init.d/mysqld start
# chkconfig mysqld on

Gitlab用のDBを作成。

# mysql

>CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'パスワード';
>CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
>GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';
>\q

接続出来ることを確認。

# mysql -u gitlab -p -D gitlabhq_production

Redisのインストール

# yum install redis
# /etc/init.d/redis start
# chkconfig redis on

GitLabのインストール

# su - git
$ cd /home/git

srcの取得。

$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab
$ cd gitlab
$ git checkout 6-8-stable

設定ファイルの変更。

$ cp config/gitlab.yml.example config/gitlab.yml
$ vi config/gitlab.yml

$ diff config/gitlab.yml{.example,}
18c18
<     host: localhost
---
>     host: git.example.jp
30c30
<     email_from: gitlab@localhost
---
>     email_from: gitlab@git.example.jp

レポジトリの場所と、SSHポートを変更したい場合もここで変更。

$ diff config/gitlab.yml{.example,}
219c219
<     repos_path: /home/git/repositories/
---
>     repos_path: new path
227c227
<     # ssh_port: 22
---
>     ssh_port: new port

必要なディレクトリを作成。

$ chown -R git log/
$ chown -R git tmp/
$ chmod -R u+rwX  log/
$ chmod -R u+rwX  tmp/
$ mkdir /home/git/gitlab-satellites
$ mkdir tmp/pids/
$ chmod -R u+rwX  tmp/pids/
$ cp config/unicorn.rb.example config/unicorn.rb

GitLabのDB設定

$ cp config/database.yml.mysql config/database.yml
$ vi config/database.yml

$ diff config/database.yml{.mysql,}
10,11c10,11
<   username: root
<   password: "secure password"
---
>   username: gitlab
>   password: "MySQLのパスワード"
24,25c24,25
<   username: root
<   password: "secure password"
---
>   username: gitlab
>   password: "MySQLのパスワード"
37,38c37,38
<   username: root
<   password:
---
>   username: gitlab
>   password: "MySQLのパスワード"

Gemsのインストール

長いので待ち続ける(Log出ないので注意)

$ cd /home/git/gitlab
$ bundle install --deployment --without development test postgres

DBの初期化とセットアップ

$ bundle exec rake gitlab:setup RAILS_ENV=production

実行時にGemのパッケージが無いとかbundleのUpdateとか言われるかも。
その場合は導入して再度実行。成功するまで繰り返し。

InitScriptのインストール

# cp lib/support/init.d/gitlab /etc/init.d/gitlab
# cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
# chmod +x /etc/init.d/gitlab
# vi /etc/init.d/gitlab
# diff /etc/init.d/gitlab
?c?
< NAME=git
---
> NAME=gitlab

ローテンションログの設定

# cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

状態チェック

# bundle exec rake gitlab:env:info RAILS_ENV=production

Assetsのビルド

# bundle exec rake assets:precompile RAILS_ENV=production

起動とサービス登録

# /etc/init.d/gitlab restart
# chkconfig gitlab on

状態チェック

gitlab:checkを実行すると正常にインストール出来るかチェックされる。
エラー時は赤文字が出るので修正する。
修正にどのコマンドを実行するばいいか教えてくれるのでその通りにする。

$ bundle exec rake gitlab:check RAILS_ENV=production

はまり点としてはGitLabのファイル群に所有者がrootのものがあった。
なので、全てgitに変更した。

$ cd /home/git/gitlab
$ chown -R git:git .

Nginxのインストール

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# yum install nginx

起動とサービス登録。

# chkconfig nginx on
# /etc/init.d/nginx start

GitLabの設定ファイルを配置。

# cp lib/support/nginx/gitlab /etc/nginx/conf/gitlab

SSLを導入

サーバー証明書持ってないのでオレオレ証明書を作成。

# mkdir /etc/nginx/cert
# cd /etc/nginx/cert
# openssl genrsa -des3 -out server.key 1024
# openssl req -new -key server.key -out server.csr
# cp server.key server.key.org
# openssl rsa -in server.key.org -out server.key
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

設定ファイルを変更。

# vi/etc/nginx/conf/gitlab

以下全容を記述。

upstream gitlab {
  server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}

server {
#  listen *:80 default_server;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
#  server_name YOUR_SERVER_FQDN;     # e.g., server_name source.example.com;
  server_name 127.0.0.1;
  server_tokens off;     # don't show the version number, a security best practice
  root /home/git/gitlab/public;

  listen 443 default ssl;
  ssl on;
  ssl_certificate /etc/nginx/cert/server.crt;
  ssl_certificate_key /etc/nginx/cert/server.key;

  # Increase this if you want to upload large attachments
  # Or if you want to accept large git objects over http
  client_max_body_size 20m;

  # individual nginx logs for this gitlab vhost
  #access_log  /var/log/nginx/gitlab_access.log;
  #error_log   /var/log/nginx/gitlab_error.log;

  #auth_basic  "Restricted";
  #auth_basic_user_file "/etc/nginx/.htpasswd";

  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;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (gitlab unicorn)
  location @gitlab {
    # If you use https make sure you disable gzip compression 
    # to be safe against BREACH attack
    # gzip off;

    proxy_read_timeout 300; # Some requests take more than 30 seconds.
    proxy_connect_timeout 300; # Some requests take more than 30 seconds.
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;

    proxy_pass         http://gitlab;
    #proxy_redirect     http:// https://;

    access_log  /var/log/nginx/gitlab_access.log;
    error_log   /var/log/nginx/gitlab_error.log;
  }

  # Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
  location ~ ^/(assets)/  {
    root /home/git/gitlab/public;
    gzip_static on; # to serve pre-gzipped version
    expires max;
    add_header Cache-Control public;
  }

  error_page 502 /502.html;
}

再起動。

# /etc/init.d/nginx restart

GitLabへアクセス

以下のURLでGitLabのログイン画面が表示される。

https://IPアドレス

初期のIDとパスは以下。

ID:root
PASS:5iveL!fe

再起動時にMySQLより早く起動してしまう問題

再起動した際に、GitLabよりMySQLサービスが早く立ち上がり以下のエラーが発生。

# Can't connect to local MySQL server through socket

起動スクリプトに起動順序の指定が無いのが原因のため以下を追加する。

# vi /etc/init.d/gitlab
# diff /etc/init.d/gitlab
15c15
< # chkconfig: - 75 25

再度サービス登録。

# chkconfig --delete gitlab
# chkconfig gitlab on
# chkconfig --list gitlab
gitlab         0:off   1:off   2:on    3:on    4:on    5:on    6:off
112
108
6

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
112
108