Litao OK Blog

A little blog for my life.

在CentOS 6.8 X86 64bit上安装GitLab Community Edition 8.12.3

参考资料: https://bbs.gitlab.cc/topic/35/gitlab-ce-8-7-%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85%E6%89%8B%E5%86%8C-centos6-rehl6

一、关闭SELinux

1. 修改/etc/selinux/config 文件

将SELINUX=enforcing改为SELINUX=disabled, 然后重启电脑

2. 查看selinux状态

1
sestatus -v

关闭后应该显示:

1
SELinux status:                 disabled

二、安装相关程序

1. 添加EPEL源

1) 下载EPEL的GPG KEY,导入到系统中

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

2) 安装epel-release-latest-6.noarch.rpm包,启用EPEL

1
rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm

2. 安装开发工具库

1
yum install automake bison cmake cpio curl curl-devel expat-devel gcc gcc-c++ gettext-devel perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker libcurl-devel libffi-devel libicu-devel libtool libxml2-devel libxslt-devel libyaml-devel make mlocate nodejs  openssl-devel patch readline-devel ruby-devel unzip vim vixie-cron wget zlib-devel

3. 安装Git

  1. 检查git版本,需要高于2.7版本
1
git version
  1. 下载安装git
1
2
3
4
5
6
7
8
9
10
mkdir -p src/tar
cd src/tar/
wget https://github.com/git/git/archive/v2.10.0.tar.gz
cd ..
tar -zxf tar/v2.10.0.tar.gz
cd git-2.10.0/
make prefix=/usr/local all
make prefix=/usr/local install
# 查看版本号
git version

4. 添加用户

1
adduser -c 'Gitlab' -s /bin/bash git

5. 安装Ruby环境

  1. 如果系统有老版本ruby,先删除之
1
yum remove ruby
  1. 通过rvm安装
1
2
3
4
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm install 2.3.1
  1. 修改gem源为国内
1
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
  1. 安装bundle工具
1
2
3
gem install bundler --no-ri --no-rdoc
su - git
bundle config mirror.https://rubygems.org https://gems.ruby-china.org/

6. 安装Go

1
2
3
4
5
cd ~/src/tar/
wget https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz
cd ..
tar -zxf tar/go1.7.1.linux-amd64.tar.gz
mv go /usr/local/

修改环境变量 /etc/profile,在export PATH前面添加

1
2
3
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
PATH=$PATH:$GOBIN:$JAVA_HOME/bin

7. 安装数据库

虽然官方建议使用postgresql,但是我对其并不熟悉,所以还是继续选用MySQL,此处使用mysql-community 5.7.15

先去http://dev.mysql.com/downloads/repo/yum/ 下载源文件,选择mysql57-community-release-el6-9.noarch.rpm,下载完后安装

1
2
3
yum localinstallmysql57-community-release-el6-9.noarch.rpm
yum installmysql-community-servermysql-community-develmysql-community-commonmysql-community-libsmysql-community-client
mysql_secure_installation

由于新版本对密码复杂度和长度有要求,12个字符以上,包含中英文大小写、数字、符号等,设置完登录开始配置数据库:

1
2
3
4
5
6
7
mysql -u root -p
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER, LOCK TABLES, REFERENCES ON `gitlabhq_production`.* TO 'git'@'localhost';
mysql> FLUSH PRIVELEGES;
mysql> \q
 

8.安装Redis

添加用户和组:

1
groupadd redis && useradd -g redis redis -s /sbin/nologin

下载安装:

1
2
3
4
5
6
7
8
9
cd ~/src/tar/
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
cd ..
tar -zxf tar/redis-3.2.4.tar.gz
cd redis-3.2.4/
make
make install
mkdir -p /etc/redis
cp redis.conf /etc/redis

修改配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
# 把'post'设置为0以禁止监听TCP端口
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf
# 让redis以socket方式启动
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf
# 启动守护进程
sed -i 's/daemonize no/daemonize yes/g' /etc/redis/redis.conf
# 创建存放socket的目录
mkdir /var/run/redis
sudo chown redis:redis /var/run/redis
sudo chmod 755 /var/run/redis
# Persist the directory which contains the socket, if applicable
if [ -d /etc/tmpfiles.d ]; then
  echo 'd  /var/run/redis  0755  redis  redis  10d  -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi
# 把git用户加入redis组
sudo usermod -aG redis git
# 下载redis init 脚本
$ curl -L http://packages.gitlab.cc/install/init-script/redis/cenots6/redis-server -o /etc/init.d/redis-server
$ chmod +x /etc/init.d/redis-server

启动redis

1
2
3
4
# 启动redis服务
$ service redis-server start
# 将redis加入自启动
$ chkconfig redis-server on

三、安装Gitlabe-CE

1. 用git克隆代码

1
2
3
su - git
cd /home/git/
git clone https://github.com/gitlabhq/gitlabhq.git -b 8-12-stable gitlab

