在自己的服务器上部署 GitLab 社区版

GitLab 简介

因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新。这篇文章是在 Gitlab 7.4 的环境下配置的,相关内容可能已经过时。
后续做了一次迁移,将 Gitlab 升级到了 8.9,又重新整理了一篇文章 CentOS 7 Minimal 安装 Gitlab 8.9,如果有需要可以前往查看。
其他相关文章:

  1. Git & Gitlab 使用指南
  2. CentOS 6.5 环境下 Gitlab 7.5 安装和维护笔记

提供 Git 项目仓库托管服务的有业界闻名的 GitHub,但你要将代码上传到 GitHub上面,而且将项目设为私有还要收费。而 GitLab 则是开源免费的(社区版免费,企业版需要订阅),同样是采用了 Ruby on Rails 开发,可以让你在自己的内网搭建一个“山寨版”的 GitHub。GitHub 的使用体验是诱人的,因此部署自己的 GitLab 就十分吸引人。

Gitlab的Github地址
https://github.com/gitlabhq/gitlabhq

安装部署

以下内容为自己阅读官方文档并试验后的学习笔记加部分翻译

GitLab 提供了两种方式来安装,一种是使用官方打包好的文件,这种方法比较简单,也不容易出错;另一种是手动安装,就是把代码下载下来,然后安装各种环境,数据库,编译环境等等,一步一步安装起来,这种就比较麻烦,但是可以安装各种分支版本,比如中文翻译版

先介绍下自己的环境,是在虚拟机中安装的:
Ununtu 14.04,GitLab 7.4.2 Community Edition

一、使用官方安装包安装

官方安装文档
https://about.gitlab.com/downloads/

1. 下载gitlab安装包

https://downloads-packages.s3.amazonaws.com/ubuntu-14.04/gitlab_7.4.2-omnibus-1_amd64.deb
官方文档使用wget,但是这个文件有280MB,还是用迅雷先下载好,速度比较快。

2. 安装 openssh-server

sudo apt-get install openssh-server

3. 安装邮件服务器

sudo apt-get install postfix

选择Internet Site
这里选择Internet Site

设置FQDN
然后让设置FQDN,但是使用默认的机器名即可,这里不是很确定。

4. 安装gitlab安装包

sudo dpkg -i gitlab_7.4.2-omnibus-1_amd64.deb

安装完之后,会建立一个git用户和一个gitlab-www用户,在GitLab上建立的代码仓库都是放在/home/git/下面。但是奇怪的是,该目录竟然对当前登录的管理员用户不可见,需要用git用户登录才可以,而且git用户还不能登录到图形窗口,只能登录到命令行。由于git用户建立时使用了--disabled-login,需要先设置密码才能登录,可以用passwd git,修改git用户的密码。

5. 配置 gitlab

这一步在官方的文档里面没有,但是如果没有配置的话,直接启动GitLab,会出现不正确的FQDN错误,导致无法正常启动。因此必须做配置。
这边的配置使用这里分享的经验。

sudo mkdir -p /etc/gitlab
sudo touch /etc/gitlab/gitlab.rb
sudo chmod 600 /etc/gitlab/gitlab.rb
sudo gedit /etc/gitlab/gitlab.rb

![修改'\({external_url}'][5] 把'\){external_url}'改成部署机器的域名或者IP地址

这个地址很重要,上传的图片什么的,url会以这个为基准,如果地址写错,将无法访问到图片等这些资源。

6. 然后对GitLab进行重配置即可

这一步也是启动 GitLab

sudo gitlab-ctl reconfigure

7. 打开浏览器登陆

Username: root
Password: 5iveL!fe

第一次启动的时候,需要初始化大量的东西,经常会出现502错误,通常是由于内存不足的原因导致,所以需要准备好足够的内存。官方推荐生产环境中使用2G内存,2核CPU。虚拟机中测试1GB内存基本就可以了。

二、手动安装

