Git Server 安裝 (New)

OS: Ubuntu 14.04 Server x64

安裝Gitlab + MySQL + Nginx

  1. 系統更新
apt-get update
apt-get upgrade -y
apt-get dist-upgrade
  1. 安裝基本需求
# 記得要先裝vim
update-alternatives --set editor /usr/bin/vim.basic
sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate cmake
  1. 安裝python
sudo apt-get install -y python
python --version
python2 --version
# 確認 python版本,如果版本是3.x以上,則需額外安裝2.x版
# sudo apt-get install -y python2.7
# python --version # Python 2.7.3

# python2 --version # 命令執行會採用 python2, 所以需要確認 python2 的版本
# python2 不存在的話, 需要把 python 2.7 版 的 ln 過去.
# sudo ln -s /usr/bin/python /usr/bin/python2

apt-get install -y python-docutils
  1. 安裝Git
# 檢查Git目前可apt安裝的版本
apt-cache showpkg git-core

# 如果版本大於1.7.10,則可以直接使用~否則建議手動安裝新版本
# 直接安裝 => apt-get install -y git-core
# 如果原本就安裝舊有版本,移除 => apt-get remove git-core

# 手動安裝Git
apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev build-essential
cd /tmp
curl --progress https://git-core.googlecode.com/files/git-1.9.0.tar.gz | tar xz
cd git-1.9.0/
make prefix=/usr/local all
make prefix=/usr/local install
  1. 安裝Ruby
# 如果裝有1.8版,先行移除
# apt-get remove ruby1.8

mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p481.tar.gz | tar xz
cd ruby-2.0.0-p481/
./configure --disable-install-rdoc
# 下面這行可以用 make 就好
make -j  `cat /proc/cpuinfo | grep 'cpu cores' | awk '{if ($4=1) print $4; else print $4-1}'`

make install
gem install bundler --no-ri --no-rdoc
  1. 新增Gitlab系統帳號
adduser --disabled-login --gecos 'GitLab' git
  1. 安裝GitLab Shell
cd /home/git
sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell.git
cd gitlab-shell

# 查看最新版本為何
# 或是使用 sudo -u git -H git checkout `sudo -u git -H git describe --abbrev=0 --tags` 自動判斷安裝最新版
sudo -u git -H git tag 
sudo -u git -H git checkout v2.1.0

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

#修改gitlab_url,改成要使用的domain
sudo -u git -H editor config.yml

sudo -u git -H ./bin/install
  1. 安裝MySQL Database
apt-get install -y mysql-server mysql-client libmysqlclient-dev
mysql -u root -p
#修改密碼
> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'GITLAB_PASSWORD';
> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
>GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

# 使用gitlab帳號測試MySQL
sudo -u git -H mysql -u gitlab -p -D gitlabhq_production
  1. 安裝GitLab
cd /home/git
sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab
cd /home/git/gitlab

# 檢查版本,找stable的
# 或是使用下面自動判斷最新且stable的版本進行安裝
# sudo -u git -H git checkout `sudo -u git -H git branch -a | grep 'stable' | awk 'E
ND{print}' | awk 'split($1, version, "/") {print version[3]}'`
sudo -u git -H git branch -a
sudo -u git -H git checkout 7-0-stable
  1. 設定GitLab
cd /home/git/gitlab
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# 所有 localhost 改成要使用的domain. 
# /usr/bin/git 改成 /usr/local/bin/git
sudo -u git -H editor config/gitlab.yml

#調整檔案權限
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/
chmod -R u+rwX  tmp/pids/
chmod -R u+rwX  tmp/sockets/
sudo -u git -H mkdir public/uploads
chmod -R u+rwX  public/uploads
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
chmod -R u+rwX  public/uploads
sudo -u git -H mkdir public/uploads

#如果硬體記憶體規格超過2G,可將worker_processes改成3
#sudo -u git -H editor config/unicorn.rb

sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
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
  1. GitLab DB設計
sudo -u git cp config/database.yml.mysql config/database.yml

# 將production區塊的secure password值改成MySQL gitlab帳號設定的帳號、密碼
sudo -u git -H editor config/database.yml

sudo -u git -H chmod o-rwx config/database.yml
  1. 安裝Gems
cd /home/git/gitlab
sudo -u git -H bundle install --deployment --without development test postgres aws
  1. 初始化 Database
# 問啥就輸入yes以便建立Database
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
# 取得系統預設帳號、密碼 (所有新安裝都一樣,務必要更改)
Administrator account created:
    login.........admin@local.host
    password......5iveL!fe
  1. 設定開機自動啟動
cp lib/support/init.d/gitlab /etc/init.d/gitlab
chmod +x /etc/init.d/gitlab
update-rc.d gitlab defaults 21
  1. 設定Logtotate
cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
  1. 檢查GitLab應用程式設定狀態
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
  1. 啟動GitLab
service gitlab start

19.安裝、設定apache2

apt-get install nginx -y
cd /home/git/gitlab
cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

# 修改ServerName => 使用的Domain
vim /etc/nginx/sites-available/gitlab

# 如果沒有domain name,也一樣使用localhost,請記得刪除 /etc/nginx/sites-enabled/default
  1. 重新啟動 nginx
service nginx restart
  1. 進行初始化,避免發生可愛的502錯誤…..這邊會需要一點點的時間,請等待….
cd /home/git/gitlab
sudo -u git -H RAILS_ENV=production bundle exec rake assets:precompile
  1. 用網頁連線到http://使用的domain以上面取得的帳號密碼進行測試
    第一次讀取網頁可能會需要多一點時間,也有可能因為第20步驟所以這邊不會等太久
This entry was posted in Git, Nginx, Ubuntu. Bookmark the permalink.