一、前提
- 系統:Centos 6.5
- 軟件版本:gitlab-7.8.4
- Selinux:關閉
- 防火牆規則:先清空(搭建好了后續自己添加相關放行規則)
二、yum源配置和相關依賴包
1.添加epel源和PUIAS_6_computational源
# yum -y install epel-release
創建該文件並添加以下內容:
# vim /etc/yum.repos.d/PUIAS_6_computational.repo [PUIAS_6_computational] name=PUIAS computational Base $releasever - $basearch mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist #baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
獲取和導入相關驗證文件
# wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-puias http://springdale.math.ias.edu/data/puias/6/x86_64/os/RPM-GPG-KEY-puias # rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-puias # yum repolist
2.安裝依賴包
# yum install -y vim lrzsz wget gcc gcc-c++ make makeconf cmake # 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 git cmake libcom_err-devel.i686 libcom_err-devel.x86_64 nodejs cmake libicu-devel libicu openssl openssl-devel 查看安裝的git版本,需要1.7.12以上,有PUIAS源會裝1.8 # git --version
三、安裝Ruby
1.查看是否安裝有Ruby
# rpm -qa | grep ruby
2.安裝Ruby
# mkdir /tmp/ruby && cd /tmp/ruby # curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz # cd ruby-2.1.2 # ./configure --disable-install-rdoc # make # make prefix=/usr/local install
3.安裝Bundler Gem
# gem install bundler --no-doc
可能會報以下錯誤,這是因為國內網絡導致rubygems.org存放在Amazon S3上面的資源文件間接性鏈接失敗,用國內的RubyGems鏡像(參見http://ruby.taobao.org/)替換官方鏡像
更換鏡像地址:
# gem sources --remove https://rubygems.org/ # 刪除原鏡像地址 # gem sources -a https://ruby.taobao.org/ # 添加新鏡像地址
查看是否更換成功:
再次安裝:
四、創建系統用戶git
# adduser --system --shell /bin/bash --comment 'GitLab' --create-home --home-dir /home/git/ git 讓git用戶可以找到/usr/local/bin命令,並可以使用sudo # visudo 找到這一行: Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin 在后面添加 /usr/local/bin : Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin 再找到這一行: root ALL=(ALL) ALL 在下一行添加: git ALL=(ALL) ALL
五、安裝MySQL
1.前提
這里的數據庫需要安裝Mysql 5.5以上的版本,而epel源默認只有5.1,所以需要添加有Mysql 5.5的源
# rpm -qa | grep mysql # 查看是否安裝比較低版本的mysql,然后卸載 # yum remove -y mysql* # yum install -y mysql55-server mysql55-devel mysql55 postfix # 安裝5.5版本 # chkconfig mysqld on # 設置開機啟動 # service mysqld start
要求是InnoDB引擎,登陸查看是否為InnoDB引擎
如果不是可以用SET storage_engine=INNODB設置,或者修改my.cnf文件后重啟mysql
2.配置GitLab使用的數據庫
> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`; > GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'git'@'localhost' identified by 'git'; > GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'git'@'127.0.0.1' identified by 'git'; > flush privileges; > exit;
六、安裝Redis
安裝Reids
# yum -y install redis # chkconfig redis on # cp /etc/redis.conf /etc/redis.conf.orig
下面這條命令會把redis.conf的端口設置為0,而redis.conf.orig不變
# sed 's/^port .*/port 0/' /etc/redis.conf.orig | sudo tee /etc/redis.conf # echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis.conf # echo -e 'unixsocketperm 0770' | sudo tee -a /etc/redis.conf
授權/var/run/redis給redis權限
# mkdir -p /var/run/redis/ # chown redis:redis /var/run/redis # chmod 755 /var/run/redis
將git加入redis組,並啟動Redis
# usermod -aG redis git # service redis start
七、安裝GitLab
1.克隆GitLab
# su - git # cd /home/git # git clone https://gitlab.com/larryli/gitlab.git -b 7-8-zh gitlab
2.配置
切換至GitLab目錄下面,復制一份配置文件
# cd gitlab # cp config/gitlab.yml.example config/gitlab.yml
確保GitLab可以對 log 和 tmp 目錄有寫入權限
# chown -R git log/ # chown -R git tmp/ # chmod -R u+rwX log/ # chmod -R u+rwX tmp/
為satelites創建目錄
# mkdir /home/git/gitlab-satellites # chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites
確保GitLab可以對 tmp/pids、tmp/sockets、public/uploads目錄有寫入權限
# chmod -R u+rwX tmp/pids/ # chmod -R u+rwX tmp/sockets/ # chmod -R u+rwX public/uploads
從配置案例復制一份unicorn配置文件,並修改配置文件中的timeout選項的值為600(防止倉庫文件太大,拉取時間過長出現超時)
# su - # cd /home/git/gitlab # sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb # vim gitlab/config/unicorn.rb 修改timeout 600
從配置案例復制一份rack attack配置文件
# sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
為git用戶配置git全局配置
# sudo -u git -H git config --global user.name "GitLab" # sudo -u git -H git config --global user.email "xxxxxx@qq.com" # sudo -u git -H git config --global core.autocrlf input
配置redis連接
# sudo -u git -H cp config/resque.yml.example config/resque.yml
配置GitLab數據庫設置
# sudo -u git cp config/database.yml.mysql config/database.yml # vim config/database.yml
只需配置生產的部分(第一部分),用戶名和密碼
配置好以后運行該命令
# sudo -u git -H chmod o-rwx config/database.yml
配置GitLab綁定的域名或者IP
# vim config/gitlab.yml 修改host為gitlab的域名為你主機的IP地址(有兩項,test一項應該沒有影響,不過也修改吧) host:主機IP
安裝Gems
# sudo -u git -H bundle install --deployment --without development test postgres aws
可能會報錯,還是大陸訪問官網的問題,替換成https://ruby.taobao.org,然后再安裝
替換命令:
# sed -i '/^source/s/https\:\/\/rubygems\.org/https\:\/\/ruby\.taobao\.org/' Gemfile
安裝GitLab shell
# sudo -u git -H bundle exec rake gitlab:shell:install[v2.5.4] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
初始化數據庫
# sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
如果想要自己設置GitLab的root默認登陸密碼則運行以下命令:
# sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword
默認登陸賬號密碼
login.........root password......5iveL!fe
配置啟動腳本
# cp lib/support/init.d/gitlab /etc/init.d/
配置日志切割
# cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
運行命令檢測GitLab和它的環境是否正確
# sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
運行命令結果截圖如下:
拉取GitLab靜態資源文件
# sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
修改配置文件的gitlab_url為主機IP或者域名
# vim /home/git/gitlab-shell/config.yml
啟動GitLab實例
# service gitlab start
八、配置Nginx
安裝和配置開機啟動(這里用yum安裝,也可以自己編譯安裝)
# yum install -y nginx # chkconfig nginx on
下載Nginx配置文件,並命名為gitlab.conf
# wget -O /etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab
把nginx加入到git組
# usermod -a -G git nginx # chmod g+rx /home/git/
修改gitlab.conf文件
# vim /etc/nginx/conf.d/gitlab.conf server unix:/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket fail_timeout=0; 修改為 server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0; listen 0.0.0.0:80 default_server; 修改為 listen 80; listen [::]:80 default_server; 該行刪除 server_name YOUR_SERVER_FQDN; 修改為 server_name 主機IP
啟動Nginx
# service nginx start
九、訪問
訪問地址,http://主機IP或者域名,第一次登陸后就需要修改root用戶的密碼,然后再去掉GitLab的用戶注冊功能。到此GitLab搭建完畢!