阿里云部署gitlab填坑记

本文同步自我的博客
地址:http://www.reeoo.me/archives/gitlab.html

我的博客是基于hexo生成的静态博客,每次写完文章,hexo g一下,然后把生成的文件用ftp上传到server,虽然一切正常,但是感觉这种方式很low,想尝试一下自动部署,无奈在服务器上部署git服务器多次,都没能成功,经群里面的大神推荐,于是尝试一把gitlab

像往常一样,不会搞,先在网上找一篇教程,CentOS安装GitLab,按照这篇,搞了一边,各种坑,边填坑边记录吧。

软件环境

先来说说我的软件环境

软件 版本
System centos6.7
Python 2.6
Ruby 2.1.5
Git 2.7.0
Redis 2.0
GitLab 7.8.4
GitLab Shell 2.6.0

大概如此。

yum源切换

由于天朝众所周知的原因,为了提高软件安装速度,可以将yum源设置为阿里云开源镜像。

cd /etc/yum.repos.d
wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

但是某些包还是安装的很慢。

必要软件包

yum -y install libicu-devel patch gcc-c++ readline-devel zlib-devel libffi-devel openssl-devel make autoconf automake libtool bison libxml2-devel libxslt-devel libyaml-devel zlib-devel openssl-devel cpio expat-devel gettext-devel curl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

安装Git

//查看当前git版本
git --version
// 如果小于1.7.10则先卸载
yum remove git
// 下载最新的git并安装
wget -O git-src.zip https://github.com/git/git/archive/master.zip
unzip git-src.zip
cd git-src
make prefix=/usr/local all
make prefix=/usr/local install
ln -fs /usr/local/bin/git* /usr/bin/

安装Ruby环境

mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/ruby-2.1.5.tar.gz | tar xz
// 这步有点慢,等的我差点睡着了,基本上一秒钟0.1%    
cd ruby-2.1.5
./configure --disable-install-rdoc
make && make install

ln -s /usr/local/bin/bundle /usr/bin/bundle
ln -s /usr/local/bin/ruby /usr/bin/ruby
ln -s /usr/local/bin/gem /usr/bin/gem

// 设置ruby gem源为淘宝源
gem source -r https://rubygems.org/
gem source -a https://ruby.taobao.org/  //这里应该是https,原文是`http`,也是个坑
gem install bundler --no-ri --no-rdoc

安装MySQL及初始化GitLab库

yum install mysql mysql-devel mysql-server -y
/etc/init.d/mysqld start
chkconfig mysqld on

// 登录mysql创建gitab的帐号和数据库
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab';
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

安装Redis

yum -y install redis
/etc/init.d/redis start
chkconfig redis on

添加git帐号并允许sudo

useradd --comment 'gitlab' git
echo "git ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers

测试是否可以用git帐号登录数据库

sudo -u git -H mysql -u gitlab -p -D gitlabhq_production

安装GitLab

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-8-stable gitlab
cd /home/git/gitlab
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
   
//编辑git路径, gitlab的host:port
vim config/gitlab.yml
// bin_path: /usr/local/bin/git
// host: localhost
// port: 80 
   
// 给文件夹添加相应的权限
chown -R git log/
chown -R git tmp/
chmod -R u+rwX  log/
chmod -R u+rwX  tmp/
   
// 创建必要的文件夹,以及复制配置文件
sudo -u git -H mkdir /home/git/gitlab-satellites
sudo -u git -H mkdir tmp/pids/
sudo -u git -H mkdir tmp/sockets/
sudo chmod -R u+rwX  tmp/pids/
sudo chmod -R u+rwX  tmp/sockets/
sudo -u git -H mkdir public/uploads
sudo chmod -R u+rwX  public/uploads
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
sudo -u git -H cp config/initializers/rack_attack.rb.example
config/initializers/rack_attack.rb
// 可能某些文件夹已经存在了,直接跳过~~~
// 配置数据库连接信息
sudo -u git cp config/database.yml.mysql config/database.yml
sudo -u git -H vim  config/database.yml
vim config/database.yml
// production:
// username: gitlab
// password: "gitlab

