gitlab搭建之旅
前言:由于公司项目需求,需要自行维护一套git环境,调研了目前现有的git托管工具,最终确定使用gitlab这个开源平台。So,之后就尝试搭建一套测试环境,不过此中过程并非一帆风顺(虽早有心理准备),确是经历了一番波折。为了提高后续的部署效率,避免重复错误,在此记个随笔以备忘。
搭建环境:
服务器 -- RedHat 5.4
内核版本 -- linux 2.6.18 x86_64
参考文档:https://github.com/gitlabhq/gitlabhq/blob/stable/doc/install/installation.md 基本上是照着官方这个文档做的,不过官方文档的标准环境是debian环境,所以会有些微区别
概要:
gitlab的安装包括以下几步:
- 安装依赖包
- 安装Ruby
- 创建系统账户
- 安装Gitolite
- 搭建数据库环境
- 搭建GitLab
- 搭建Nginx
1. 安装依赖包
搭建gitlab环境需要安装以下库: (这些安装包都需要事先检查下,否则在后面会出现返工的问题,缺少这些包在编译ruby的时候不会报错,但是在搭建gitlab环境的时候会提示你缺少XXX库,然后还要重新编译ruby,很麻烦)
build-essential,如果没有此group,则分别安装make.gcc,g++,libc等开发包
libffi-devel.
yum install
git.x86_64
yum install curl.x86_64
yum install openssh-server.x86_64
yum install redis.x86_64
yum install postfix.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install curl.x86_64
yum install openssh-server.x86_64
yum install redis.x86_64
yum install postfix.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install openssh-server.x86_64
yum install redis.x86_64
yum install postfix.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install redis.x86_64
yum install postfix.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install postfix.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64
2. 安装ruby (必须是1.9.3+版本)
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p327.tar.gz | tar xz
cd ruby-1.9.3-p327
./configure
make
sudo make install
Install the Bundler Gem:
sudo gem install bundler
3.创建系统账户
sudo useradd -r -m git
sudo useradd -g git gitlab
#Add x privilege to /home/git
sudo chmod g+x /home/git
# Generate the SSH key
sudo -u gitlab -H ssh-keygen -q -N '' -t rsa -f /home/gitlab/.ssh/id_rsa
4. 安装Gitolite
下载gitolite的源代码
cd /home/git
sudo -u git -H git clone -b gl-v320 git://github.com/gitlabhq/gitolite.git /home/git/gitolite
安装gitolite
# Add Gitolite scripts to $PATH
sudo -u git -H mkdir /home/git/bin
sudo -u git -H sh -c 'printf "%b\n%b\n" "PATH=\$PATH:/home/git/bin" "export PATH" >> /home/git/.profile'
sudo -u git -H sh -c 'gitolite/install -ln /home/git/bin'
# Copy the gitlab user's (public) SSH key ...
sudo cp /home/gitlab/.ssh/id_rsa.pub /home/git/gitlab.pub
sudo chmod 0444 /home/git/gitlab.pub
# ... and use it as the admin key for the Gitolite setup
sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gitolite setup -pk /home/git/gitlab.pub"
配置gitolite相关路径的权限
# Make sure the Gitolite config dir is owned by git
sudo chmod 750 /home/git/.gitolite/
sudo chown -R git:git /home/git/.gitolite/
配置仓库路径的权限
# Make sure the repositories dir is owned by git and it stays that way
sudo chmod -R ug+rwXs,o-rwx /home/git/repositories/
sudo chown -R git:git /home/git/repositories/
sudo chmod -R ug-s /home/git/repositories/
find /home/git/repositories/ -type d -print0 | sudo xargs -0 chmod g+s
将域名加到gitlab账户的known_hosts列表中
sudo -u gitlab -H ssh git@localhost
sudo -u gitlab -H ssh git@YOUR_DOMAIN_NAME
sudo -u gitlab -H ssh git@YOUR_GITOLITE_DOMAIN_NAME
测试是否安装成功
# Clone the admin repo so SSH adds localhost to known_hosts ...
# ... and to be sure your users have access to Gitolite
sudo -u gitlab -H git clone git@localhost:gitolite-admin.git /tmp/gitolite-admin
# If it succeeded without errors you can remove the cloned repo
sudo rm -rf /tmp/gitolite-admin
如果测试失败,不要继续往下走,请查看https://github.com/gitlabhq/gitlab-public-wiki/wiki/Trouble-Shooting-Guide帮助解决问题
5. 搭建数据库
gitlab支持两种数据库:Mysql和PostgreSQL
Mysql:
# Install the database packages
sudo yum install mysql.x86_64 mysql-devel.x86_64 mysql-server.x86_64
# Login to MySQL
$ mysql -u root -p
# Create a user for GitLab. (change $password to a real password)
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';
# Create the GitLab production database
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
# Grant the GitLab user necessary permissopns on the table.
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';
# Quit the database session
mysql> \q
# Try connecting to the new database with the new user
sudo -u gitlab -H mysql -u gitlab -p -D gitlabhq_production
PostgreSQL:
# Install the database packages
sudo yum install postgresql.x86_64 postgresql-devel.x86_64 postgresql-server.x86_64
# Login to PostgreSQL
sudo -u postgres psql -d template1
# Create a user for GitLab. (change $password to a real password)
template1=# CREATE USER gitlab WITH PASSWORD '$password';
# Create the GitLab production database & grant all privileges on database
template1=# CREATE DATABASE gitlabhq_production OWNER gitlab;
# Quit the database session
template1=# \q
# Try connecting to the new database with the new user
sudo -u gitlab -H psql -d gitlabhq_production
6. 搭建GitLab
下载代码:
# We'll install GitLab into home directory of the user "gitlab"
cd /home/gitlab
# Clone GitLab repository
sudo -u gitlab -H git clone git://github.com/gitlabhq/gitlabhq.git gitlab
# Go to gitlab dir
cd /home/gitlab/gitlab
# Checkout to stable release
sudo -u gitlab -H git checkout 4-1-stable
设置配置项:
cd /home/gitlab/gitlab
# Copy the example GitLab config
sudo -u gitlab -H cp config/gitlab.yml.example config/gitlab.yml
# Make sure to change "localhost" to the fully-qualified domain name of your
# host serving GitLab where necessary
sudo -u gitlab -H vim config/gitlab.yml
# Make sure GitLab can write to the log/ and tmp/ directories
sudo chown -R gitlab log/
sudo chown -R gitlab tmp/
sudo chmod -R u+rwX log/
sudo chmod -R u+rwX tmp/
# Make directory for satellites
sudo -u gitlab -H mkdir /home/gitlab/gitlab-satellites
# Copy the example Unicorn config
sudo -u gitlab -H cp config/unicorn.rb.example config/unicorn.rb
配置数据库:
# Mysql
sudo -u gitlab cp config/database.yml.mysql config/database.yml
# PostgreSQL
sudo -u gitlab cp config/database.yml.postgresql config/database.yml
//待续…
安装Gems:
cd /home/gitlab/gitlab
sudo gem install charlock_holmes --version '0.6.9'
# For MySQL (note, the option says "without")
sudo -u gitlab -H bundle install --deployment --without development test postgres
# Or for PostgreSQL
sudo -u gitlab -H bundle install --deployment --without development test mysql
配置Git:
gitlab需要能够提交代码到gitolite,所以我们需要设置一个全局的用户信息: email和username (建议直接使用config/gitlab.yml配置文件中的email.from值)
sudo -u gitlab -H git config --global user.name "GitLab"
sudo -u gitlab -H git config --global user.email "gitlab@localhost"
设置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 -H bundle exec rake gitlab:setup RAILS_ENV=production
安装启动脚本:
sudo curl --output /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/4-1-stable/init.d/gitlab
sudo chmod +x /etc/init.d/gitlab
检查应用状态:
检查gitlab和其运行环境是否配置正确:
sudo -u gitlab -H bundle exec rake gitlab:env:info RAILS_ENV=production
确保没有遗漏:
sudo -u gitlab -H bundle exec rake gitlab:check RAILS_ENV=production
如果所有的检查结果都是绿色,那个恭喜你已经成功安装了Gitlab,但是下面仍有一些工作要完成
启动Mysql
sudo service mysql start
# or
sudo /etc/init.d/mysql start
启动Redis
sudo service redis-server start
# or
sudo /etc/init.d/redis restart
启动GitLab
sudo service gitlab start
# or
sudo /etc/init.d/gitlab start
7. 搭建Nginx
安装:
sudo yum install nginx
设置配置文件:
#Download an example site config:
sudo curl --output /etc/nginx/conf.d/gitlab.conf https://raw.github.com/gitlabhq/gitlab-recipes/4-1-stable/nginx/gitlab
#Make sure to edit the config file to match your setup:
# Change **YOUR_SERVER_IP** and **YOUR_SERVER_FQDN**
# to the IP address and fully-qualified domain name
# of your host serving GitLab
sudo vim /etc/nginx/conf.d/gitlab.conf
启动Nginx:
sudo service nginx start
sudo /etc/init.d/nginx start
8. 搭建完成
现在你可以使用管理员账户访问你的gitlab网站了:
admin@local.host
5iveL!fe
9. FQA
1. 用http协议上传代码的时候会hung住,服务器也没反应?
这是因为客户端上传的代码太大,超过了服务器和客户端的限制
解决方案:
增加客户端的buffer大小
git config http.postBuffer 524288000
增加服务端的限制 conf/gitlab.xml
git:
bin_path: /usr/bin/git
# Max size of git object like commit, in bytes
# This value can be increased if you have a very large commits
max_size: 524288000 # 500.megabytes
# Git timeout to read commit, in seconds
timeout: 10
2. 用http协议push时服务器报411错误
这是因为nginx的http头检查为通过
解决方案:重新编译nginx,添加chunkin-nginx-module: https://github.com/agentzh/chunkin-nginx-module
3. 用http协议push时服务器包413错误
这是因为push文件大小超过了nginx的限制
解决方案:
修改nginx配置 /etc/nginx.conf
client_max_body_size 200M;
4. 在线浏览代码的时候,当遇到*.md文件,报错 DistributionNotFound: MarkupSafe>=0.9.2
这是因为本机的python缺少MarkupSafe库
解决方案:
从https://pypi.python.org/pypi/MarkupSafe上下载最新的库安装
5. 在线提交代码时报错:Gitlab::SatelliteNotExistError (Satellite doesn't exist)
可能是post-receive中的命令执行出错:
确定redis-cli的路径正确
#!/usr/bin/env bash
# Version 4.1
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
while read oldrev newrev ref
do
# For every branch or tag that was pushed, create a Resque job in redis.
repo_path=`pwd`
env -i /usr/local/bin/redis-cli rpush "resque:gitlab:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$repo_path\",\"$oldrev\",\"$newrev\",\"$ref\",\"$GL_USER\"]}" > /dev/null 2>&1
done