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
$ cd /usr/local/src
$ 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
$ cd /usr/local/src
$ 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: git@121.413.102.123 :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 p_w_uploads
## 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服務器
$ vim /home/git/gitlab-shell/config.yml
auth_file: "/home/git/.ssh/authorized_keys"
檢查git服務端上面有沒有這個文件,然後檢查用戶的公鑰有沒有自動寫入到這個文件裏面,如果沒有ssh目錄和authorized_keys文件需要手動創建,目錄權限爲600
參考鏈接:
http://www.centoscn.com/CentosServer/test/2015/0211/4692.html
http://www.restran.net/2015/04/09/gilab-centos-installation-note/