安装GitLab-Shell

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-shell.git -b v2.6.0
cd gitlab-shell/
sudo -u git -H cp config.yml.example config.yml

// 编辑配置文件, 设置gitlab_url, redis-cli, log-level...
vim config.yml
// gitlab_url: "http://localhost/"
// /usr/bin/redis-cli

// 安装git-shell
sudo -u git -H ./bin/install

安装需要ruby的gems

在执行下面的命令之前先要把gem的默认源改为淘宝的,虽然前面改过了,但这里还是默认的没改的,不知道怎么回事,需要vim Gemfile,修改source "https://rubygems.org"https://ruby.taobao.org/,注意是https不是http

cd /home/git/gitlab
sudo -u git -H bundle install --deployment --without development test postgres aw  

这个过程可能会出现下面的错误:

An error occurred while installing rugged (0.21.2), and Bundler cannot continue.
Make sure that `gem install rugged -v '0.21.2'` succeeds before bundling.

需要安装:

sudo yum -y install cmake

然后重新执行就OK了。

初始化数据库(创建GitLab相关表)

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

这步完成会出现用户名和密码

Administrator account created:
login.........root
password......5iveL!fe

安装启动文件以及日志切割文件

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

设置git帐号信息

$ sudo -u git -H git config --global user.name "Reeoo Shen"
$ sudo -u git -H git config --global user.email "ireeoo@163.com"
$ sudo -u git -H git config --global core.autocrlf input

这一步虽然设置了这些信息,但是到后面执行检查的时候,还是会说你没有设置。。。

安装nginx

我的服务器本来就装的nginx,这一步我就不写了
更改权限,启动nginx

nginx -t
chown -R git:git /var/lib/nginx/
/etc/init.d/nginx start

检测当前环境

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

输出:

[root@iZ234fbksx3Z gitlab]# sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production

System information
System:        CentOS 6.7
Current User:    git
Using RVM:    no
Ruby Version:    2.1.5p273
Gem Version:    2.2.2
Bundler Version:1.11.2
Rake Version:    10.3.2
Sidekiq Version:3.3.0

GitLab information
Version:    7.8.4
Revision:    019ffd9
Directory:    /home/git/gitlab
DB Adapter:    mysql2
URL:        http://testxxx.reeoo.me
HTTP Clone URL:    http://testxxx.reeoo.me/some-project.git
SSH Clone URL:    git@testxxx.reeoo.me:some-project.git
Using LDAP:    no
Using Omniauth:    no

GitLab Shell
Version:    2.6.0
Repositories:    /home/git/repositories/
Hooks:        /home/git/gitlab-shell/hooks/
Git:        /usr/local/bin/git

拉取gitlab静态资源文件

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

启动gitlab

/etc/init.d/gitlab start

检测各个组件是否正常工作

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

这里就是坑了,各种no。。。。
是不是我看错教程了,我怀疑这个教程本来就是跑不通的,尼玛。。。
经过将近一天的折腾,终于把问题降到了就剩下一个了:

Checking Environment ...

Git configured for git user? ... yes

Checking Environment ... Finished

Checking GitLab Shell ...

GitLab Shell version >= 2.5.4 ? ... OK (2.6.0)
Repo base directory exists? ... yes
Repo base directory is a symlink? ... no
Repo base owned by git:git? ... yes
Repo base access is drwxrws---? ... yes
Satellites access is drwxr-x---? ... yes
hooks directories in repos are links: ... 
reeoo / mytest ... ok
Running /home/git/gitlab-shell/bin/check
Check GitLab API access: OK
Check directories and files: 
    /home/git/repositories: OK
    /home/git/.ssh/authorized_keys: OK
