最新消息:

Linux 下搭建 GitLab 服务器

git admin 4919浏览 0评论

这两天因为项目需求需要搭建一个GitLab服务器,遇到了很多问题,参考了很多网络资料,终于搭建成功,在此把这个过程记录一下,利人利己。

一、最终目的

1,在Linux下创建GitLab服务器,客户端能够完成git 的 clone,pull,commit,push操作。

2,能够通过浏览器访问服务器上的GitLab主页,登录之后能够实现,创建工程,增加用户等操作。

二、准备知识

虽然按照后续过程能够实现最终目的,但本人强烈建议读者大致了解下以下知识点。(本人就是因为事先对有些知识不了解导致搭建过程中困难重重)

1,git的基本用法

2,gitolite和github

3,ssh认证

4,uginx代理服务器

 

三、搭建环境

服务器: Ubuntu11.04(本人使用的是虚拟机),需要已经启动了ssh服务。

测试客户端:Win7,需要先安装git

 

四、开始搭建

原文链接地址

https://github.com/gitlabhq/gitlabhq/blob/stable/doc/installation.md

参考: http://www.zhigang.net/ (感谢这位网友的分享)

 

 

平台需求:

 

此项目被设计用于Linux操作系统。

也许可以工作在 FreeBSD 与 Mac OS 系统,但我们无法保证系统稳定性与功能完整性。

官方支持的 Linux 发行版:

  • Ubuntu Linux
  • Debian/GNU Linux

它应该工作于:

  • Fedora
  • CentOS
  • RedHat

你使用这些系统需要些运气,但不保证稳定性:

  • MacOS X
  • FreeBSD

GitLab 不能运行于 Windows 并且我们也没有支持的计划。

硬件需求:

我们推荐至少 1GB 内容用于 gitlab 实例。

本安装指南已于 Debian/Ubuntu 测试通过。

  • 安装总共需要6步:
  • 安装依赖包
  • 安装 Ruby
  • 安装 Gitolite
  • 安装与配置 GitLab
  • 启动前端Web服务器
  • 启动Resque进行(用于后台任务)

重要信息

在你发邮件列表询问安装与配置问题之前请确认你已经根据本文完成了所有步骤。

Only create a GitHub Issue if you want a specific part of this installation guide updated.

Also read the Read this before you submit an issue wiki page.

使用这个安装脚本可以轻易的跳过前3个步骤。

# 安装 curl 与 sudo
apt-get install curl sudo

# 三合一命令 :)
curl https://raw.github.com/gitlabhq/gitlab-recipes/master/install/debian_ubuntu.sh | sh
使用这个命令默认的数据库依赖包是mysql的.

现在你可以直接到到第四步

如果你在 Amazon Web Services 使用 Ubuntu 12.04,你可以使用一个命令跳过所有步骤(1-6)

curl https://raw.github.com/gitlabhq/gitlab-recipes/master/install/debian_ubuntu_aws.sh | sh

更多详细信息,你可以阅读此脚本的 HOWTO 部分。

 

笔者注:本人使用了三合一命令欲跳过前三步,但后续安装怎么都不成功,后来才发现第二步安装有问题,按照第2步要求安装ruby就可以了。

所以请关注这个三个一命令状态,确保都能执行成功。

1. 安装依赖包

请记住,Debian 默认并没有安装 sudo,请使用 root 安装它:

apt-get update && apt-get upgrade && apt-get install sudo

现在你可以安装必须包:

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install -y wget curl gcc checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libreadline6-dev libc6-dev libssl-dev libmysql++-dev make build-essential zlib1g-dev libicu-dev redis-server openssh-server git-core python-dev python-pip libyaml-dev postfix libpq-dev

数据库

SQLite

sudo apt-get install -y sqlite3 libsqlite3-dev

MySQL

sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

# Login to MySQL
$ mysql -u root -p

# Create the GitLab production database
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

# Create the MySQL User change $password to a real password
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';

# Grant proper permissions to the MySQL User
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

PostgreSQL

sudo apt-get install -y postgresql-9.2 postgresql-server-dev-9.2

# Connect to database server
sudo -u postgres psql -d template1

# Add a user called gitlab. Change $password to a real password
template1=# CREATE USER gitlab WITH PASSWORD '$password';

# Create the GitLab production database
template1=# CREATE DATABASE IF NOT EXISTS gitlabhq_production;

# Grant all privileges on database
template1=# GRANT ALL PRIVILEGES ON DATABASE gitlabhq_production to gitlab;

# Quit from PostgreSQL server
template1=# q

