设为首页 收藏本站
查看: 686|回复: 0

[经验分享] CentOS 自定义安装GitLab

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-4-7 09:53:19 | 显示全部楼层 |阅读模式
准备工作和说明
说明:
本文主要参考官方文档而来
gitlab 安装路径为/data/git

基于CentOS minimal 系统,系统安装时没有安装依赖包,可以安装开发包,也可依报错信息安装缺少依赖。
主要涉及以下组件安装与配置
  • 依赖包

  • Ruby
  • Go
  • System Users

  • Database
  • Redis
  • GitLab
  • Nginx


1. 依赖包安装

更新系统及软件包
1
2
3
#以 root 用户运行
yum update -y
yum upgrade -y



安装 vim wget

1
yum install vim wget -y



安装软件包
1
2
3
yum install -y zlib-devel libyaml-devel libffi-devel curl openssh-server libxml2-devel \
libxslt-devel libicu-devel logrotate python-docutils cmake openssh-clients openssl-devel \
libcurl-devel



安装开发工具
1
2
yum install gcc gcc-c++ wget
#或yum groupinstall 'Development Tools'



nodjs安装
包管理器安装,源码安装,二进制文件安装任选其一

包管理器

1
2
3
4
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
yum -y install nodejs
yum install gcc-c++ make
# or: yum groupinstall 'Development Tools'



二进制包
1
2
3
4
5
wget -c https://nodejs.org/dist/v4.4.1/node-v4.4.1-linux-x64.tar.gz
SHASUMS256:f0a53527f52dbcab3b98921a6cfe8613e5fe26fb796624988f6d615c30305a95  node-v4.4.1-linux-x64.tar.gz
mkdir /usr/local/node
tar zxvf node-v4.4.1-linux-x64.tar.xz -C /usr/local/node --strip-components 1
ln -s /usr/local/node/bin/{node,npm} /usr/bin/



源码安装
CentOS 6上需要 gcc-c++ 的版本不低于4.8,源码安装单独写篇文章

1
2
3
wget -c https://nodejs.org/dist/v4.4.1/node-v4.4.1.tar.gz
echo 'f3e604cc4d05a4810c37cd43a838a2dc4399d517bd1e8c53b7670dcffc4dc481  node-v4.4.1.tar.gz' \
| sha256sum -c - && tar zxf node-v4.4.1.tar.gz



Git软件
确认git版本2.7.4或更高,否则源码安装git软件
1
git --version




安装依赖
1
yum install perl-devel gettext libcurl-devel



下载并解压git软件

1
2
3
4
curl -O --progress https://www.kernel.org/pub/software/scm/git/git-2.8.0.tar.gz
echo '2c6eee5506237e0886df9973fd7938a1b2611ec93d07f64ed3447493ebac90d1  git-2.8.0.tar.gz' \
| sha256sum -c - && tar zxf git-2.8.0.tar.gz
cd git-2.8.0



配置git准备编译:
1
./configure



编译软件包:
1
make prefix=/usr/local all



安装软件包:
1
make prefix=/usr/local install



建立软链接
1
ln -sf /usr/local/bin/git /usr/bin



注:一定要装libcurl-devel 开发包
git是报 fatal: Unable to find remote helper for 'https'
就是没装libcurl-devel开发包的原因

安装邮件服务
1
yum install -y postfix




2. Ruby
注:目前支持的Ruby 版本是 2.1.x, 2.2和2.3暂不支持
下载Ruby 并校验文件:
1
2
3
4
5
wget -c https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.9.tar.gz
echo '034cb9c50676d2c09b3b6cf5c8003585acea05008d9a29fa737c54d52c1eb70c  ruby-2.1.9.tar.gz' \
| sha256sum -c - && tar zxf ruby-2.1.9.tar.gz

cd ruby-2.1.9



配置Ruby准备编译:
1
./configure --disable-install-rdoc



配置选项含义:
--disable-install-rdoc
禁用rdoc

编译软件包:
1
make



安装软件包:
1
make install



创建软链接:
1
ln -sv /usr/local/bin/{ruby,gem,bundler} /usr/bin



更换Ruby源为国内镜像源:
1
sudo -u git -H gem sources --add  --remove https://rubygems.org



安装bundler:
1
sudo gem install bundler --no-ri --no-rdoc