Test redis-cli executable: redis-cli 2.4.10
Send ping to redis server: PONG
gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes
Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Checking LDAP ...

LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab ...

Database config exists? ... yes
Database is SQLite ... no
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config outdated? ... no
Log directory writable? ... yes
Tmp directory writable? ... yes
Init script exists? ... yes
Init script up-to-date? ... no
  Try fixing it:
  Redownload the init script
  For more information see:
  doc/install/installation.md in section "Install Init Script"
  Please fix the error above and rerun the checks.
projects have namespace: ... 
reeoo / mytest ... yes
Projects have satellites? ... 
reeoo / mytest ... yes
Redis version >= 2.0.0? ... yes
Ruby version >= 2.0.0 ? ... yes (2.1.5)
Your git bin path is "/usr/local/bin/git"
Git version >= 1.7.10 ? ... yes (2.7.0)

Checking GitLab ... Finished

就是这个

Init script up-to-date? ... no
  Try fixing it:
  Redownload the init script
  For more information see:
  doc/install/installation.md in section "Install Init Script"
  Please fix the error above and rerun the checks.

有人说可以忽略这个,我也没找到解决方案,只能暂时忽略了,不知道后面的本地clone仓库里面的代码老是要求输入密码,是不是跟这个也有关系。

最后访问testxxx.reeoo.me出现502 Bad GateWay
查询nginx日志,
tail /var/log/nginx/gitlab_error.log
发现是"/home/git/gitlab/public/favicon.ico.html" failed (13: Permission denied)

加上权限,chmod 775 /home/gittestxxx.reeoo.me可以访问了(之前的那个检查里面有好多有问题的时候,貌似testxxx.reeoo.me也是可以正常访问的),新建个项目,发现不能commit
Your changes could not be committed, because the file has been changed(在gitlab服务器上commit
commit就报这个错。。。
发现gitlab-shell/config.yml里面的gitlab_url: "testxxx.reeoo.me:80"是这样的,
改成gitlab_url: "http://testxxx.reeoo.me"重启再试,我靠,好了,Your changes have been successfully committed

问题

  1. 前面说到设置了git的信息,但执行检查的时候,还是会说你没有设置git信息,
    参考Error in Gitlab: git configured for git user … no try fixing it

解决了

  1. 关于redis配置的错误:

        sudo -u git ./check
        Check GitLab API access: OK
        Check directories and files:
                /home/git/repositories: OK
                /home/git/.ssh/authorized_keys: OK
        Test redis-cli executable: redis-cli 2.4.10
        Send ping to redis server: Could not connect to Redis at /var/run/redis/redis.sock: No such file or directory

    到/home/git/gitlab-shell/config.yml里面把socket注掉,重新运行检查就好了:

        redis:
          bin: /usr/bin/redis-cli
          # host: 127.0.0.1
          # port: 6379
          # pass: redispass # Allows you to specify the password for Redis
          database: 0
          #socket: /var/run/redis/redis.sock # Comment out this line if you want to use TCP //把这行注掉
          namespace: resque:gitlab
  2. 期间还遇到一个问题,说可用内存不足的(我的阿里云服务器单核1G内存),网上查了一下,貌似需要做别的处理,挺麻烦,直接又花了400RMB把内存升级到了2G。

  3. 最后一个问题,也是发这篇文章的时候还没解决的:
    把ssh key上传到gitlab之后,clone仓库,老是要求输入密码,但是密码怎么输都不对,不知道怎么回事,百度谷歌了N多资料,也没有解决,目前gitlab算是部署了,但是不能clone,更别提pull,commit,push了,继续折腾中。。。。

等把这个问题解决了,在折腾自动部署的功能。

最后希望遇到过上面第三个问题的网友,说说解决方法,或者探讨一下,谢谢!

阅读 10.7k

推荐阅读
Reeoo's Page
用户专栏

Reeoo's Page

8 人关注
5 篇文章
专栏主页