新さくらVPSへの引越し vol.3 GitLab + Unicorn + Nginxの導入
新さくらのVPSへの引越しメモ vol.3 GitLab + Unicorn + Nginxの導入
vol.3 目次
いよいよGitLab環境を構築していきます!
依存モジュールの導入
GitLabやRubyで必要になるモジュールを導入します。
ひとおもいに全部一行で入れてもらっても構いません。
- zlib-devel
%sudo yum -y install zlib-devel
- openssl-devel
%sudo yum -y install openssl-devel
- mysql-devel
%sudo yum -y install mysql-devel
- libxml2-devel
%sudo yum -y install libxml2-devel
- libxslt-devel
%sudo yum -y install libxslt-devel
- sqlite-devel
%sudo yum -y install sqlite-devel
- readline-devel
%sudo yum -y install readline-devel
- mysql++-devel
%sudo yum -y install mysql++-devel
- libicu-devel
%sudo yum -y install libicu-devel
- python-devel
%sudo yum -y install python-devel
- python-setuptools
%sudo yum -y install python-setuptools
%sudo yum -y install sendmail
- redis
%sudo yum -y install redis
- python-pip
%sudo easy_install pip
- pygments
%sudo pip install pygments
- redis起動
# 起動 %sudo /etc/init.d/redis start # 自動起動設定 %sudo chkconfig redis on
システム管理ユーザ作成
- gitユーザ作成(※bashでも構わないが/sbin/nologinだとgitコマンドでエラーが出る)
%sudo /usr/sbin/useradd -s /bin/zsh -c 'git version control' -d /home/git git
- gitlabユーザ作成(※bashでも構わない)
%sudo /usr/sbin/useradd -s /bin/zsh -c 'gitlab system' -d /home/gitlab gitlab
- gitolite用公開鍵作成(空パスワード)
%sudo -u gitlab -H ssh-keygen -t rsa %sudo cp /home/gitlab/.ssh/id_rsa.pub /tmp/gitlab.pub %sudo chmod 777 /tmp/gitlab.pub
Nginx
- nginxユーザもここで作成される
%sudo yum -y install nginx
- nginxユーザ公開鍵作成(空パスワード)
%sudo -u nginx -H ssh-keygen -t rsa %sudo cp /var/lib/nginx/.ssh/id_rsa.pub /tmp/nginx.pub %sudo chmod 777 /tmp/nginx.pub
- グループ&パーミッション設定
%sudo chmod 750 /home/git %sudo chmod 750 /home/gitlab %sudo usermod -a -G git gitlab %sudo usermod -a -G gitlab nginx
GitLab用Ruby
%mkdir -p ~/work/repos %cd ~/work/repos %git clone git://github.com/sstephenson/ruby-build.git %cd ruby-build %sudo ./install.sh
- rbenv(Rubyのバージョン管理ツール)
%sudo su gitlab [gitlab] %cd [gitlab] %git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
- .zshrcに設定を追記
[gitlab] %vi ~/.zshrc
以下の記述を追加
export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$HOME/.rbenv/versions/1.9.2-p290/bin:/usr/local/bin:$PATH" eval "$(rbenv init -)" source ~/.rbenv/completions/rbenv.zsh
- 設定を反映
[gitlab] %source ~/.zshrc
- コンパイルオプション設定
[gitlab] %CONFIGURE_OPTS="--with-zlib-include=/usr/include -with-zlib-lib=/usr/lib --with-openssl-dir=/usr/local/ssl --with-mysql-config=/usr/bin/mysql_config"
- 1.9.2-p290のインストール
[gitlab] %rbenv install 1.9.2-p290 [gitlab] %rbenv rehash [gitlab] %rbenv global 1.9.2-p290 [gitlab] %exit
GitLabのインストールガイドに沿うと1.9.2-p290。最新は1.9.3-p125。
Gitolite
- Gitからリポジトリを取得し、gitlabユーザの公開鍵を登録
%cd /var/lib/nginx %sudo -u git -H git clone git://github.com/gitlabhq/gitolite /home/git/gitolite %sudo -u git -H /home/git/gitolite/src/gl-system-install %sudo -u git -H sed -i 's/0077/0007/g' /home/git/share/gitolite/conf/example.gitolite.rc %sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gl-setup -q /tmp/gitlab.pub"
- パーミッション設定
%sudo chmod -R g+rwX /home/git/repositories/ %sudo chown -R git:git /home/git/repositories/
- gitlabユーザでgitolite-adminリポジトリを取得
%sudo su gitlab [gitlab] %cd [gitlab] %git clone ssh://git@localhost:10022/gitolite-admin.git
- nginxユーザをadminに追加する
[gitlab] %cp /tmp/nginx.pub ~/gitolite-admin/keydir/
- 設定ファイルを編集
[gitlab] %vi ~/gitolite-admin/conf/gitolite.conf
nginxユーザを追加
repo gitolite-admin RW+ = gitlab nginx
- 変更をコミット
[gitlab] %cd ~/gitolite-admin [gitlab] %git add keydir/nginx.pub [gitlab] %git commit -a -m 'added nginx user to admin' [gitlab] %git push [gitlab] %exit
- nginxがadminに追加されていることをチェック
%sudo -u nginx -H ssh -p 10022 git@localhost
以下の文言が表示されればOK
hello nginx, this is gitolite v2.2-11-g8c4d1aa running on git 1.7.1 the gitolite config gives you the following access: R W gitolite-admin @R_ @W_ testing Connection to localhost closed.
GitLab
- 依存モジュール導入
%sudo su gitlab [gitlab] %cd [gitlab] %gem install charlock_holmes [gitlab] %gem install bundler
- GitLabのリポジトリを取得
[gitlab] %git clone git://github.com/gitlabhq/gitlabhq.git gitlab
- GitLab各種設定
[gitlab] %cd ~/gitlab [gitlab] %cp config/gitlab.yml.example config/gitlab.yml [gitlab] %cp config/database.yml.sqlite config/database.yml [gitlab] %bundle install --without development test --deployment [gitlab] %bundle exec rake gitlab:app:setup RAILS_ENV=production
GitLab初回ログイン用アカウントが得られる。
login.........admin@local.host password......5iveL!fe
- 設定ファイルの編集
[gitlab] %vi ~/gitlab/config/gitlab.yml
以下の部分を修正
git_host: system: gitolite +admin_uri: ssh://git@your.domain.com:10022/gitolite-admin.git base_path: /home/git/repositories/ +host: your.domain.com git_user: git +port: 10022
- GitLabの動作を確認する
[gitlab] %bundle exec rake gitlab:app:status RAILS_ENV=production [gitlab] %bundle exec rails s -e production => Ctrl-cで抜ける
一行目は以下のような文言が表示されればOK
二行目はサーバが起動した旨が表示されればOK
Starting diagnostic config/database.yml............exists config/gitlab.yml............exists /home/git/repositories/............exists /home/git/repositories/ is writable?............YES The authenticity of host '[your.domin.com]:10022 ([XXX.XX.XXX.XXX]:10022)' can't be established. RSA key fingerprint is 6b:a3:54:77:62:f1:0d:ba:a0:db:56:8f:91:5a:dd:4c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[your.domain.com]:59824,[XXX.XX.XXX.XXX]:10022' (RSA) to the list of known hosts. remote: Counting objects: 14, done. remote: Compressing objects: 100% (11/11), done. remote: Total 14 (delta 0), reused 0 (delta 0) Receiving objects: 100% (14/14), done. Can clone gitolite-admin?............YES UMASK for .gitolite.rc is 0007? ............YES
- rescueの実行
[gitlab] %~/gitlab/resque.sh
Unicorn
[gitlab] %cd ~/gitlab [gitlab] %cp config/unicorn.rb.orig config/unicorn.rb [gitlab] %bundle exec unicorn_rails -c config/unicorn.rb -E production -D [gitlab] %exit
- nginx設定ファイルの編集
%sudo vi /etc/nginx/nginx.conf
次のコードをhttpセクション内の末尾に加える
upstream gitlab { server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket; } server { + listen IPアドレス:80; + server_name your.domin.com; root /home/gitlab/gitlab/public; # individual nginx logs for this gitlab vhost access_log /var/log/nginx/gitlab_access.log; error_log /var/log/nginx/gitlab_error.log; 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 { proxy_redirect off; # you need to change this to "https", if you set "ssl" directive to "on" proxy_set_header X-FORWARDED_PROTO http; + proxy_set_header Host your.domain.com:80; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://gitlab; } }
- nginx起動
%sudo /etc/init.d/nginx start
- gitlab起動スクリプトを作成しておく
%sudo vi /etc/init.d/gitlab
以下の内容を転記する(GitHubより転載)
#! /bin/bash ### BEGIN INIT INFO # Provides: gitlab # Required-Start: $local_fs $remote_fs $network $syslog redis-server # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: GitLab git repository management # Description: GitLab git repository management ### END INIT INFO DAEMON_OPTS="-c /home/gitlab/gitlab/config/unicorn.rb -E production -D" NAME=unicorn DESC="Gitlab service" PID=/home/gitlab/gitlab/tmp/pids/unicorn.pid RESQUE_PID=/home/gitlab/gitlab/tmp/pids/resque_worker.pid case "$1" in start) CD_TO_APP_DIR="cd /home/gitlab/gitlab" START_DAEMON_PROCESS="bundle exec unicorn_rails $DAEMON_OPTS" START_RESQUE_PROCESS="./resque.sh" echo -n "Starting $DESC: " if [ `whoami` = root ]; then sudo -u gitlab sh -c "$CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS && $START_RESQUE_PROCESS" else $CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS && $START_RESQUE_PROCESS fi echo "$NAME." ;; stop) echo -n "Stopping $DESC: " kill -QUIT `cat $PID` kill -QUIT `cat $RESQUE_PID` echo "$NAME." ;; restart) echo -n "Restarting $DESC: " kill -USR2 `cat $PID` kill -USR2 `cat $RESQUE_PID` echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " kill -HUP `cat $PID` kill -HUP `cat $RESQUE_PID` echo "$NAME." ;; *) echo "Usage: $NAME {start|stop|restart|reload}" >&2 exit 1 ;; esac exit 0
- パーミッション追加
%sudo chmod +x /etc/init.d/gitlab
- 起動/停止/再起動/再読込がコマンド一発でできる
%sudo /etc/init.d/gitlab {start|stop|restart|reload}
できた!
http://your.domain.com(またはhttp://IPアドレス)
を開くとGitLabのログイン画面が出るので先ほど入手したアカウントでログインしてみる。
login.........admin@local.host password......5iveL!fe
My profileから名前・Email・パスワード等を更新し、普段使う端末で作った公開鍵を登録してプロジェクトを作成・コミットすれば使えます。