天天看點

Centos6.5_x64-GitLab搭建私有GitHub

   GitLab,是一個利用 Ruby on Rails 開發的開源應用程式,實作一個自托管的Git項目倉庫,可通過Web界面進行通路公開的或者私人項目。

它擁有與GitHub類似的功能,能夠浏覽源代碼,管理缺陷和注釋。可以管理團隊對倉庫的通路,它非常易于浏覽送出過的版本并提供一個檔案曆史庫。團隊成員可以利用内置的簡單聊天程式(Wall)進行交流。它還提供一個代碼片段收集功能可以輕松實作代碼複用,便于日後有需要的時候進行查找。

一、安裝源和依賴包

$ cd /usr/local/src

#增epel源,如果你是i686系統,請把x86_64修改下。

$ wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6  https://www.fedoraproject.org/static/0608B895.txt

$ rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

$ rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

二、确認是否安裝成功

$ rpm -qa gpg*

gpg-pubkey-0608b895-4bd22942

三、安裝依賴包

$ yum -y update

$ yum -y groupinstall 'Development Tools'

$ yum -y install readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel openssl-devel curl-devel expat-devel db4-devel byacc sqlite-devel libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel libicu libicu-devel system-config-firewall-tui redis sudo wget crontabs logwatch logrotate perl-Time-HiRes  cmake libcom_err-devel.i686 libcom_err-devel.x86_64

四、編譯安裝git

$ wget https://www.kernel.org/pub/software/scm/git/git-2.4.5.tar.gz

$ yum install zlib-devel perl-CPAN gettext curl-devel expat-devel gettext-devel openssl-devel

$ yum remove git -y

$ tar xf git-2.4.5.tar.gz

$ cd git-2.4.5

$ ./configure --prefix=/usr/local/git

$ make;make install

五、安裝ruby,ruby版本需要2.0+,是以先解除安裝系統已存在的

$ yum remove ruby

$ wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.6.tar.gz

$ tar xf ruby-2.1.6.tar.gz

$ cd ruby-2.1.6 && ./configure --prefix=/usr/local/ --disable-install-rdoc && make && make  install

六、完成後驗證ruby

$which ruby

/usr/local/bin/ruby

$ruby -v

ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]

七、安裝bundler