2. 配置Gitlab-CE

1
2
3
4
5
# 进入gitlab目录
cd /home/git/gitlab

# 复制gitlab.yml(Gitlab的主配置文件)
cp config/gitlab.yml.example config/gitlab.yml

3. 修改gitlab.yml

1
vim config/gitlab.yml
  • 修改第32行 host: localhost为 host: 你的域名或者ip
  • 修改第479行 bin_path: /usr/bin/git 为 bin_path: /usr/local/bin/git

4. 复制文件,创建文件夹,修改权限

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
cp config/secrets.yml.example config/secrets.yml
chmod 0600 config/secrets.yml
chown -R git log/
chown -R git tmp/
chmod -R u+rwX,go-w log/
chmod -R u+rwX tmp/
chmod -R u+rwX tmp/pids/
chmod -R u+rwX tmp/sockets/

mkdir public/uploads/
chmod 0700 public/uploads

chmod -R u+rwX builds/

chmod -R u+rwX shared/artifacts/

# 复制 Unicorn 配置文件
cp config/unicorn.rb.example config/unicorn.rb

# 查询CPU核心数
nproc

# 如果你想搭建一个高负载的Gitlab实例,可启用集群模式.
# 修改'worker_processes'参数,至少要跟cpu核心数一样.
# 举例:2G RAM的服务器修改workers数量为3
# vim config/unicorn.rb

# 复制Rack attack 配置文件
cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

# Configure Git global settings for git user
# 'autocrlf' is needed for the web editor
git config --global core.autocrlf input

# Disable 'git gc --auto' because GitLab already runs 'git gc' when needed
git config --global gc.auto 0

# 复制 Redis 连接配置文件
cp config/resque.yml.example config/resque.yml

# 如果之前修改过redis socket的路径,在config/resque.yml文件里面修改为当前的路径.

检查unicorn启动端口,默认8080,如果已经被别的服务占用了,必须要修改config/unicorn.rb文件

5. 修改GitLab DB 设置

1
cp config/database.yml.mysql config/database.yml

修改文件config/database.yml中的用户名和密码

修改权限:

1
chmod o-rwx config/database.yml

6. 安装gems包

1
2
3
# su - git 使用git用户
cd /home/git/gitlab
bundle install --deployment --without development test postgres aws kerberos --withmysql

7. 安装Gitlab-shell

1
2
3
# su - git
cd /home/git/gitlab
bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production

8. 安装gitlab-workhorse

1
2
3
4
5
cd /home/git/
git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
git checkout v0.7.1
make

9. 初始化数据库,激活高级特性

1
2
3
4
5
6
7
# su - git
cd /home/git/gitlab
bundle exec rake gitlab:setup RAILS_ENV=production

# 输入 'yes' 以创建数据库表

# 当看到 'Administrator account created:' 表示已经安装完成

10. 安全设置 secrets.yml

secrets.yml 文件为每个会话和安全变量存储密钥.把这个文件备份到别的地方,但是不要和数据库备份放在一块,否则你的数据库备份损坏会导致这个文件丢失.

11. 安装Gitlab启动脚本

1
2
3
4
5
6
7
8
9
10
# su - root
sudo cp /home/git/gitlab/lib/support/init.d/gitlab /etc/init.d/gitlab

sudo cp /home/git/gitlab/lib/support/init.d/gitlab.default.example /etc/default/gitlab

# 设置自动启动
chkconfig gitlab on

# 安装logrotate
sudo cp /home/git/gitlab/lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

12. 检查环境配置

1
2
3
su - git
cd gitlab/
bundle exec rake gitlab:env:info RAILS_ENV=production

13. 生成GitLab前端资源

1
2
3
# su - git
cd /home/git/gitlab
bundle exec rake assets:precompile RAILS_ENV=production

14. 启动GitLab

1
2
3
sudo service gitlab start
# 或者
sudo /etc/init.d/gitlab start

15. 再检查一次Gitlab的所有组件

1
2
3
su - git
cd /home/git/gitlab
bundle exec rake gitlab:check RAILS_ENV=production

四、设置nginx代理

1.yum安装nginx

1
2
# 创建/etc/yum.repos.d/nginx.repo
touch /etc/yum.repos.d/nginx.repo

文件内容:

1
2
3
4
5
6
7
# 编辑nginx.repo,把下面的内容写入;
# 注意修改OS和OSRELEASE,如centos6则把OS改成centos, OSRELEASE改成6;
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

安装:

1
2
yum update
yum install -y nginx

2. 设置代理

1
2
# 复制gitlab的nginx配置文件到nginx的conf.d目录
sudo cp /home/git/gitlab/lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf

修改文件 /etc/nginx/conf.d/gitlab,将YOUR_SERVER_FQDN修改为你的域名

修改/home/git/目录的权限

1
sudo chmod 755 /home/git/

修改vim /home/git/gitlab/config/gitlab.yml中host,改为相同域名

3. 启动nginx

1
service nginx restart