Skip to content

Instantly share code, notes, and snippets.

@ryosms
Last active December 11, 2015 17:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryosms/4632650 to your computer and use it in GitHub Desktop.
Save ryosms/4632650 to your computer and use it in GitHub Desktop.
さくらのVPSにUbuntu12.04を突っ込んでGitLab 4.1を入れる手順

1. Ubuntuのインストール

2. 初期設定

  • インストール時に作成したuser name/passwordでSSH接続する

  • ローカルマシンで操作 ssh接続用の公開鍵をサーバーにアップロードする

$ scp ~/.ssh/id_rsa.pub <username>@your.domain.name:
  • 公開鍵を登録する
$ mkdir ~/.ssh  
$ mv ~/id_rsa.pub ~/.ssh/authorized_keys  
$ chmod 700 ~/.ssh  
$ chmod 600 ~/.ssh/authorized_keys  

・sshdの設定変更

$ sudo vi /etc/ssh/sshd_config
  • ポート番号の変更
    Port 22
    Port xxxxx

    • rootでのログインを禁止
      PermitRootLogin yes
      PermitRootLogin no

    • パスワードによるログインを禁止
      #PasswordAuthentication yes
      PasswordAuthentication no

    • X11フォワードを無効にする
      X11Forwarding yes
      X11DisplayOffset 10
      X11Forwarding no
      #X11DisplayOffset 10

    sshd再起動

    $ sudo service sshd restart
    

※sshの設定や公開鍵の設定が間違っていたらログインできなくなるので、別ターミナルを立ちあげてssh接続ができることを確認すること

  • ロケールの設定

    • language-packのインストール
      $ sudo apt-get install language-pack-ja

    • .bashrcの修正
      $ vi ~/.bashrc

    export LANG=ja_JP.UTF-8

    • .bashrcの読み込み
      $ source ~/.bashrc
  • logwatchの設定

    • logwatchのインストール
      $ sudo apt-get install logwatch ※postfixの設定は Local Only を選択する

    • デフォルトの設定ファイルをコピーする
      $ sudo cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf

    *編集
    $ sudo vi /etc/logwatch/conf/logwatch.conf

    MailTo = rootMailTo = hoge@your.mail.addr

    • キャッシュ用ディレクトリを作成
      $ sudo mkdir /var/cache/logwatch

2. 必要なパッケージのインストール

  • 以下のパッケージ群をインストールする
sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core openssh-server redis-server postfix checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev
  • Pythonのバージョンを確認
    $ python --version

※python2系(2.5以降)が入ってない場合は、 gitlabのinstallation.md 見てpython2.7を入れる

3. Rubyのインストール

$ mkdir /tmp/ruby && cd /tmp/ruby  
$ curl --progress http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p327.tar.gz | tar xz  
$ cd ruby-1.9.3-p327  
$ ./configure  
$ make  
$ sudo make install  

※make installしてるけど、rbenvとかにしたほうがいいかも?

  • Bundlerをインストール
    $ sudo gem install bundler

4. ユーザーの作成

  • Gitolite用のユーザー作成
$ sudo adduser \  
    --system \  
    --shell /bin/sh \  
    --gecos 'Git Version Control' \  
    --group \  
    --disabled-password \  
    --home /home/git \  
    git  
  • GitLab用のユーザー作成
    $ sudo adduser --disabled-login --gecos 'GitLab' gitlab

  • gitグループに追加
    $ sudo usermod -a -G git gitlab

  • SSHの鍵を作成
    $ sudo -u gitlab -H ssh-keygen -q -N '' -t rsa -f /home/gitlab/.ssh/id_rsa

5. Gitoliteのインストール

  • Gitoliteをクローンする
$ cd /home/git  
$ sudo -u git -H git clone -b gl-v320 https://github.com/gitlabhq/gitolite.git /home/git/gitolite  
  • Gitoliteのスクリプトにパスを通す
$ sudo -u git -H mkdir /home/git/bin  
$ sudo -u git -H sh -c 'printf "%b\n%b\n" "PATH=\$PATH:/home/git/bin" "export PATH" >> /home/git/.profile'  
$ sudo -u git -H sh -c 'gitolite/install -ln /home/git/bin'  
  • gitlabユーザーのSSHの公開鍵をコピーする
$ sudo cp /home/gitlab/.ssh/id_rsa.pub /home/git/gitlab.pub  
$ sudo chmod 0444 /home/git/gitlab.pub  
  • gitlabを管理者としてGitoliteをセットアップする
$ sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gitolite setup -pk /home/git/gitlab.pub"
  • パーミッションと所有権を修正する
$ sudo chmod 750 /home/git/.gitolite/  
$ sudo chown -R git:git /home/git/.gitolite/  
$ sudo chmod -R ug+rwXs,o-rwx /home/git/repositories/  
$ sudo chown -R git:git /home/git/repositories/  
  • known_hostsに登録するため、一度SSHで接続する
    ※localhostと、urlと両方で接続しておく
$ sudo -u gitlab -H ssh git@localhost  
$ sudo -u gitlab -H ssh git@YOUR_DOMAIN_NAME  
$ sudo -u gitlab -H ssh git@YOUR_GITOLITE_DOMAIN_NAME  
  • Gitoliteに接続できることを確認する
$ sudo -u gitlab -H git clone git@localhost:gitolite-admin.git /tmp/gitolite-admin  
$ sudo rm -rf /tmp/gitolite-admin  

6. Databaseのセットアップ

  • MySQLのインストール
$ sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
  • MySQLに接続
