Ubuntu搭建GitLab

GitHub是一个相当不错的代码托管服务,简单易用。但如果是非付费用户的话(绝大多数都是),创建的项目都是公开的,对于某些做研究或者商业项目的人来说,就有些不太适合了。如果有自己的服务器或者比较空闲的VPS,搭建一个自己专属的git版本管理服务器倒不错。其中一个理想选择就是GitLab。

本文部署环境:安装Ubuntu 12.04 x86的VPS一台

GtiLab官方文档其实相当详尽,所以这里也是基于官方的安装方法来作介绍

1. 安装相关包和依赖

# run as root
apt-get update
apt-get upgrade
apt-get install sudo

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

# 安装依赖包
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
sudo apt-get install python

# 查看python版本是否高于2.5
python --version

# 如果默认python版本为python3,则安装python2
sudo apt-get install python2.7

# 检查python2安装情况
python2 --version

# 替换python链接到python2
sudo ln -s /usr/bin/python /usr/bin/python2

# 安装邮件服务器,用以发送邮件通知
sudo apt-get install postfix 

# 如果系统中有其他邮件服务,需停用,避免占用端口,例如本文系统中已安装Sendmail服务
sudo service sendmail stop

# 启用postfix服务
sudo service postfix start

2. 配置Ruby

# 如果已装有ruby1.8,先卸载
sudo apt-get remove ruby1.8

# 下载、编译安装ruby
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p392.tar.gz | tar xz
cd ruby-1.9.3-p392
./configure
make
sudo make install

# 安装Bundler Gem
sudo gem install bundler

3. 为Gitlab创建用户

sudo adduser --disabled-login --gecos 'GitLab' git

4. 配置安装Gitlab Shell

# 切换为用户git
sudo su git

# 进入目录
cd /home/git

# Clone gitlab shell
git clone https://github.com/gitlabhq/gitlab-shell.git
cd gitlab-shell

# switch to right version
git checkout v1.4.0
cp config.yml.example config.yml

# 编辑config.yml文件
# 替换gitlab_url为对应的url
vim config.yml

# 例如我的gitlab地址是http://gitlab.nickdotcat.info,则修改为
gitlab_url: "http://gitlab.nickdotcat.info/"

# 安装
./bin/install

5. 配置数据库(这里采用的是MySQL)

# 安装MySQL数据库
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

# 登录MySQL
mysql -u root -p

# 创建GitLab用户. (将$password改成自己的密码)
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';

# 创建 GitLab production 数据库
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

# 给Gitlab用户授权
mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

# 退出
mysql> \q

# 测试Gitlab用户能否正确连接MySQL
sudo -u git -H mysql -u gitlab -p -D gitlabhq_production

6. 安装配置GitLab

# 将Gitlab安装到git用户目录下
cd /home/git

# 获取Gitlab项目
sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab

# 转到gitlab目录
cd /home/git/gitlab

# 获取5.3版本
sudo -u git -H git checkout 5-3-stable

# 进行配置
cd /home/git/gitlab

# 复制样本配置文件为gitlab.yml
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# 修改该文件,将localhost改为自己服务器域名
sudo -u git -H vim config/gitlab.yml

# 例如
host: nickdotcat.info
host: gitlab.nickdotcat.info
email_from: gitlab@nickdotcat.info
support_email: support@nickdotcat.info

# 使Gitlab对log/和tmp/目录与相关权限
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX  log/
sudo chmod -R u+rwX  tmp/

# 创建satellites目录
sudo -u git -H mkdir /home/git/gitlab-satellites

# 创建sockets/pids目录并修改权限
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/

# 创建public/uploads目录避免备份失败
sudo -u git -H mkdir public/uploads
sudo chmod -R u+rwX  public/uploads

# 复制样本Puma配置文件
sudo -u git -H cp config/puma.rb.example config/puma.rb

# 如果有需要,可以启用cluster模式,此处没启用
sudo -u git -H vim config/puma.rb

# 配置Gitlab全局环境变量
sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "gitlab@localhost

7. 配置Gitlab数据库设定

# 复制样本数据库配置文件
sudo -u git cp config/database.yml.mysql config/database.yml

# 修改为自己的数据库配置
username: gitlab
password: "gitlab的数据库密码"

8. 安装Gems

cd /home/git/gitlab

sudo gem install charlock_holmes --version '0.6.9.4'

# For MySQL (note, the option says "without")
sudo -u git -H bundle install --deployment --without development test postgres

9. 初始化数据库并激活功能

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

10. 安装启动脚本

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

11. 检查程序状态

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

12. 启动Gitlab实例

sudo service gitlab start
# or
sudo /etc/init.d/gitlab restart

13. 再次检查Gitlab状态

# 如果全绿则正常,如果有红色则根据提示进行操作
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

# 如果提示git版本过低,则更新git
# 添加源 git-core/ppa,并更新
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get -y upgrade

14. 安装Nginx,实现web访问

# 一般ubuntu会默认安装apache
# 如果apache正在运行,先停止服务
sudo service apache stop

# 卸载apache
sudo apt-get autoremove --purge apache*

# 然后安装nginx
sudo apt-get install nginx

# 配置gitlab site
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

# 修改该配置文件
sudo vim /etc/nginx/sites-available/gitlab

# YOUR_SERVIER_IP修改为服务器IP
# YOUR_SERVER_FQDN修改为server域名,和在第步配置gitlab_url相符
listen *:80 default_server;
server_name gitlab.nickdotcat.info;

# 重启nginx服务
sudo service nginx restart

至此,GitLab已经顺利安装完毕,访问gitlab的url看看,应该能看到GitLab页面了。

参考链接:
https://github.com/gitlabhq/gitlabhq/blob/5-3-stable/doc/install/installation.md
https://github.com/gitlabhq/gitlabhq/blob/5-3-stable/doc/install/databases.md

原文地址:点击打开链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值