Gem是一個管理Ruby庫和程式的标準包,它通過Ruby Gem(如 http://rubygems.org/ )源來查找、安裝、更新和解除安裝軟體包,非常的便捷

由于國内網絡原因(你懂的),導緻 rubygems.org 存放在 Amazon S3 上面的資源檔案間歇性連接配接失敗。是以你會與遇到 gem install rack 或 bundle install 的時候半天沒有響應,具體可以用 gem install rails -V 來檢視執行過程

将ruby預設的gem源替換成淘寶的既可以正常安裝軟體

$ gem sources --remove https://rubygems.org/

$ gem sources -a https://ruby.taobao.org/

$ gem sources -l

$ gem install bundler --no-doc

八、建立git需要的系統使用者

useradd git

九、安裝mysql

$ yum install -y mysql-server mysql-devel

$ chkconfig mysqld on

$ service mysqld start

十、建立git資料庫使用者

mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY 'gitpwd';

十一、建立git需要的資料庫、并授權一個連接配接git資料庫的使用者

mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq`.* TO 'git'@'localhost';

mysql> flush privileges;

十二、配置redis,

前面安裝依賴包的時候已經使用yum把redis安裝好了。

$ chkconfig redis on

$ cp /etc/redis.conf /etc/redis.conf.orig

修改redis監聽端口

sed 's/^port .*/port 0/' /etc/redis.conf.orig |tee /etc/redis.conf

配置redis,增加redis配置内容

$ echo 'unixsocket /var/run/redis/redis.sock' |tee -a /etc/redis.conf

$ echo -e 'unixsocketperm 0770' |tee -a /etc/redis.conf

修改redis目錄權限

$ chown redis:redis /var/run/redis

$ chmod 755 /var/run/redis

啟動redis服務

$service redis restart

附加git到redis組

usermod -aG redis git

十三、安裝gitlab配置

$ cd /home/git

#添加下面内容,不驗證SSL,否則 git clone 時會出現錯誤 Peer certificate cannot be authenticated with known CA certificates

$ vi /home/git/.bash_profile 

export GIT_SSL_NO_VERIFY=1

#下載下傳源碼,安裝Gitlab

$ su - git

$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab

#修改配置,并修改gitlab配置檔案

$ cd gitlab/

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

$ vim  config/gitlab.yml

gitlab:

   ## Web server settings (note: host is the FQDN, do not include http://)

   host: 121.413.102.123          //這裡的ip是你利用http或者ssh通路git的時候位址

   port: 80                    //http方式通路git的端口

   https: false

   time_zone: 'Beijing'        //将時區改為北京

git:

    #bin_path: /usr/bin/git

    bin_path: /usr/local/git/bin/git

#建立目錄,配置unicorn

$ mkdir /home/git/gitlab-satellites

$ chmod 750 /home/git/gitlab-satellites    

$ cp config/unicorn.rb.example config/unicorn.rb

#修改unicorn配置檔案,特别注意:比較差配置的機器,注意将unicorn.rb中的timeout設定大一點,因為第一次啟動的時候Gitlab需要初始化,如果timeout太小,由于需要執行較長時間,導緻無法正常啟動,出現502錯誤

$ vim config/unicorn.rb

worker_processes 4

#拷貝配置檔案rack_attack.rb

$ cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

#定義全局的使用者和郵箱,會将配置寫入到gitlab.yml配置檔案

$ git config --global user.name "GitLab"

$ git config --global user.email "[email protected]"

$ git config --global core.autocrlf input

#編輯連接配接redis配置,拷貝配置

$ cp config/resque.yml.example config/resque.yml

#配置gitlab資料庫檔案

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

#編輯git連接配接資料庫配置檔案

$ vim config/database.yml

production:

  adapter: mysql2

  encoding: utf8

  collation: utf8_general_ci

  reconnect: false

  database: gitlabhq

  pool: 10

  username: git

  password: "gitpwd"

  host: localhost

  socket: /var/lib/mysql/mysql.sock

#修改檔案權限,隻有git使用者可讀

chmod 660 config/database.yml

十四、安裝gem

#修改gemfile配置,改為淘寶的ruby源,修改為

cd /home/git/gitlab

$ vi Gemfile

source 'https://ruby.taobao.org/'

#執行指令,這一步的時間會等很久

$ bundle install --deployment --without development test postgres aws

十五、安裝gitlab-shell

$ yum install nodejs

$ su  -  git

$ cd  /home/git/

$ git clone https://github.com/gitlabhq/gitlab-shell.git

$ cd gitlab-shell

$ mkdir /home/git/repositories

$ cp config.yml.example config.yml

#修改gitlab-shell配置檔案

$ vim /home/git/gitlab-shell/config.yml

user: git

gitlab_url: "http://121.413.102.123 " 

http_settings:

  self_signed_cert: true

repos_path: "/home/git/repositories/"

auth_file: "/home/git/.ssh/authorized_keys"

redis:

  bin: "/usr/bin/redis-cli"

  namespace: resque:gitlab

  socket: "/var/run/redis/redis.sock"

log_level: INFO

audit_usernames: false

十六、初始化資料庫

$ cd /home/git/gitlib

bundle exec rake gitlab:setup RAILS_ENV=production

#可以設定管理者密碼(此步驟可省略。。。)

$ bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=newpassword

十七、下載下傳服務腳本,退出git使用者使用root使用者執行下面的指令

$ exit 

$ wget -O /etc/init.d/gitlab  https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/init/sysvinit/centos/gitlab-unicorn

$ chmod +x /etc/init.d/gitlab

$ chkconfig --add gitlab

$ chkconfig gitlab on

#設定logrotate

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

#檢測應用狀态

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

System information

System:          CentOS 6.6

Current User:     root

Using RVM:     no

Ruby Version:     2.1.6p336

Gem Version:     2.2.3

Bundler Version:1.10.5

Rake Version:     10.4.2

Sidekiq Version:3.3.0

GitLab information

Version:     7.13.0.pre

Revision:     32330e8

Directory:     /home/git/gitlab

DB Adapter:     mysql2

URL:          http://121.43.102.23:8080

HTTP Clone URL:     http://121.413.102.123:8080/some-project.git

SSH Clone URL:     [email protected] :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

#執行指令

$ bundle exec rake assets:precompile RAILS_ENV=production

#啟動gitlab服務

$ service gitlab start

十八,配置gitlab 的web服務

$ yum -y install nginx

$ chkconfig nginx on

#下載下傳gitlab配置檔案,不使用SSL

$ wget -O /etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab --no-check-certificate

#編輯gitlab配置檔案

$ vi /etc/nginx/conf.d/gitlab.conf

  listen 0.0.0.0:80 default_server;     //設定登陸gitlab 的登陸端口。

  #listen [::]:80 default_server;

  #server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com

  server_name 121.413.102.123 ; #修改為本機ip

  server_tokens off; ## Don't show the nginx version number, a security best practice

  root /home/git/gitlab/public;

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

  client_max_body_size 256m;     //修改為256

#添加nginx使用者到git組

$ usermod -a -G git nginx

$ chmod g+rx /home/git/

#啟動nginx服務

service nginx start

#修改gitlab_url為nginx中配置的相應端口

$ vi /home/git/gitlab-shell/config.yml

gitlab_url: "http://121.413.102.123"

#建立軟連接配接。否則用戶端在clone的時候報錯

$ ln -s /usr/local/git/bin/git-receive-pack /usr/bin/

$ ln -s /usr/local/git/bin/git-upload-pack  /usr/bin/ 

#gitlab郵件服務設定檔案。

$cd /home/git/gitlab/config/initializers/

$cp smtp_settings.rb.sample smtp_settings.rb

十九、通路gitlab,系統預設admin但是我用預設密碼我登入不了,這裡我自己注冊了使用者名,注冊完密碼是通過郵件發送的。

#使用者名:xx

#密碼:xx

二十、至此gitlab安裝完成,登入到gitlab上面可以進行項目的建立、使用者的添加、删除、權限配置設定。

問題總結

1、服務端建立好使用者後無法通過ssh方式通路git伺服器

檢查git服務端上面有沒有這個檔案,然後檢查使用者的公鑰有沒有自動寫入到這個檔案裡面,如果沒有ssh目錄和authorized_keys檔案需要手動建立,目錄權限為600

參考連結:

<a href="http://www.centoscn.com/CentosServer/test/2015/0211/4692.html" target="_blank">http://www.centoscn.com/CentosServer/test/2015/0211/4692.html</a>

<a href="http://www.restran.net/2015/04/09/gilab-centos-installation-note/" target="_blank">http://www.restran.net/2015/04/09/gilab-centos-installation-note/</a>

<a href="http://dl528888.blog.51cto.com/2382721/1213286" target="_blank">http://dl528888.blog.51cto.com/2382721/1213286</a>

本文轉自 張玉坡 51CTO部落格,原文連結:http://blog.51cto.com/fighter/1678308