gitlab版本控制系统源码部署

本文章适用于需要自建gitlab代码管理系统,仅供参考

1. 环境准备

安装软件包及版本要求

1. Ubuntu/Debian/CentOS/RHEL 
2. ruby 2.4+
3. git 2.7.2+
4. go 1.0.0+
5. redis 3.2+
6. node 8.0+
7. MySQL(5.7+) or PostgreSQ (9.4+)
8. gitlab ce 11-3-stable
9. nignx (1.12.2)

2. 安装所需要软件依赖包

添加EPEL源

# wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7
# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

安装基础软件以及相关依赖包

# rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm
# yum groupinstall "Development tools"
# yum install autoconf automake bison expat-devel gcc-c++ zlib-devel mysql-devel \
 gdbm-devel re2-devel readline-devel ncurses-devel curl-devel libxml2-devel \
 libxslt-devel openssl-devel libicu-devel rsync python-docutils cmake  -y

3. 安装git (版本为2.18.x)

卸载原有的git包

# sudo yum remove git

安装git所需要的依赖包

# sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel

下载稳定版本的git源码包并安装

# cd /root 
# curl --remote-name --location --progress https://www.kernel.org/pub/software/scm/git/git-2.18.0.tar.gz

解压并安装git

# cd /root
# tar xvf git-2.18.0.tar.gz -C /usr/local/src  &&  cd /usr/local/src/git-2.18.0/
# ./configure prefix=/usr/local/git all
# make && make install

设置git命令路径优化

# vim /etc/profile.d/git.sh 
内容如下:
export GIT_HOME=/usr/local/git
export PATH=$GIT_HOME/bin:$PATH

# source  /etc/profile.d/git.sh

4. 安装ruby (版本为2.4.x)

下载最新稳定版的ruby二进制源码包

# cd /root 
# curl --remote-name --progress https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.4.tar.gz

编译安装ruby

# cd /root && tar zxvf ruby-2.4.4.tar.gz -C /usr/local/src
# cd /usr/local/src/ruby-2.4.4/
# ./configure  --prefix=/data/apps/ruby --disable-install-rdoc
# make && make install

设置ruby命令路径优化

# sudo vim /etc/profile.d/ruby.sh
内容如下:
export RUBY_HOME=/data/apps/ruby
export PATH=$RUBY_HOME/bin:$PATH

# source  /etc/profile.d/ruby.sh

安装bundle命令

# gem install bundler --no-ri --no-rdoc
# ln -sf  /data/apps/ruby/bin/* /usr/local/bin/

5. 安装go (版本为1.10.x)

卸载原有的go命令包

# sudo rm -rf /usr/local/go

下载稳定版的go二进制源码包

# cd /root
# curl --remote-name --progress https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
# sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz
# sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
# rm go1.10.3.linux-amd64.tar.gz

6. 安装node (版本为8.0.x)

安装node.js应用 (这里版本为8.0以上)

# cd /root
# curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
# sudo yum install -y nodejs

安装完node后安装yarn
# cd /root
# sudo yum install gcc-c++ make -y
# curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
# sudo yum install yarn -y

7. 安装redis (版本为3.2.x)

yum安装redis服务

# yum install redis -y

配置redis配置文件

# cat > /etc/redis.conf <

对redis相关目录授权

# mkdir /var/run/redis
# mkdir /data/apps/redis/db -p
# chown -R redis:redis /data/apps/redis
# chown  redis:redis /var/run/redis
# chmod 755 /var/run/redis

8. 安装数据库 (版本为5.7.x)

这里使用的数据库为MySQL 5.7.0以上版本,数据库安装详情请参考https://docs.gitlab.com/ee/install/database_mysql.html

9. 安装nignx (版本为1.12.x)

有关nginx安装这里为yum安装,当然也可以根据自己的选择源码安装

# yum install nignx -y

10. 部署gitlab应用 (版本为11-3 stable版)

10.1 首先创建运行gitlab服务的git用户

# groupadd git
# useradd -g git -c "Gitlab Service" -d /data/apps/git git
# ln -sf /data/apps/git/ /home/git
# sudo usermod -aG git nignx
# sudo usermod -aG redis git

