Ubuntu之Gitlab安裝

前提

安裝

安裝依賴包

Needed to compile Ruby and native extensions to Ruby gemsnode

$ sudo apt-get install vim # editor
$ sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake nodejs

安裝Git


$ sudo apt-get install -y git git-core gitweb git-review

# Make sure Git is version 1.7.10 or higher, for example 1.7.12 or 2.0.0
$ git --version

安裝配置Ruby

Ruby的版本能夠經過瀏覽http://cache.ruby-lang.org/pub/ruby/來查到python

下載安裝Ruby

# Remove the old Ruby 1.8 if present
$ sudo apt-get remove ruby1.8

$ mkdir /tmp/ruby && cd /tmp/ruby
$ curl -L --progress http://cache.ruby-lang.org/pub/ruby/ruby-2.2.2.tar.gz | tar xz
$ cd ruby-2.2.2
$ ./configure --disable-install-rdoc
$ make
$ sudo make install

配置Ruby

修改Gem源指向taobao
https://rubygems.org/在國內被牆着
mysql


$ gem source -r https://rubygems.org/
$ gem source -a http://ruby.taobao.org/

安裝 Bundel 命令

$ sudo gem install bundler --no-ri --no-rdoc

爲 Gitlab 建立一個 git 用戶

接下來Gitlab安裝都用這個帳戶nginx

$ sudo adduser --disabled-login --gecos 'GitLab' git

Gitlab Shell

用來 ssh 訪問倉庫的管理軟件git

下載 Gitlab Shell

$ cd /home/git
$ sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell.git
$ cd gitlab-shell
$ sudo -u git -H cp config.yml.example config.yml

修改 gitlab-shell/config.yml

/etc/hosts裏修改gitlab.zjc.com指向本機IP或者配置DNSgithub

$ sudo -u git -H vim /home/git/gitlab-shell/config.yml
gitlab_url: "http://gitlab.zjc.com/"

安裝 GitLab Shell

$ cd /home/git/gitlab-shell
$ sudo -u git -H ./bin/install

Mysql

安裝 Mysql 包

$ sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

給 Gitlab 建立 Mysql 數據庫並受權用戶訪問

下面代碼中gitlabuser, gitlabpassgitlabdb分配給的用戶的用戶名和密碼,用戶能夠自行設置,可是在下面用到的地方要記得修改web

$ sudo mysql -uroot -p
$ > create database gitlabdb;
$ > grant all on gitlabdb.* to 'gitlabuser'@'localhost' identified by 'gitlabpass';

GitLab

下載 GitLab 源代碼,並切換到最新的分支上

$ cd /home/git
$ sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab
$ cd gitlab
# 切換到的最新穩定版號能夠經過瀏覽器瀏覽`https://github.com/gitlabhq/gitlabhq`,點擊branch得到
$ sudo -u git -H git checkout 7-13-stable

配置 GitLab,修改 gitlab.yml

其中 host: 項和 gitlab-shell 中 gitlab_url 的主機一致
cd /home/git/gitlab
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
sudo -u git -H vim config/gitlab.yml
host: gitlab.zjc.com
email_from: gitlab@zjc.com
email_reply_to: noreply@zjc.com
redis

相關目錄-權限

# Make sure GitLab can write to the log/ and tmp/ directories
$ sudo chown -R git log/
$ sudo chown -R git tmp/
$ sudo chmod -R u+rwX,go-w log/
$ sudo chmod -R u+rwX tmp/

# Create directory for satellites
$ sudo -u git -H mkdir /home/git/gitlab-satellites
$ sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites

# Make sure GitLab can write to the tmp/pids/ and tmp/sockets/ directories
$ sudo chmod -R u+rwX tmp/pids/
$ sudo chmod -R u+rwX tmp/sockets/

# Make sure GitLab can write to the public/uploads/ directory
$ sudo chmod -R u+rwX  public/uploads

修改 unicorn.rb 監聽端口爲:8081

$ cd /home/git/gitlab/
$ sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
$ sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
$ sudo -u git -H vim config/unicorn.rb
listen "gitlab.zjc.com:8081", :tcp_nopush => true

配置 GitLab 訪問 mysql 數據庫設置

$ cd /home/git/gitlab/
$ sudo -u git cp config/database.yml.mysql config/database.yml
$ sudo -u git -H vim config/database.yml 
修改 Production 部分,注意這裏用到的gitlabdb表的用戶名和密碼
production: adapter: mysql2 encoding: utf8 collation: utf8_general_ci reconnect: false database: gitlabdb pool: 10 username: gitlabuser password: "gitlabpass" host: localhost socket: /var/run/mysqld/mysqld.sock

設置 GitLab 使用指定郵箱發送郵件

$ cd /home/git/gitlab/
$ sudo -u git -H vim config/environments/production.rb
修改 :sendmail 爲 :smtp
config.action_mailer.delivery_method = :smtp

