ZeusYu

部署GitLab小记

Tags: gitlab, git, ubuntu

上个月看到GitLab6.0稳定版发布了,手痒,简单搭个玩玩,照着文档一步步坐下来,稍微遇到点问题,记录一下,以后备查。

1.安装依赖包

sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev

确保系统中的python版本正确(2.5以上,3.x以下)

# 安装python
sudo apt-get install -y python

# 确认python版本  
python --version

# 若python版本不正确,重新安装正确版本的python  
sudo apt-get install python2.7

为了gitlab能够正常使用,还需要配置一个邮件服务器,官方推荐在ubuntu上使用postfix sudo apt-get install -y postfix

安装完postfix后,系统会直接要求进行配置,直接选择’Internet Site’确认就行了。

2.Ruby

系统默认的ruby版本是1.8,略低,先卸载掉吧
sudo apt-get remove -y ruby1.8

下载编译最新的Ruby

mkdir /tmp/ruby && cd /tmp/ruby  
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz | tar xz  
cd ruby-2.0.0-p247  
./configure  
make  
sudo make install

安装Bundler Gem
sudo gem install bundler --no-ri --no-rdoc

3.系统用户

为Gitlab创建一个系统用户
sudo adduser --disabled-login --gecos 'GitLab' git

4.安装GitLab shell

# Go to home directory
cd /home/git

# Clone gitlab shell
sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell.git

cd gitlab-shell

# switch to right version
sudo -u git -H git checkout v1.7.1

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

# Edit config and replace gitlab_url
# with something like 'http://domain.com/'
sudo -u git -H editor config.yml

# Do setup
sudo -u git -H ./bin/install

5.安装数据库

GitLab支持MySQL和PostgreSQL,选了PostgreSQL。

# Install the database packages
sudo apt-get install -y postgresql-9.1 libpq-dev

# Login to PostgreSQL  
sudo -u postgres psql -d template1

# Create a user for GitLab. (change $password to a real password)  
template1=# CREATE USER git WITH PASSWORD '$password';

# Create the GitLab production database & grant all privileges on database  
template1=# CREATE DATABASE gitlabhq_production OWNER git;

# Quit the database session  
template1=# \q

# Try connecting to the new database with the new user
sudo -u git -H psql -d gitlabhq_production

6.安装GitLab

终于开始正题了。

先把代码Clone下来

#We'll install GitLab into home directory of the user "git"  
cd /home/git

# Clone GitLab repository
sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab

# Go to gitlab dir
cd /home/git/gitlab

# Checkout to stable release
sudo -u git -H git checkout 6-0-stable

简单配置一下

cd /home/git/gitlab

# Copy the example GitLab config  
sudo -u git -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 git -H editor config/gitlab.yml

# Make sure GitLab can write to the log/ and tmp/ directories  
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

# Create directories for sockets/pids and make sure GitLab can write to them  
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/

# Create public/uploads directory otherwise backup will fail  
sudo -u git -H mkdir public/uploads  
sudo chmod -R u+rwX  public/uploads

# Copy the example Unicorn config  
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

# Enable cluster mode if you expect to have a high load instance  
# Ex. change amount of workers to 3 for 2GB RAM server
sudo -u git -H editor config/unicorn.rb

# Configure Git global settings for git user, useful when editing via web  
# Edit user.email according to what is set in gitlab.yml
sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "gitlab@localhost"
sudo -u git -H git config --global core.autocrlf input

配置数据库

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

# Make sure to 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 'root' to 'gitlab'  
# 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

# Make config/database.yml readable to git only
sudo -u git -H chmod o-rwx config/database.yml

安装Gem包

cd /home/git/gitlab

#把gem源改成淘宝源http://ruby.taobao.org/  
sudo -u git -H editer Gemfile  

sudo gem install charlock_holmes --version '0.6.9.4'  
sudo -u git -H bundle install --deployment --without development test mysql aws

这里我遇到一个问题,说是bundle不是个命令,略奇怪,不过通过重新安装bundle这个问题算是解决了。

初始化数据库

sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
完成后会显示默认的管理员账号 这里我犯了一个低级错误,在修改数据库配置文件时,在密码那一行的冒号后面少打了一个空格,导致键值对判断出错,执行这行命令的时候报错,⊙﹏⊙b汗

安装初始脚本

sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab  
sudo chmod +x /etc/init.d/gitlab 
 
#随系统自启动  
sudo update-rc.d gitlab defaults 21

启动Gitlab

sudo service gitlab start

7.Nginx

最后配置一下服务器,官方推荐使用Nginx。

安装

sudo apt-get install -y nginx

站点配置

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

# Change YOUR_SERVER_FQDN to the fully-qualified  
# domain name of your host serving GitLab.  
sudo editor /etc/nginx/sites-available/gitlab

这里由于我们只是内网访问,也没有域名可以用,就先用本机ip代替了 YOUR_SERVER_FQDN.

重启服务器

sudo service nginx restart

这下,使用ip访问,用默认管理员登陆,这就完事了。 其中遇到的诸如github被墙这种问题,唉,没啥好说的了。。。