官方安装文档
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md

安装前最好先确认下能否访问amazon aws上面的资源,因为有很多安装包需要从这里获取,如果不行就先配置下代理服务器或者VPN,解决这个问题。否则后面的安装会很痛苦。

安装过程包含以下几个内容

  1. Packages / Dependencies
  2. Ruby
  3. System
  4. Users
  5. Database
  6. Redis
  7. GitLab
  8. Nginx

1. Packages / Dependencies

需要先更新下系统,不然有些依赖的包会找不到

sudo apt-get update -y 
sudo apt-get upgrade -y
sudo apt-get install sudo -y

下面这一步为可选,如果熟悉用vim就安装vim为默认的编辑器,或者选择其它

sudo apt-get install -y vim
sudo update-alternatives --set editor /usr/bin/vim.basic

安装依赖,编译 Ruby 的时候需要用到

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

安装 Git

sudo apt-get install -y git-core

需要确保 Git 的版本在 1.7.10 以上,如 1.7.12 或 2.0.0

git --version

如果当前系统中的 Git 版本太低,可以先删除后安装,如果版本没问题就跳过这一步。

删除 Git

sudo apt-get remove git-core 

安装依赖

sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev build-essential

下载源码并编译安装 Git

cd /tmp
curl -L --progress https://www.kernel.org/pub/software/scm/git/git-2.1.2.tar.gz | tar xz
cd git-2.1.2/
make prefix=/usr/local all

安装到 /usr/local/bin

sudo make prefix=/usr/local install

When editing config/gitlab.yml (Step 5), change the git -> bin_path to /usr/local/bin/git

安装邮件服务器,用于发送 GitLab 的通知邮件

sudo apt-get install -y postfix
Then select 'Internet Site' and press enter to confirm the hostname.

安装邮件服务器,选择 Internet Site,并在 FQDN 处,设置为主机名(缺省就是主机名)

2. Ruby

如果已经安装了 Ruby 1.8 版本,先删除

sudo apt-get remove ruby1.8

下载并编译安装 Ruby

mkdir /tmp/ruby && cd /tmp/ruby
curl -L --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
sudo make install

安装 Bundler Gem:

sudo gem install bundler --no-ri --no-rdoc

如果步骤1中的依赖部分,有些没有安装成功的话,这里就会出现错误,比如
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass

3. System Users

在系统中为 GitLab 创建一个 git 用户

Create a git user for GitLab:
sudo adduser --disabled-login --gecos 'GitLab' git

这里创建的系统用户,显示的用户名是GitLab,但是在home目录下已经有/home/git了

4. Database

官方推荐使用 PostgreSQL 数据库

安装数据库包

sudo apt-get install -y postgresql postgresql-client libpq-dev

登录到 PostgreSQL

sudo -u postgres psql -d template1

为 GitLab创建一个用户

template1=# CREATE USER git CREATEDB;

创建 GitLab 生成环境数据库,并给数据库授予所有权限

template1=# CREATE DATABASE gitlabhq_production OWNER git;

退出数据库会话

template1=# \q

试一下用新建的用户连接到新建的数据库,然后退出会话

sudo -u git -H psql -d gitlabhq_production
template1=# \q

5. Redis

安装 resis 服务器

sudo apt-get install redis-server

配置 redis 以使用 sockets

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig

将 redis 的监听端口设置为 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

Grant permission to the socket to all members of the redis group

echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf

Create the directory which contains the socket

sudo 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

Activate the changes to redis.conf

sudo service redis-server restart

Add git to the redis group

sudo usermod -aG redis git

6. GitLab

GitLab 将被安装到/homt/git目录下

cd /home/git

通过克隆 GitLab 代码仓库的方式下载 GitLab

sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-4-stable gitlab

这里可以替换为其他版本,例如中文翻译版

sudo -u git -H git clone https://gitlab.com/larryli/gitlab.git -b 7-4-zh gitlab

