# 概述
Gitlab的安装过程主要包括以下组件的配置:

* 安装软件包及解决依赖项
* Ruby环境
* Go
* 系统用户
* 数据库(Mysql/Postgresql)
* Redis
* Gitlab-CE
* Nginx
* 非必要条件:[点击链接加入群【GitLab交流群】](http://jq.qq.com/?_wv=1027&k=XaOY5J)

## 1.安装软件包及解决依赖项

Debian系统默认是没有`sudo`的.确保你的系统已经更新到最新状态,并安装`sudo`.

``` bash
#run as root!
apt-get update -y
apt-get upgrade -y
apt-get install sudo -y
```
* 安装系统必要的软件包:

``` bash
sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev 
libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server 
redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev 
libicu-dev logrotate python-docutils pkg-config cmake nodejs
```

如果你要用`Kerberos`来验证用户,需要另外安装`libkrb5-dev`:

``` bash
sudo apt-get install libkrb5-dev
```

> 如果你不知道Kerberos是干嘛使得,就不用安装上面的libkrb5-dev了.

* 安装git

``` bash
# 安装Git
sudo apt-get install -y git-core
#检查git的版本,确保git版本不小于1.7.10
git --version
```

如果系统包里的git版本过旧,可以删除系统自带的,然后用源码编译最新的git.

``` bash
# 删除git
sudo apt-get remove git-core
# 安装依赖
sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev 
libssl-dev build-essential
#下载并编译源码
cd /tmp
curl -L --progress 
https://www.kernel.org/pub/software/scm/git/git-2.4.3.tar.gz | tar xz
cd git-2.4.3/
./configure
make prefix=/usr/local all
# 安装到/usr/local/bin目录
sudo make prefix=/usr/local install
# 当编辑config/gitlab.yml(step 5),修改git路径为/usr/local/bin/git
```

> 
注意:为了让Gitlab拥有发送通知邮件的功能,你需要安装一个邮件服务.在Debian系统上默认自带一个`exim4`的附件,但是Ubuntu上并没有附带这个.Ubuntu上我们可以安装`Postfix`来发送邮件.

``` bash
sudo apt-get install -y postfix
```

然后选择`Internet Site`回车后再确认下主机名.

## 2.Ruby环境

在Gitlab生产环境使用Ruby版本管理工具[RVM](http://rvm.io/),[rbenv](https://github.com/sstephenson/rbenv)或者[chruby](https://github.com/postmodern/chruby)常常会带来很多疑难杂症.比如Gitlab-shell版本管理器调用OpenSSH的功能以防止越过ssh对仓库进行pull和push操作.而前面提到的三个版本管理器不支持这样的功能,所以我们强烈建议大家按照下面的方式来安装Ruby.

如果系统上存在旧的Ruby1.8,先删除掉:

``` bash
sudo apt-get remove ruby1.8
```

下载Ruby源码,编译安装:

``` bash
mkdir /tmp/ruby && cd /tmp/ruby
# 这里替换官方文档的下载地址为ruby.taobao.com提供的镜像地址
curl -O --progress https://ruby.taobao.org/mirrors/ruby/ruby-2.2.2.tar.gz 
tar xzf ruby-2.2.2.tar.gz
cd ruby-2.2.2
./configure --disable-install-rdoc
make
sudo make install
```
国内使用Ruby的Gem和Bundler必须要做的事情:

``` bash
# 修改gem安装源为淘宝源
$ sudo gem sources --add https://ruby.taobao.org/ --remove 
https://rubygems.org/
$ sudo  gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org
```

安装Bundler Gem:

``` bash
sudo gem install bundler --no-ri --no-rdoc
# 修改bundler的源为淘宝源
sudo -u git -H bundle config mirror.https://rubygems.org 
https://ruby.taobao.org
```

## 3.Go

从Gitlab8.0开始,Git的HTTP请求由gitlab-git-http-server来处理.我们需要Go编译器来安装gitlab-git-http-server.下面一系列的指令都将假定你用的是64位的Linux系统.你也可以在[GoLang官方网站](https://golang.org/dl)下载其他平台的Go编译器.

``` bash
mkdir /tmp/go && cd /tmp/go
curl -O --progress 
http://www.golangtc.com/static/go/go1.5.1/go1.5.1.linux-amd64.tar.gz
echo '46eecd290d8803887dec718c691cc243f2175fe0  go1.5.1.linux-amd64.tar.gz' 
| shasum -c - && \
  sudo tar -C /usr/local -xzf go1.5.1.linux-amd64.tar.gz
sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
rm go1.5.1.linux-amd64.tar.gz
```

## 4.系统用户

为GitLab创建一个名为git的用户:

``` bash
sudo adduser --disabled-login --gecos 'GitLab' git
```

## 5.数据库

Gitlab官方建议我们用PostgreSQL数据库.如果喜欢用Mysql请前往[Gitlab使用Mysql数据库的安装说明](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/database_mysql.md).

> 注意:Gitlab使用的部分扩展插件需要PostgreSQL版本至少为9.1.

``` bash

# 安装数据库软件包
sudo apt-get install -y postgresql postgresql-client libpq-dev

# 使用系统用户postgres登录到PostgreSQL,目标数据库为template1
sudo -u postgres psql -d template1

# 为Gitlab创建一个用户
# 不要输入 'template1=#', 这是PostgreSQL的提示符
template1=# CREATE USER git CREATEDB;

# 创建Gitlab生产环境数据库并赋予git用户属主权限
template1=# CREATE DATABASE gitlabhq_production OWNER git;

# 退出数据库会话
template1=# \q

# 用git用户测试下是否能登录刚才创建的数据库
sudo -u git -H psql -d gitlabhq_production

# 退出数据库会话
gitlabhq_production> \q
```

## 6.Redis

``` bash
sudo apt-get install redis-server

# 配置redis使用socket来监听
sudo 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

# Enable Redis socket for default Debian / Ubuntu path
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a 
/etc/redis/redis.conf
# 给redis用户组的所有成员授权
echo 'unixsocketperm 770' | sudo tee -a /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

# 应用新的 redis.conf
sudo service redis-server restart

# 把git用户加入redis组
sudo usermod -aG redis git
```

## 7.Gitlab(重头戏来了)

``` bash
# 我们将gitlab安装到git用户的HOME目录
cd /home/git
```

克隆Gitlab源码

``` bash
# 克隆GIT@OSC上的Gitlab源码
sudo -u git -H git clone http://git.oschina.net/qiai365/gitlab-ce.git -b 
8-1-stable gitlab
```

> 如果你想体验最新的非稳定版,你也可以克隆`master`分支,但是不赞同在生产服务器上使用master分支.

配置Gitlab

``` bash
# 进入Gitlab安装目录
cd /home/git/gitlab

# 创建Gitlab主配置文件'gitlab.yml'
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# 更新配置文件
sudo -u git -H vim config/gitlab.yml

# 创建 secrets 配置文件
sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml

# 修改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/

# 修改 tmp/pids/ 和 tmp/sockets/ 的权限
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

# 修改public/uploads/ 权限
sudo -u git -H mkdir -p public/uploads
sudo chmod -R u+rwX  public/uploads

# 修改CI编译和存储目录的权限
sudo chmod -R u+rwX builds/

# 创建 Unicorn 配置文件
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

# 查询CPU核心数
nproc

# 如果你想搭建一个高负载的Gitlab实例,可启用集群模式.
# 修改'worker_processes'参数,至少要跟cpu核心数一样.
# 举例:为2G RAM的服务器修改workers数量为3
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

# Configure Git global settings for git user, used when editing via web 
editor
sudo -u git -H git config --global core.autocrlf input

# 配置 Redis 选项
sudo -u git -H cp config/resque.yml.example config/resque.yml

# 如果之前修改过redis socket的路径,在这个配置文件里面修改为当前的路径.
sudo -u git -H vim config/resque.yml
```

> 重要提示: 一定要按照你自己的情况修改`gitlab.yml`和`unicorn.rb`

修改Gitlab 数据库设置

``` bash
# 此命令仅针对PostgreSQl:
sudo -u git cp config/database.yml.postgresql config/database.yml

# 此命令仅针对MySQL:
sudo -u git cp config/database.yml.mysql config/database.yml

# 以下修改针对MySQL和远程PostgreSQL:
# 修改username/password.
# 生产环境只需要修改第一部分即可.
# 修改'secure password' 为你设置的密码
# 密码字段可以使用"双引号" 
sudo -u git -H editor config/database.yml

# PostgreSQL MySQL都适用:
# 修改database.yml的权限,确保git用户可以读取该文件.
sudo -u git -H chmod o-rwx config/database.yml
```

安装Gems

这个步骤是很多新手头疼的问题,不过你只要严格按照本文关于Ruby环境的搭建来做.还是可以保证你顺利的安装下来的.

> Note: 自bundler1.5.2起,你可以使用`bundle install 
-jN`(`N`就是cpu核心数)安装Gems,速度比之前要快大约60%.详细的内容可以点[此处](http://robots.thoughtbot.com/parallel-gem-installing-using-bundler)查看.不过首先要确保你的bundler版本>=1.5.2(运行bundle
 
-v查看).

``` bash
# PostgreSQL 环境
sudo -u git -H bundle install --deployment --without development test mysql 
aws kerberos

# MySQL 环境
sudo -u git -H bundle install --deployment --without development test 
postgres aws kerberos
```

> Note:如果想使用`kerberos`来验证用户,在`--with-out`选项里面排除`kerberos `

安装GitLab Shell

GitLab Shell是专为GitLab开发的ssh访问和仓库管理的软件.

``` bash 
# 运行安装gitlab shell的任务 (根据自己的redis安装情况修改`REDIS_URL`),这里如果你事先没有clone 
gitlab-shell的仓库,就会自动clone官方的仓库进行安装:
sudo -u git -H bundle exec rake gitlab:shell:install[v2.6.6] 
REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production

# 默认情况下,gitlab-shell的配置是根据Gitlab的配置生产的.
# 你可以运行下面的命令查看和修改gitlab-shell的配置:
sudo -u git -H vim /home/git/gitlab-shell/config.yml
```

> Note: Make sure your hostname can be resolved on the machine itself by 
either a proper DNS record or an additional line in /etc/hosts ("127.0.0.1 
hostname"). This might be necessary for example if you set up gitlab behind 
a reverse proxy. If the hostname cannot be resolved, the final installation 
check will fail with "Check GitLab API access: FAILED. code: 401" and 
pushing commits will be rejected with "[remote rejected] master -> master 
(hook declined)".


安装gitlab-git-http-server

``` bash
cd /home/git
sudo -u git -H git clone 
https://gitlab.com/gitlab-org/gitlab-git-http-server.git
cd gitlab-git-http-server
sudo -u git -H git checkout 0.3.0
sudo -u git -H make
```

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

``` bash
# 进入Gitlab安装目录

cd /home/git/gitlab

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

#  输入 'yes' 来创建数据库表.
#  初始化完成后,会显示 'Administrator account created:',这里会输出默认账号和密码

Administrator account created:

login.........root
password......5iveL!fe
```

> Note:你也可以设置环境变量`GITLAB_ROOT_PASSWORD`,这样在初始数据库的时候就会使用你指定的密码,否则就是上面的默认密码.

``` bash
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production 
GITLAB_ROOT_PASSWORD=yourpassword
```

安全设置 secrets.yml

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

安装Gitlab启动脚本

``` bash
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
```

设置Gitlab为开机自启动

``` bash
sudo update-rc.d gitlab defaults 21
```

配置Logrotate
```
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
```

检查应用状态

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

生成资源(Assets)

```
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
```

启动Gitlab实例

```
sudo service gitlab start
# or
sudo /etc/init.d/gitlab restart
```

-- 
You received this message because you are subscribed to the Google Groups 
"GitLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to gitlabhq+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/gitlabhq/b745396e-41f2-4f41-8e12-3445181a7286%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to