10.2 下载gitlab源码(这里版本为11-3-stable)

# cd /home/git
# sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 11-3-stable gitlab

10.3 修改gitlab相关配置文件并做相应的修改

拷贝gitlab模板并修改相关配置

# cd /home/git/gitlab
# sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
# sudo -u git -H vim config/gitlab.yml

找到如下行内容
## GitLab settings
gitlab:
  host: code.mo9.com  修改为你需要访问的域名
  port: 80 # Set to 443 如果需要https访问,开启HTTPS即可;

 ## Git settings
  git:
    bin_path: /usr/local/git/bin/git  修改为git的命令执行路径

 time_zone: 'UTC'   修改为“Asia/Shanghai”

拷贝unicorn模板并编辑配置,修改如下

# sudo -u git -H vim config/unicorn.rb

找到如下内容
worker_processes 3  (值修改为服务器的cpu数+1)
listen "127.0.0.1:8080", :tcp_nopush => true (该值为主机的ip)

拷贝database模板并编辑配置

# sudo -u git cp config/database.yml.mysql config/database.yml
# sudo -u git -H chmod o-rwx config/database.yml
# sudo -u git -H vim config/database.yml

修改内容如下(此处仅为模板)
#
# PRODUCTION
#
production:
  adapter: mysql2
  encoding: utf8
  collation: utf8_general_ci
  reconnect: false
  database: gitlab
  pool: 10
  username: root
  password: "1qaz@WSX"
  host: rm-bp1lsjz89883fih35.mysql.rds.aliyuncs.com

拷贝resque模板并编辑配置

# sudo -u git -H cp config/resque.yml.example config/resque.yml
# sudo -u git -H vim config/resque.yml

修改内容如下(此处仅为参考模板,如果需要配置redis集群请详见官网配置说明)
# If you change this file in a Merge Request, please also create
# a Merge Request on https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests
#
production:
  # Redis (single instance)
  url: unix:/var/run/redis/redis.sock
  ##

拷贝secrets文件模板并授权(注意此文件内容自动生成, 在迁移gitlab需要用到该文件)

# sudo -u git -H cp config/secrets.yml.example config/secrets.yml
# sudo -u git -H chmod 0600 config/secrets.yml

对gitlab相关目录授权

# sudo chown -R git log/
# sudo chown -R git tmp/
# sudo chmod -R u+rwX,go-w log/
# sudo chmod -R u+rwX tmp/
# sudo chmod -R u+rwX tmp/pids/
# sudo chmod -R u+rwX tmp/sockets/
# sudo -u git -H mkdir public/uploads/
# sudo chmod 0700 public/uploads
# sudo chmod -R u+rwX builds/
# sudo chmod -R u+rwX shared/artifacts/
# sudo chmod -R ug+rwX shared/pages/

拷贝rack_attack.rb文件模板

# sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

设置git初始参数

# sudo -u git -H git config --global core.autocrlf input
# sudo -u git -H git config --global gc.auto 0
# sudo -u git -H git config --global repack.writeBitmaps true
# sudo -u git -H git config --global receive.advertisePushOptions true

10.4 安装gitlab所需要的gems包
自bundler1.5.2起,你可以使用bundle install -jN(N就是cpu核心数)安装Gems,速度比之前要快大约60%.详细的内容可以查看官文.不过首先要确保你的bundler版本>=1.5.2(运行bundle -v查看)

由于默认的ruby源地址访问会存在网络访问慢的问题,所以需要更换为国内的ruby源

# cd /home/git/gitlab
# gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
# gem sources -l
    https://gems.ruby-china.com  (确保只有 https://gems.ruby-china.com/)

使用git用户修改Gemfile 和 Gemfile.lock
更改 https://rubygems.org/ 为: https://gems.ruby-china.com/

安装gems包

For PostgreSQL (note, the option says "without ... mysql")
# sudo -u git -H bundle install --deployment --without development test mysql aws kerberos

Or if you use MySQL (note, the option says "without ... postgres")
# sudo -u git -H bundle install --deployment --without development test postgres aws kerberos