# Try connect to new database
$ su - gitlab
$ psql -d gitlabhq_production -U gitlab

(译者注:以上3种数据库根据需要安装其一即可)

2. 安装 Ruby

wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz
tar xfvz ruby-1.9.3-p194.tar.gz
cd ruby-1.9.3-p194
./configure
make
sudo make install

3. 安装 Gitolite

为 Git 创建用户:

sudo adduser 
  --system 
  --shell /bin/sh 
  --gecos 'git version control' 
  --group 
  --disabled-password 
  --home /home/git 
  git

为 GitLab 创建用户:

# ubuntu/debian
sudo adduser --disabled-login --gecos 'gitlab system' gitlab

将 gitlab 用户添加到 git 用户组:

sudo usermod -a -G git gitlab

将 git 用户添加到 gitlab 用户组:

sudo usermod -a -G gitlab git

生成密钥:

sudo -H -u gitlab ssh-keygen -q -N '' -t rsa -f /home/gitlab/.ssh/id_rsa

克隆 GitLab 的 Gitolite 分支源代码:

sudo -H -u git git clone -b gl-v304 https://github.com/gitlabhq/gitolite.git /home/git/gitolite

安装:

cd /home/git
sudo -u git -H mkdir bin
sudo -u git sh -c 'echo -e "PATH=$PATH:/home/git/binnexport PATH" >> /home/git/.profile'
sudo -u git sh -c 'gitolite/install -ln /home/git/bin'

sudo cp /home/gitlab/.ssh/id_rsa.pub /home/git/gitlab.pub
sudo chmod 0444 /home/git/gitlab.pub

sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gitolite setup -pk /home/git/gitlab.pub"

权限:

sudo chmod -R g+rwX /home/git/repositories/
sudo chown -R git:git /home/git/repositories/

检查:退出并重新登录以使 git 用户组生效

# 克隆 admin 资源库以将 localhost 添加到 known_hosts
# 并且确认 gitlab 用户有权访问 gitolite
sudo -u gitlab -H git clone git@localhost:gitolite-admin.git /tmp/gitolite-admin

# 如果执行成功,你可以将其删除
sudo rm -rf /tmp/gitolite-admin

重要! 如果你不能克隆 gitolite-admin 资源库,请不要继续本次安装,请根据 Trouble Shooting Guide 并且确认你已经小心的完成上文的全部步骤。

 

笔者注:这一步测试能否克隆成功。本人没有注意这个提示,完成后续安装后发现怎么都不能通过git@localhost:gitolite-admin.git的方式克隆工程,原因就是ssh认证失败,所以请务必确认这一点。顺便说下本人ssh认证失败的原因: /etc/ssh/sshd_config配置文件里面PubkeyAuthentication的值为no,意味着不允许公钥认证,改为yes就可以了。如果还是不能克隆,重复下第3步,并且注意每个命令是否执行成功。或者删除git和gitlab用户,重新执行第3步。

 

4. 克隆 GitLab 源代码并安装先决条件

sudo gem install charlock_holmes --version '0.6.8'
sudo pip install pygments
sudo gem install bundler
cd /home/gitlab

# Get gitlab code. Use this for stable setup
sudo -H -u gitlab git clone -b stable https://github.com/gitlabhq/gitlabhq.git gitlab
(2013/1/6,最近发现最新的版本是4.0.0.rc2,这个版本已经没有支持sqlite,而我选择sql数据库的时候没有成功,
克隆之后执行
sudo -u gitlab git checkout 2.9.1
可以回到2.9.1的版本,这个版本既支持sqlite,其gitlab管理界面也较美观。3.1.0以后的版本管理界面都有点难看。)
 # Skip this for stable setup.(笔者注:执行了上个命令就不用执行这个命令了)
# Master branch (recent changes, less stable)
sudo -H -u gitlab git clone -b master https://github.com/gitlabhq/gitlabhq.git gitlab

cd gitlab

# Rename config files
sudo -u gitlab cp config/gitlab.yml.example config/gitlab.yml

选择你希望使用的数据库

         笔者注:建议选择SQLite
# SQLite
sudo -u gitlab cp config/database.yml.sqlite config/database.yml

# Mysql
sudo -u gitlab cp config/database.yml.mysql config/database.yml

# PostgreSQL
sudo -u gitlab cp config/database.yml.postgres config/database.yml

# 修改 config/database.yml 确认输入了正确的用户名/密码

安装数据库 gems

# mysql
sudo -u gitlab -H bundle install --without development test sqlite postgres  --deployment

# 或者 postgres
sudo -u gitlab -H bundle install --without development test sqlite mysql --deployment