**配置 GitLab**

切换到 GitLab 安装目录

cd /home/git/gitlab

复制一份 GitLab 配置文件的例子

sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

更新配置文件

sudo -u git -H editor config/gitlab.yml

需要配置两项

host: example.com # Gitlab settings
email_from: example@example.com # Email settings 

host 这个字段的值要改成服务器的ip或域名,不要包含 http,这个字段很重要,会影响到后面建立项目的地址,以及上传的图片的地址,如把 host 配置成192.168.137.135,那么项目地址就会使这样
http://192.168.137.135/username/test_project.git

email_from 配置的项就是 Gitlab 发邮件时的发件人,这里一定要写成一个合法的 email 地址,可以任意。
当新建用户,或者有评论时,就会收到这里设置的邮箱发来的邮件。如果这里填的邮箱格式有错,将导致收不到邮件。

为 GitLab 配置写 log/ 和 tmp/ 目录的权限

sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX log/
sudo chmod -R u+rwX tmp/

Create directory for satellites

sudo -u git -H mkdir /home/git/gitlab-satellites
sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites

为 GitLab 配置写 tmp/pids/ 和 tmp/sockets/ 目录的权限

sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

为 GitLab 配置写 public/uploads/ 目录的权限

sudo chmod -R u+rwX  public/uploads

复制一份 Unicorn 配置文件的例子

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

查看当前机器有多少核CPU

nproc

Enable cluster mode if you expect to have a high load instance
Ex. change amount of workers to 3 for 2GB RAM server
Set the number of workers to at least the number of cores

sudo -u git -H editor 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, useful when editing via web

根据 gitlab.yml 中配置的信息来配置 user.email

sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "example@example.com"
sudo -u git -H git config --global core.autocrlf input

配置 redis 连接设置

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

Change the Redis socket path if you are not using the default Debian / Ubuntu configuration

sudo -u git -H editor config/resque.yml

Important Note: Make sure to edit both gitlab.yml and unicorn.rb to match your setup.

Note: If you want to use HTTPS, see Using HTTPS for the additional steps.

配置 GitLab 数据设置

PostgreSQL only:

sudo -u git cp config/database.yml.postgresql config/database.yml

MySQL only:

sudo -u git cp config/database.yml.mysql config/database.yml

MySQL and remote PostgreSQL only:
Update username/password in config/database.yml.
You only need to adapt the production settings (first part).
If you followed the database guide then please do as follows:
Change 'secure password' with the value you have given to $password
You can keep the double quotes around the password

sudo -u git -H editor config/database.yml

PostgreSQL and MySQL:
Make config/database.yml readable to git only

sudo -u git -H chmod o-rwx config/database.yml

Install Gems

Note: As of bundler 1.5.2, you can invoke bundle install -jN (where N the number of your processor cores) and enjoy the parallel gems installation with measurable difference in completion time (~60% faster). Check the number of your cores with nproc. For more information check this post. First make sure you have bundler >= 1.5.2 (run bundle -v) as it addresses some issues that were fixed in 1.5.2.

For PostgreSQL (note, the option says "without ... mysql")
如果使用 PostgreSQL,请执行这条命令

sudo -u git -H bundle install --deployment --without development test mysql aws

这里如果出现安装错误,请检查前面的依赖是否安装正确

Or if you use MySQL (note, the option says "without ... postgres")
如果使用 MySQL,请执行这条命令

sudo -u git -H bundle install --deployment --without development test postgres aws

**安装 GitLab Shell**

GitLab Shell is an SSH access and repository management software developed specially for GitLab.

Run the installation task for gitlab-shell (replace REDIS_URL if needed):

sudo -u git -H bundle exec rake gitlab:shell:install[v2.0.1] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production

By default, the gitlab-shell config is generated from your main GitLab config.
You can review (and modify) the gitlab-shell config as follows:

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

如果要使用HTTPS,则要去查看官方文档的HTTPS的配置步骤。