上述命令执行完后,结果如下:
Bundle complete! 226 Gemfile dependencies, 321 gems now installed.
Gems in the groups development, test, postgres, aws and kerberos were not installed.
Bundled gems are installed into `./vendor/bundle`

注意:在安装gems过程中,可能会遇到以下错误:

如果提示下面的错误:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
error occurred while installing charlock_holmes (0.7.3), and Bundler cannot continue.
Make sure that `gem install charlock_holmes -v ‘0.7.3’` succeeds before bundling.
brew install icu4c or apt-get install libicu-dev
解决办法:
# yum install libicu.x86_64  libicu-devel.x86_64

An error occurred while installing rugged (0.25.1.1), and Bundler cannot continue.
Make sure that `gem install rugged -v '0.25.1.1'` succeeds before bundling.
解决办法:
# yum install cmake
# gem install rugged -v '0.25.1.1'

ERROR:  Could not find a valid gem 'charlock_holmes' (= 0.6.9.4), here is why:
          Unable to download data from https://rubygems.org/ - Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org/quick/Marshal.4.8/charlock_holmes-0.6.9.4.gemspec.rz)
ERROR:  Possible alternatives: charlock_holmes
解决办法:
#yum install libicu-devel

An error occurred while installing mysql2 (0.4.20), and Bundler cannot continue.
Make sure that `gem install mysql2 -v ‘0.4.20’` succeeds before bundling.
#解决办法
# yum install mysql-devel.x86_64
# gem install mysql2 -v '0.4.20

An error occurred while installing re2 (1.0.0), and Bundler cannot continue.
Make sure that `gem install re2 -v '1.0.0'` succeeds before bundling.
#解决办法
# yum install -y re2-devel

An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v ‘0.18.4’` succeeds before bundling.
#解决办法
# gem install pg -v '0.18.4'
# yum install postgresql-devel.x86_64

An error occurred while installing sqlite3 (1.5.14), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v ‘1.5.14’` succeeds before bundling.
# 解决办法
# yum install sqlite-devel.x86_64
# gem install sqlite3 -v '1.5.14'

Bundler::GemRequireError: There was an error while trying to load the gem ‘coffee-rails’.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
#解决办法:因为execjs需要javascript的支持
#参考这里:NodeJs的安装

10.5 安装gitlab-shell

在实际安装gitlab-shell过程中会存在克隆gitlab-shell仓库网络慢问题,所以我们必须修改gitlab-shell仓库源为国内源:https://git.oschina.net/qiai365/gitlab-shell.git 操作如下:

# cd /home/git/gitlab
# sudo -u git -H vim  /home/git/gitlab/lib/tasks/gitlab/shell.rake
替换掉gitlab-shell官方仓库源即可

10.7 执行安装gitlab-shell脚本

# cd /home/git/gitlab
# sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production SKIP_STORAGE_VALIDATION=true

编辑gitlab-shell配置文件,并根据实际情况修改

# sudo -u git -H vim /home/git/gitlab-shell/config.yml

内容如下:
---
user: git
gitlab_url: http://code.mo9.com/   (修改为gitlab对外访问的域名FQDN)
http_settings:
  self_signed_cert: false (如果需要开启https访问,修改为ture,并使用ca_file指定)  
auth_file: "/data/apps/git/.ssh/authorized_keys"
redis:
  bin: "/usr/bin/redis-cli"   (该值修改为redis命令路径)
  namespace: resque:gitlab
  socket: "/var/run/redis/redis.sock" (该值修改为redis的sock路径)
log_level: INFO
audit_usernames: false

10.6 安装gitlab-workhorse

# cd /home/git/gitlab
# sudo -u git -H bundle exec rake "gitlab:workhorse:install[/home/git/gitlab-workhorse]" RAILS_ENV=production

10.7 安装gitlab-pages

# cd /home/git
# sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-pages.git
# cd gitlab-pages
# sudo -u git -H git checkout v$(

10.8 安装gitaly

# cd /home/git/gitlab
# sudo -u git -H bundle exec rake "gitlab:gitaly:install[/home/git/gitaly,/home/git/git-data]" RAILS_ENV=production
# sudo chmod 0700 /home/git/gitlab/tmp/sockets/private
# sudo chown git /home/git/gitlab/tmp/sockets/private