# 或者 sqlite
sudo -u gitlab -H bundle install --without development test mysql postgres  --deployment

初始化数据库

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

设置 GitLab hooks

sudo cp ./lib/hooks/post-receive /home/git/.gitolite/hooks/common/post-receive
sudo chown git:git /home/git/.gitolite/hooks/common/post-receive

确认应用程序状态:

sudo -u gitlab bundle exec rake gitlab:app:status RAILS_ENV=production

# OUTPUT EXAMPLE
Starting diagnostic
config/database.yml............exists
config/gitlab.yml............exists
/home/git/repositories/............exists
/home/git/repositories/ is writable?............YES
remote: Counting objects: 603, done.
remote: Compressing objects: 100% (466/466), done.
remote: Total 603 (delta 174), reused 0 (delta 0)
Receiving objects: 100% (603/603), 53.29 KiB, done.
Resolving deltas: 100% (174/174), done.
Can clone gitolite-admin?............YES
UMASK for .gitolite.rc is 0007? ............YES
/home/git/share/gitolite/hooks/common/post-receive exists? ............YES

笔者注:如果所有结果都是 YES,恭喜!你可以继续进行下一步。

5. 设置 web server

应用可以用下一个命令行动:

# 用于测试目的
sudo -u gitlab bundle exec rails s -e production

# 用于守护进程
sudo -u gitlab bundle exec rails s -e production -d

默认登录用户名及密码:

笔者注:记住这个用户名和密码,在通过浏览器登录gitlab工程主页的时候有用。

admin@local.host
5iveL!fe

 

 

6. 运行 Resque 进程(用于处理工作队列)

 

# 手动启动
sudo -u gitlab bundle exec rake environment resque:work QUEUE=* RAILS_ENV=production BACKGROUND=yes

# GitLab 启动脚本
sudo -u gitlab ./resque.sh
# 如果你使用 root 运行此脚本,会导致 /home/gitlab/gitlab/tmp/pids/resque_worker.pid 文件的拥有者为 root
# 将导致 resque 在下一次系统初始化中无法启动

自定义 Resque 使用的 Redis 连接

如果你希望 Resque 连接到一个非标准端口号或另一台服务器上的 Redis,你可以在 config/resque.yml 文件修改连接信息:

production: redis.example.com:6379

好了,我们已经拥有了一个工作正常的 GitLab 了,但请继续下去,有一些事情是必须完成的。

Nginx 与 Unicorn

1. Unicorn

cd /home/gitlab/gitlab
sudo -u gitlab cp config/unicorn.rb.example config/unicorn.rb
sudo -u gitlab bundle exec unicorn_rails -c config/unicorn.rb -E production -D

2. Nginx

# 初次安装 Nginx
sudo apt-get install nginx

# 添加GitLab 到 nginx sites
sudo wget https://raw.github.com/gitlabhq/gitlab-recipes/master/nginx/gitlab -P /etc/nginx/sites-available/
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

# 修改 **YOUR_SERVER_IP** 与 **YOUR_SERVER_FQDN**
# 为起初的 IP 地址与准备让 GitLab 服务的域名
sudo vim /etc/nginx/sites-enabled/gitlab

笔者注:本人最初的时候不知道这个配置文件怎么配置,在浏览器里输入服务器ip的时候老是出现“welcome to nginx”页面。
后来的配置是
listion 80;  #监听所有80端口的客户端请求
server_name: 192.168.1.120; #这是我ubuntu服务器的ip地址。因为我们是小组局域网访问,所以直接配ip地址就可以了。远程访问的话可以通过vpn链接。
# 重启 nginx:
sudo /etc/init.d/nginx restart

3. Init 脚本

在 /etc/init.d/gitlab 创建 init 脚本:

sudo wget https://raw.github.com/gitlabhq/gitlab-recipes/master/init.d/gitlab -P /etc/init.d/
sudo chmod +x /etc/init.d/gitlab

设置 GitLab 自动启动:

sudo update-rc.d gitlab defaults 21

现在你可以用这种方式启动/重启/停止 GitLab 服务:

sudo /etc/init.d/gitlab restart

 

至此搭建过程全部完成,关于添加用户和创建工程请参考这篇博文:

http://blog.csdn.net/passion_wu128/article/details/8218041

https://github.com/xiaobozi/gitlab-recipes/blob/master/install/CentOS_6.md

演示地址

http://demo.gitlab.com/users/sign_in

转载请注明:爱开源 » Linux 下搭建 GitLab 服务器

您必须 登录 才能发表评论!