$ sudo -u git -H cp config/initializers/smtp_settings.rb.sample config/initializers/smtp_settings.rb
$ sudo -u git -H vim config/initializers/smtp_settings.rb
若是郵件服務器配置爲[Ubuntu之郵件服務器 ](http://blog.csdn.net/stwstw0123/article/details/47130293),則要求用戶名應該爲gitlab這樣的形式
若是郵件服務器配置爲[Ubuntu之郵件服務器(Postfix, Dovecot, MySql)](http://blog.csdn.net/stwstw0123/article/details/47373183) ,則要求用戶名應該爲gitlab@vzjc.com這樣的形式
若是是其餘郵件服務器,請查看服務器說明
地址、端口、密碼等請根據實際狀況配置
Gitlab::Application.config.action_mailer.delivery_method = :smtp ActionMailer::Base.smtp_settings = { address: "mail.zjc.com", port: 587, user_name: "gitlab", password: "111111", domain: "zjc.com", authentication: :plain, enable_starttls_auto: true, openssl_verify_mode: 'none' # See ActionMailer documentation for other possible options }

安裝 gem

修改 Gemfile 文件中源指向爲 taobao

$ cd /home/git/gitlab/
$ sudo -u git -H vim Gemfile
source "http://ruby.taobao.org/"

安裝必須的依賴項

$ cd /home/git/gitlab/
$ sudo -u git -H bundle install --deployment --without development test postgres aws kerberos

初始化數據庫並激活高級功能

$ cd /home/git/gitlab/
$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production

輸入 yes 來初始化數據庫、建立相關表,最後會輸出 GitLab Web 管理員用來登陸的帳號和密碼

這裏寫圖片描述

設置 GitLab 啓動服務

$ cd /home/git/gitlab/
$ sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
$ sudo update-rc.d gitlab defaults 21

設置 GitLab 使用 Logrotate 備份 Log

$ cd /home/git/gitlab/
$ sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

檢查GitLab及其環境的配置是否正確:

$ cd /home/git/gitlab/
$ sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
 ENV=production System information System: Ubuntu 14.04 Current User: git Using RVM: no Ruby Version: 2.1.6p336 Gem Version: 2.2.3 Bundler Version:1.10.6 Rake Version: 10.4.2 Sidekiq Version:3.3.0 GitLab information Version: 7.12.2 Revision: 30bffd5 Directory: /home/git/gitlab DB Adapter: mysql2 URL: http://gitlab.zjc.com HTTP Clone URL: http://gitlab.zjc.com/some-project.git SSH Clone URL: git@gitlab.zjc.com:some-project.git Using LDAP: no Using Omniauth: no GitLab Shell Version: 2.6.3 Repositories: /home/git/repositories/ Hooks: /home/git/gitlab-shell/hooks/ Git: /usr/bin/git  

啓動 GitLab 服務

$ sudo /etc/init.d/gitlab restart
  

最後編譯一下

sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production

安裝配置Nginx

安裝Nginx

安裝方法查看本博客 Ubuntu下安裝Nginx、PHP sql

配置Nginx

$ cd /home/git/gitlab
$ sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
$ sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
$ sudo vim /etc/nginx/sites-available/gitlab
server { listen 0.0.0.0:80; listen [::]:80; server_name gitlab.zjc.com;
[...]
 location /uploads/ { [...] proxy_pass http://gitlab.zjc.com:8081; }
[...]
 location @gitlab { [...] proxy_pass http://gitlab.zjc.com:8081; }

重啓Nginx

$ sudo /etc/init.d/nginx restart
# or
$ sudo service nginx restart

訪問

用瀏覽器訪問: `http://gitlab.zjc.com` 用戶名:root 密碼:5iveL!fe

Gitlab帳戶配置

接下來作幾件事兒shell

  • Gitlab用root登陸
  • root用戶配置,修改郵箱爲admin@zjc.com

    admin爲主機上的帳戶,修改郵箱後會向此帳戶發送郵件,點擊郵件裏的驗證,郵箱就算是完全修改過來了


--- 如下爲具體項目配置,請暫時忽略

# 建立admin帳戶
$ sudo adduser admin
$ sudo passwd admin
  • root添加SSH密鑰

    用root登陸Gitlab,更新密鑰爲ssh-rsa *** admin@zjc.com形式, 公鑰根據下面代碼獲得,要根據實際配置

$ ssh-keygen -C admin@zjc.com
$ cat ~/.ssh/id_rsa.pub

這裏寫圖片描述
這裏寫圖片描述

  • Gitlab建立用戶ur

    建立後也須要向ur@zjc.com發送一封郵件,驗證郵箱後才能登陸

  • 爲用戶ur更新SSH公鑰
  • 登陸root,建立Group名字爲DevGroup
  • DevGroup下建立項目TestProject
  • 讓ur加入DevGroup組,角色爲Reporter

  • ur用戶clone Test
    git地址能夠以ur登陸Gitab後在TestProject項目下看到


    zjc@zjc:~$ su - ur
    Password:
    ur@zjc:~$ git clone git@gitlab.zjc.com:DevGroup/TestProject.git
    Cloning into 'TestProject'...
    The authenticity of host 'gitlab.zjc.com (192.168.31.102)' can't be established.
    ECDSA key fingerprint is 46:7e:15:9a:92:44:dd:ef:a5:58:ca:9d:c7:f2:ba:25.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'gitlab.zjc.com,192.168.31.102' (ECDSA) to the list of known hosts.
    warning: You appear to have cloned an empty repository.
    Checking connectivity... done.

  • 配置ur的git信息,若是已經登陸ur就不須要執行su - ur這一步
$ su - ur
$ git config --global user.name 'ur'
$ git config --global user.email 'ur@zjc.com'
  • 測試ur的reporter權限,若是已經登陸ur就不須要執行su - ur這一步

    $ su - ur
    $ cd TestProject
    $ touch testfile
    $ git add testfile
    $ git commit testfile -m 'ur add testfile'
    $ git push
    ...
    Please make sure you have the correct access rights
    and the repository exists.

參考:

http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/ https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/installation.md http://my.oschina.net/u/1158620/blog/289191

相關文章
相關標籤/搜索