10.9 初始化数据库

# cd /home/git/gitlab
# sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=gitlab.com [email protected]

当看到以下内容,表示已经安装完成
Administrator account created:
login:    root
password: your_passwd
== Seed from /home/git/gitlab/db/fixtures/production/010_settings.rb

10.10 打包前端静态资源文件
编译gettext文件

# cd /home/git/gitlab
# sudo -u git -H bundle exec rake gettext:compile RAILS_ENV=production

生成gitlab前端静态assets文件

# cd /home/git/gitlab
# sudo -u git -H yarn install --production --pure-lockfile
# sudo -u git -H bundle exec rake gitlab:assets:compile RAILS_ENV=production NODE_ENV=production

10.11 初始化gilab服务脚本以及日志切割脚本

# cd /home/git/gitlab
# sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
# sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
# sudo service gitlab start

11. 配置nginx代理

# cd /home/git/gitlab
# sudo cp lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf
# nginx -t
# service nginx start

如果需要开启https访问,需要更改三个地方的配置文件:

/home/git/gitlab/config/gitlab.yml  开启https,并修改访问域名以及访问端口;
/home/git/gitlab-shell//config.yml  开启https,并修改访问域名添加ssl证书;
/etc/nginx/conf.d/gitlab.conf        开启https,并修改访问域名添加ssl证书;

12. 检查gitlab应用信息以及状态

检查gitlab组件信息

# sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production

当出现如下信息表示安装无误:
System information
System:     CentOS 7.4.1708
Current User:   git
Using RVM:  no
Ruby Version:   2.4.4p296
Gem Version:    2.6.14.1
Bundler Version:1.16.6
Rake Version:   12.3.1
Redis Version:  3.2.12
Git Version:    2.18.0
Sidekiq Version:5.2.1
Go Version: go1.10.3 linux/amd64

GitLab information
Version:    11.4.0-pre
Revision:   0d84dd22872
Directory:  /data/apps/git/gitlab
DB Adapter: mysql2
URL:        http://code.mo9.com
HTTP Clone URL: http://code.mo9.com/some-group/some-project.git
SSH Clone URL:  [email protected]:some-group/some-project.git
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:

GitLab Shell
Version:    8.3.3
Repository storage paths:
- default:  /home/git/repositories
Hooks:      /home/git/gitlab-shell/hooks
Git:        /usr/local/git/bin/git

检查gitlab应用状态

# sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

当出现如下信息时,说明整个gitlab部署成功:
Checking GitLab Shell ...

GitLab Shell version >= 8.3.3 ? ... OK (8.3.3)
Repo base directory exists?
default... yes
Repo storage directories are symlinks?
default... no
Repo paths owned by git:root, or git:git?
default... yes
Repo paths access is drwxrws---?
default... yes
hooks directories in repos are links: ...
Administrator / test ... ok
Running /home/git/gitlab-shell/bin/check
Check GitLab API access: OK
Redis available via internal API: OK

Access to /data/apps/git/.ssh/authorized_keys: OK
gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes
Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Reply by email is disabled in config/gitlab.yml
Checking LDAP ...

LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab ...

Git configured correctly? ... yes
Database config exists? ... yes
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config up to date? ... yes
Log directory writable? ... yes
Tmp directory writable? ... yes
Uploads directory exists? ... yes
Uploads directory has correct permissions? ... yes
Uploads directory tmp has correct permissions? ... skipped (no tmp uploads folder yet)
Init script exists? ... yes
Init script up-to-date? ... yes
Projects have namespace: ...
Administrator / test ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.3.5 ? ... yes (2.4.4)
Git version >= 2.9.5 ? ... yes (2.18.0)
Git user has default SSH configuration? ... yes
Active users: ... 2

Checking GitLab ... Finished

至此整个gitlab系统搭建完成,当然如果需要基于https访问可以参考官方文档:

转载于:https://blog.51cto.com/blief/2296409

你可能感兴趣的:(gitlab版本控制系统源码部署)