$ mysql -u root -p
  • GitLab用のユーザー/production DBを作成し、権限を変更する
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';  
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;  
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';  
mysql> \q  
  • gitlabユーザーでGitLabのproduction DBに接続できるか確認する
$ sudo -u gitlab -H mysql -u gitlab -p -D gitlabhq_production

※MySQLの代わりにPostgreSQLを使う場合は gitlabhq/gitlabhqのdatabase.md を参考にする

7. GitLabのインストール

  • gitlabのホームディレクトリで作業する
$ cd /home/gitlab
  • GitLabのソースをクローンして4-1-stableをチェックアウトする
$ sudo -u gitlab -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab  
$ cd /home/gitlab/gitlab  
$ sudo -u gitlab -H git checkout 4-1-stable
  • GitLabの設定ファイルを作成する
$ sudo -u gitlab -H cp config/gitlab.yml.example config/gitlab.yml  
$ sudo -u gitlab -H vi config/gitlab.yml
* host名を変更する  
ー `host: localhost`  
+ `host: your.server.name`  

* mailの送信アドレスを変更する  
ー `email_from: gitlab@localhost`  
+ `email_from: gitlab@your.domain.name`  

* サポートメールアドレスを変更する  
ー `support_email: support@localhost`  
+ `#support_email: support@localhost`  
※デフォルト値はmailの送信アドレス(email_from)と同じ  
※明示的に指定したい場合は設定する  

* Gitoliteの設定を変更する  
※SSHのPortを変更している場合は変更する  
ー `admin_uri: git@localhost:gitolite-admin`  
+ `admin_uri: ssh://git@localhost:<port>/gitolite-admin`  
ー `# ssh_port: 22`  
+ `ssh_port: <port>`  
  • GitLabが書き込み可能なようにlog/とtmp/ディレクトリのパーミッションを変更する
$ sudo chown -R gitlab log/  
$ sudo chown -R gitlab tmp/  
$ sudo chmod -R u+rwX  log/  
$ sudo chmod -R u+rwX  tmp/  
  • satellitesディレクトリの作成
$ sudo -u gitlab -H mkdir /home/gitlab/gitlab-satellites
  • Unicornの設定ファイルをコピーする
$ sudo -u gitlab -H cp config/unicorn.rb.example config/unicorn.rb
  • DBの設定ファイルを作成する
$ sudo -u gitlab -H cp config/database.yml.mysql config/database.yml  
$ sudo -u gitlab -H vi config/database.yml  

※PostgreSQLの場合は installation.md を参照

* productionのusernameとpasswordを変更する  
ー `username: root`  
ー `password: "secure password"`  
+ `username: gitlab`  
+ `password: "「6. Databaseのセットアップ」で設定したパスワード"`  
  • 必要なGemをインストールする
$ sudo gem install charlock_holmes --version '0.6.9'  
$ sudo -u gitlab -H bundle install --deployment --without development test postgres  

※PostgreSQLの場合は2行目の postgresmysql に変える

  • gitの設定を行う
$ sudo -u gitlab -H git config --global user.name "GitLab"  
$ sudo -u gitlab -H git config --global user.email "gitlab@your.domain.name"  
  • GitLabのHookスクリプトをセットアップする
$ sudo cp ./lib/hooks/post-receive /home/git/.gitolite/hooks/common/post-receive  
$ sudo chown git:git /home/git/.gitolite/hooks/common/post-receive  
  • セットアップ
$ sudo -u gitlab -H bundle exec rake gitlab:setup RAILS_ENV=production

※AdministratorのログインIDとパスワードが表示されるのでメモっておく

login........admin@local.host  
password.....5iveL!fe  
  • Init用のスクリプトを作成する
$ sudo curl --output /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/master/init.d/gitlab  
$ sudo chmod +x /etc/init.d/gitlab  
$ sudo update-rc.d gitlab defaults 21  
  • ここまでの設定がうまくできているか確認する
$ sudo -u gitlab -H bundle exec rake gitlab:env:info RAILS_ENV=production  

※以下を実行することで詳細な検証が可能

$ sudo -u gitlab -H bundle exec rake gitlab:check RAILS_ENV=production  
  • GitLabを開始する
$ sudo service gitlab start  

もしくは

$ sudo /etc/init.d/gitlab restart  

8. Nginxのセットアップ

  • インストール
$ sudo apt-get install nginx
  • サイトの設定ファイルを作成する
$ sudo curl --output /etc/nginx/sites-available/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/master/nginx/gitlab  
$ sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab  
$ sudo vi /etc/nginx/sites-enabled/gitlab  
* サーバーのIP、サーバー名を修正する  
ー `listen YOUR_SERVER_IP:80 default_server;`  
+ `listen xxx.xxx.xxx.xxx:80 default_server;`  

ー `server_name YOUR_SERVER_FQDN;`  
+ `server_name your.server.name;`
  • nginxを再起動する
$ sudo /etc/init.d/nginx restart  

9. 確認

  • ブラウザでアクセスしてみて表示されることを確認する。
    502 Bad Gateway が表示される場合は、GitLabとNginxを再起動してみる

  • 初期Adminのユーザー名/パスワードは共通なので、パスワードは変更すること!

  • もしくは、新規Adminユーザーを作成して、初期Adminは削除する

参考URL

カスタムOSインストールガイド Ubuntu 12.04 | Sakura Internet
さくらの VPS : Ubuntu 12.04 をインストール | oceanweb
logwatch で毎日、ログをメールで確認する | transitive.info
installation.md | gitlabhq
database.md | gitlabhq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment