设为首页 收藏本站
查看: 998|回复: 0

[经验分享] gitlab搭建之旅

[复制链接]

尚未签到

发表于 2016-11-22 06:13:17 | 显示全部楼层 |阅读模式
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,很麻烦)



yum groupinstall "Development Tools"  #等同于 apt-get install build-essential,如果没有此group,则分别安装make.gcc,g++,libc等开发包
yum install kernel-devel kernel-headers
yum install zlib-devel.x86_64
yum install libyaml.x86_64
yum install openssl-devel.x86_64
yum install gdbm-devel.x86_64
yum install readline-devel.x86_64
yum install ncurses-devel.x86_64
yum install libffi-devel.x86_64 #可能需要手动下载rpm包安装
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

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中的命令执行出错:
  vi /home/git/.gitolite/hooks/common/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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303611-1-1.html 上篇帖子: docker中建立私有git服务器[gitlab] 下篇帖子: 在自己的服务器上部署 GitLab 社区版
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表