更改bundler 源:
1
sudo -u git -H bundle config mirror.https://rubygems.org https://ruby.taobao.org




3. Go
国内原因,只能使用国内的资源下载go了
由于国内网站没有提供sha256校验码,无从校验了

1
2
3
4
5
wget http://www.golangtc.com/static/go/1.6/go1.6.linux-amd64.tar.gz
tar -C /usr/local -zxf go1.6.linux-amd64.tar.gz
ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
ln -sv /usr/local/go/bin/go /usr/bin/
rm go1.6.linux-amd64.tar.gz




4. System Users
创建一个使用GitLab的git用户:
1
useradd -c 'GitLab' git




5. Database
本文使用MySQL数据库,版本不能高于5.5.14
1
yum install -y mysql mysql-server mysql-devel



确认MySQL版本:
1
mysql --version



设置MySQL 安全:
1
mysql_secure_installation



会要求设置mysql root口令,删除空密码用户,删除测试数据库

创建GitLab 仓库的用户,并设置密码(替换$password变量):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';

#设置数据库存储类型为INNODB
mysql> SET storage_engine=INNODB;

#创建GitLab 数据库 gitlabhq
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq` DEFAULT CHARACTER SET \
`utf8` COLLATE `utf8_unicode_ci`;

#赋予GitLab 用户对gitlabhq的操作权限
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES,  \
DROP, INDEX, ALTER, LOCK TABLES ON `gitlabhq`.* TO 'git'@'localhost';

#退出数据库管理
mysql> \q

#验证用户登录
sudo -u git -H mysql -u git -p -D gitlabhq
# 登录成功会显示 'mysql>' 提示符号
#退出数据库
mysql> \q



设置开机启动
1
2
chkconfig --add mysqld
chkconfig --level 2345 mysqld




6. Redis
GitLab 使用的Redis 不能低于 2.8版本

下载Redis,并解压文件:
1
2
wget -c  http://download.redis.io/releases/redis-3.0.7.tar.gz| tar -C /tmp -xzf redis-3.0.7.tar.gz
cd /tmp/redis-3.0.7



安装软件:
1
make install



创建软链接:
1
2
ln -sv /usr/local/bin/redis-cli /usr/bin/
ln -sv /usr/local/bin/redis-server /usr/bin/redis




配置redis 使用sockets:
如已有redis服务,可更改以下redis文件名增加一个redis-gitlab服务(既将redis.conf,redis替换为redis-gitlab.conf,redis-gitlab)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#创建redis配置文件目录,并拷贝配置文件
mkdir /etc/redis && cp redis.conf /etc/redis/redis.conf

#禁用redis 监听TCP端口
sed -i 's/port .*/port 0/' /etc/redis/redis.conf

#后台方式运行
sed -i 's/^daemonize .*/daemonize yes/' /etc/redis/redis.conf
#配置redis 日志
sed -i 's/^logfile.*/logfile "\/data\/redis\/redis.log"/' /etc/redis/redis.conf
#配置redis存储路径
sed -i 's/^dir.*/dir \/data\/redis/' /etc/redis/redis.conf
#创建redis目录
mkdir /data/redis
chown redis.redis /data/redis

#启用redis socket
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf

#赋予socket redis组的所有权限
echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf

#创建socket目录,添加redis用户
mkdir /var/run/redis
useradd -M redis
chown redis:redis /var/run/redis
chmod 755 /var/run/redis

#添加git到redis组
usermod -aG redis git

#创建启动脚本
vim /etc/init.d/redis



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
#
# redis        init file for starting up the redis daemon
#
# chkconfig:   - 20 80
# description: Starts and stops the redis daemon.

# Source function library.
. /etc/rc.d/init.d/functions

name="redis-server"
exec="/usr/local/bin/$name"
pidfile="/var/run/redis.pid"
REDIS_CONFIG="/etc/redis/redis.conf"

[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/lock/subsys/redis

start() {
    [ -f $REDIS_CONFIG ] || exit 6
    [ -x $exec ] || exit 5
    echo -n $"Starting $name: "
    daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $name: "
    killproc -p $pidfile $name
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    false
}

rh_status() {
    status -p $pidfile $name
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
        exit 2
esac
exit $?



设置开机启动
1
2
chkconfig --add redis
chkconfig --level 2345 redis




7.GitLab
安装GitLab 到自定义目录,这里以data 目录为例:
1
2
mkdir -vp /data/git && cd /data/git
sudo chown git:git /data/git/




克隆源:

1
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-6-stable gitlab



配置GitLab:
1
2
3
cd /data/git/gitlab
#复制配置示例文件
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml



修改配置文件host,email_from,GitLab 主目录参数为实际值
注:本例全文采用原始文件内容做展示,实际/home/git 应为/data/git
1
sudo -u git -H vim config/gitlab.yml



1
2
3
4
5
6
7
8
9
10
11
12
32>     host: localhost
33>    port: 80 #如使用HTTPS,则将此端口修改为443
34>    https: false #启用或禁用HTTPS,默认false禁用,启用则改为true
61>     email_from: example@example.com

satellites:
386>     path: /home/git/gitlab-satellites/

gitlab_shell:
411>     path: /home/git/gitlab-shell/
414>     repos_path: /home/git/repositories/
415>     hooks_path: /home/git/gitlab-shell/hooks/




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#复制secrets示例文件
sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml

#确认GitLab 对log  、tmp 目录可写
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/

#确认GitLab 对 tmp/pids/ 和tmp/sockets/ 目录可写
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

#创建插件上传目录
sudo -u git -H mkdir public/uploads/

#确认Gitlab 用户只能访问public/uploads/ 目录
#上传文件由gitlab-workhorse 服务
sudo chmod 0700 public/uploads

#修改CI 构建目录权限
sudo chmod -R u+rwX builds/
sudo chmod -R u+rwX shared/artifacts/



拷贝Unicorn配置示例,并修改配置文件

1
2
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
sudo -u git -H vim config/unicorn.rb



1
2
3
4
5
36> working_directory "/home/git/gitlab" # available in 0.94.0+
41> listen "/home/git/gitlab/tmp/sockets/gitlab.socket", :backlog => 1024
62> pid "/home/git/gitlab/tmp/pids/unicorn.pid"
67> stderr_path "/home/git/gitlab/log/unicorn.stderr.log"
68> stdout_path "/home/git/gitlab/log/unicorn.stdout.log"



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#确认内核数
nproc

#如果是高负载,可以开启cluster 集群模式
#设置工作线程数小于内核数
#示例的工作线程数为3 运行在2GB内存的服务上
sudo -u git -H vim config/unicorn.rb


# 复制rack_attack配置示例
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

# 配置gitlab 全局用户设置
sudo -u git -H git config --global core.autocrlf input

# redis 配置文件
sudo -u git -H cp config/resque.yml.example config/resque.yml

# 修改redis 服务路径为实际值
sudo -u git -H editor config/resque.yml



1
2
3
development: redis://localhost:6379
test: redis://localhost:6379
production: unix:/var/run/redis/redis.sock




GitLab 数据库配置文件:
1
2
3
4
5
6
sudo -u git cp config/database.yml.mysql config/database.yml
#更新配置文件的'database','username','password','host','socket'为实际数值
#生产环境只需配置第一段
vim config/database.yml
#设置文件为git 用户只读权限
sudo -u git -H chmod o-rwx config/database.yml




安装Gems:
1
sudo -u git -H bundle install --deployment --without development test postgres aws kerberos




安装 GitLab Shell :

GitLab Shell 是专门用于gitlab的 SSH 和存储管理软件
1
2
3
4
5
6
#运行安装gitllab-shell 任务,使用你的redis sockte链接
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock \
RAILS_ENV=production

#默认gitlba-shell 配置信息是从GitLab 主配置文件读取生产,并修改为安装路径
sudo -u git -H vim /home/git/gitlab-shell/config.yml



1
2
3
4
5
6
7
8
9
10
11
12
13
user: git
gitlab_url:   #对应gitlab.yml 中的'host:' 值若,启用了HTTPS协议,则此url应使用HTTPS协议而非HTTP  
http_settings:
  self_signed_cert: false #若启用HTTPS协议,此处应开启证书验证,既此值为true
  #否则报Check GitLab API access: FAILED: Failed to connect to internal API 错误
repos_path: "/home/git/repositories/" #仓库文件目录
auth_file: "/home/git/.ssh/authorized_keys"
redis:
  bin: '' #redis 执行文件,此例应为/usr/local/bin/redis-cli
  namespace: resque:gitlab
  socket: "/var/run/redis/redis.sock" #redis socket路径
log_level: INFO
audit_usernames: false




安装gitlab-workhorse:
1
2
3
4
5
cd /data/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
sudo -u git -H git checkout v0.7.1
sudo -u git -H make




初始化数据库并激活高级功能:
1
2
3
4
5
cd /data/git/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production --trace
#--trace 可跟踪初始化过程,便于查看错误
#'yes' 创建数据库表。
#创建完会看到登录帐号,并说明首次登录时创建密码




下面为初始化时指定root 口令和email
1
2
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production \
GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail




secrets.yml:
此文件存储着对话加密密钥和安全设置,将此文件备份到安全地方,备份位置要以防泄漏。

安装启动脚本并修改参数为安装路径:
注:此处只需修改/etc/default/gitlab 文件即可,不需要修改启动脚本

1
2
3
4
5
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab

sudo vim /etc/default/gitlab
14> app_root="/home/$app_user/gitlab"




GitLab开机启动:
1
2
sudo chkconfig --add gitlab
sudo chkconfig --level 2345 gitlab on




安装 Logrotate,并修改为安装路径:
1
2
3
4
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
sudo vim /etc/logrotate.d/gitlab
4> /home/git/gitlab/log/*.log {
13> /home/git/gitlab-shell/gitlab-shell.log {




检查应用状态:
检查GitLab 安装配置是否正确
1
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production



编制资产:
1
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production




启动GitLab 服务:
1
sudo service gitlab start




8.Nginx
由于centos默认没有nginx源,可采用源码编译安装,亦可添加yum源
此处采用添加nginx yum源(可参考nginx官方设置)
1
2
3
4
5
6
7
#echo "[nginx]
>name=nginx repo
>baseurl=http://nginx.org/packages/centos/6/$basearch/
>gpgcheck=0
>enabled=1" > /etc/yum.repos.d/nginx.repo

#yum -y install nginx



baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/这里使用centos替换里OS,6替换了OSRELEASE
站点配置:
复制站点配置示例文件
1
2
3
sudo cp lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf
#若使用HTTPS协议,则复制gitlab-ssl,配置对应内容及证书即可
sudo cp lib/support/nginx/gitlab-ssl /etc/nginx/conf.d/gitlab-ssl.conf



创建SSL 证书:
由于此证书没有通过验证,所以在git客户端需要关闭证书验证功能
1
git config --global http.sslverify false



1
2
3
4
mkdir -p /etc/nginx/ssl/
cd /etc/nginx/ssl/
sudo openssl req -newkey rsa:2048 -x509 -nodes -days 3560 -out gitlab.crt -keyout gitlab.key
sudo chmod o-r gitlab.key




编辑配置文件,确认设置正确
1
vim /etc/nginx/conf.d/gitlab.conf



1
2
3
4
5
6
7
8
9
10
upstream gitlab-workhorse {
  server unix:/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket fail_timeout=0;
}

server_name YOUR_SERVER_FQDN; #修改YOUR_SERVER_FQDN为实际域名

  location ~ ^/(404|422|500|502)\.html$ {
    root /home/git/gitlab/public;
    internal;
  }




测试配置
1
nginx -t



重启nginx服务
1
service nginx restart




确认应用状态

1
sudo -u git -H bundle exec rake gitlab:check RATLS_ENV=production




检测应用的过程中,有几处报错,其中有一处是目录权限问题,需要按照给出的提示进行修改
1
2
3
  sudo chmod -R ug+rwX,o-rwx /home/git/repositories/
  sudo chmod -R ug-s /home/git/repositories/
  sudo find /home/git/repositories/ -type d -print0 | sudo xargs -0 chmod g+s




自定义SSH 连接
如果SSH 服务没有运行在默认端口,则必须修改GitLab SSH配置,参照如下格式添加配置
1
2
3
4
5
# Add to /home/git/.ssh/config
host localhost          # Give your setup a name (here: override localhost)
    user git            # Your remote git user
    port 2222           # Your port number
    hostname 127.0.0.1; # Your server name or IP






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-200804-1-1.html 上篇帖子: Linux&OSX下 Git打包变更的文件 下篇帖子: Ubuntu & GitHub
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表