**Initialize Database and Activate Advanced Features**
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production

Type 'yes' to create the database tables.

When done you see 'Administrator account created:'
Note: You can set the Administrator password by supplying it in environmental variable GITLAB_ROOT_PASSWORD, eg.:

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

Install Init Script

Download the init script (will be /etc/init.d/gitlab):

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

And if you are installing with a non-default folder or user copy and edit the defaults file:

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

If you installed GitLab in another directory or as a user other than the default you should change these settings in /etc/default/gitlab. Do not edit/etc/init.d/gitlab as it will be changed on upgrade.

Make GitLab start on boot:

sudo update-rc.d gitlab defaults 21

Setup Logrotate

sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

检查 GitLab 的环境是否配置正确

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

如果内存不够,会导致无法分配内存,出错。给虚拟机增加内存,重启后,再次执行出现错误could not locate gemfile
原因是执行当前命令所在的目录没有gemfile,切换到目录Gitlab安装目录
cd /home/git/gitlab
再次执行就可以了

Compile Assets

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

Start Your GitLab Instance

sudo service gitlab start

或者

sudo /etc/init.d/gitlab restart

7. Nginx

Note: Nginx is the officially supported web server for GitLab. If you cannot or do not want to use Nginx as your web server, have a look at the GitLab recipes.

Installation

sudo apt-get install -y nginx

Site Configuration

Copy the example site config:

sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

Make sure to edit the config file to match your setup:

Change YOUR_SERVER_FQDN to the fully-qualified
domain name of your host serving GitLab.

配置 Nginx

sudo editor /etc/nginx/sites-available/gitlab

需要配置两项,servre中的listen和server_name,server_name跟gitlab.yml
中的host字段配置成一样。
关键一点是去掉listen *:80 后面的default_server
否则执行sudo nginx -t时会出现nginx emerg a duplicate default server错误

如果是用gedit编辑的话,会在/etc/nginx/sites-available/保存一个default
需要删除这个文件,否则会导致nginx出现
nginx error “conflicting server name” ignored
可以在var/log/niginx/error 里面看到

症状:
访问localhost,只出现nginx的欢迎页,没有出现gitlab的登录页面,
在后面执行
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
会发现gitlab-shell出错
gitlab-shell出错

Note: If you want to use HTTPS, replace the gitlab Nginx config with gitlab-ssl. See Using HTTPS for HTTPS configuration details.

Test Configuration

Validate your gitlab or gitlab-ssl Nginx config file with the following command:

sudo nginx -t

You should receive syntax is okay and test is successful messages. If you receive errors check your gitlab or gitlab-ssl Nginx config file for typos, etc. as indicated in the error message given.

Restart

sudo service nginx restart

结束,所有的都配置完了。执行下面的命令来验证是否配置成功。

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

如果所有的项目都是绿色的,表示 GitLab 已经成功安装了!

NOTE: Supply SANITIZE=true environment variable to gitlab:check to omit project names from the output of the check command.

打开浏览器登陆吧!
用户名:root
密码:5iveL!fe

登录

其他问题:

如果安装的是 Larry Li 翻译的 7-2-zh,在新建项目的时候,如果名称填写的不符合要求,会提示
Namecan contain only letters, digits, '', '-' and '.' and space. It must start with letter, digit or ''.

第一个单词Name和can之间少了一个空格,安装官方推荐方式安装7.4.2官方版,没有这个问题。
找到文件
/home/git/gitlab/lib/gitlab/regex.rb

找到 project_regex_message,在前面加上空格,其他类似的也可以一并修改

def project_regex_message
        #"can contain only letters, digits, '_', '-' and '.' and space. " \
        " can contain only letters, digits, '_', '-' and '.' and space. " \
        "It must start with letter, digit or '_'."
end
posted @ 2014-10-31 09:13  NaN-Hax  阅读(69602)  评论(0编辑  收藏  举报