GitLab,是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的Git項目倉庫,可經過Web界面進行訪問公開的或者私人項目。html
它擁有與GitHub相似的功能,可以瀏覽源代碼,管理缺陷和註釋。能夠管理團隊對倉庫的訪問,它很是易於瀏覽提交過的版本並提供一個文件歷史庫。團隊成員能夠利用內置的簡單聊天程序(Wall)進行交流。它還提供一個代碼片斷收集功能能夠輕鬆實現代碼複用,便於往後有須要的時候進行查找。node
1、安裝源和依賴包mysql
$ cd /usr/local/srclinux
#增epel源,若是你是i686系統,請把x86_64修改下。nginx
$ wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://www.fedoraproject.org/static/0608B895.txtgit
$ rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6github
$ rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmweb
2、確認是否安裝成功redis
$ rpm -qa gpg*sql
gpg-pubkey-0608b895-4bd22942
3、安裝依賴包
$ 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
4、編譯安裝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
5、安裝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
6、完成後驗證ruby
$which ruby
/usr/local/bin/ruby
$ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
7、安裝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
8、建立git須要的系統用戶
useradd git
9、安裝mysql
$ yum install -y mysql-server mysql-devel
$ chkconfig mysqld on
$ service mysqld start
10、建立git數據庫用戶
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY 'gitpwd';
11、建立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;
12、配置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
十3、安裝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 "example@example.com"
$ 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
十4、安裝gem
#修改gemfile配置,改成淘寶的ruby源,修改成
cd /home/git/gitlab
$ vi Gemfile
source 'https://ruby.taobao.org/'
#執行命令,這一步的時間會等好久
$ bundle install --deployment --without development test postgres aws
十5、安裝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
十6、初始化數據庫
$ cd /home/git/gitlib
bundle exec rake gitlab:setup RAILS_ENV=production
#能夠設置管理員密碼(此步驟可省略。。。)
$ bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=newpassword
十7、下載服務腳本,退出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
十9、訪問gitlab,系統默認admin可是我用默認密碼我登陸不了,這裏我本身註冊了用戶名,註冊完密碼是經過郵件發送的。
#用戶名:xx
#密碼:xx
二10、至此gitlab安裝完成,登陸到gitlab上面能夠進行項目的建立、用戶的添加、刪除、權限分配。
問題總結
一、服務端新建好用戶後沒法經過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/