ubuntu+nginx+git+gitlab做爲代碼版本控制的環境部署

====git簡單介紹:版本控制====html

版本控制系統基本概念python

repository  存放全部文件及其歷史信息mysql

checkout  取出或切換到指定版本的文件nginx

version  記錄標識一個版本(編號或其餘代碼)git

tag    記錄標識一個主要版本(1.0)github


本地化版本控制系統:RCSweb

集中化版本控制系統:CVS,SVNredis

優勢:適合多人團隊協做開發,代碼集中化管理,全部開發人員代碼提交到一臺CVS服務器上集中化管理 sql

缺點: 單點故障(服務器數據庫掛掉數據丟失);必須連網工做,沒法單機本地工做shell

分佈式版本控制系統:

除了集中式版本的優勢,還能解決單點問題及能夠離線工做,每一個計算機都是一個完整倉庫。

spacer.gif

wKiom1RYkzugKpi3AADehG0jziI535.jpg

wKioL1RYk5uhSa5FAAFVPEG6gbY786.jpg


====git安裝====

最開始使用的是centos6.2系統安裝,按照官網安裝過程各類由於包版本不兼容問題刨下不少坑,各類填坑各類補丁浪費時間,快接近真相時最後仍是失敗。無奈下改用ubuntu系統,感謝Key. Posted博文的記錄,但仍是遇到了一些坑,若是按照如下筆記中一步步走下去基本能夠保證一帆風順。


環境說明:

ubuntu系統

mysql數據庫

nginx做爲web服務器

redis 緩存

git     git-lab    gitlab-shell


安裝筆記

爲了解除依賴包帶來的麻煩,建議同志們將apt源更新成中科大或者網易的源。
中科大的鏡像源站點:

http://mirrors.ustc.edu.cn/

網易的鏡像源站點:

http://mirrors.163.com/


如下操做採用root賬號或具備sudo權限的賬號執行

1.更新系統package

apt-get update

apt-get upgrade

2.安裝必須的依賴包(不然接下去的安裝過程當中你會遇到不少坑)

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 vim ruby-dev


ubuntu系統安裝好後默認自帶的是python 2.7版本,同志們保持一致吧。若是不是2.7版本,嘗試鏡像源中找下:

apt-get install python2.7

安裝郵件發送支持(默認):

apt-get install postfix

安裝Ruby 1.9.3版本 :

apt-get install ruby


安裝bundler ,也可使用taobao鏡像來縮短下載時間:

gem sources --remove http://rubygems.org

gem sources -a http://ruby.taobao.org/

gem install bundler


3. 添加git用戶

adduser  --gecos 'GitLab' git

visudo       #更改sudo權限,賦予git以root身份去執行命令,添加下面這行(這裏我爲了後續方便將git的sudo權限放的很大)

git ALL=(ALL:ALL) ALL


4. 安裝gitlab-shell

#login as git

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

vi config.yml                         編輯config.yml配置文件:替換gitlab_url爲本身的內部域名如git.abc.com

./bin/install


5.數據庫安裝

sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

建立gitlab用戶並設置權限:

mysql -u root -p

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, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';


6. 安裝gitlab主程序

su - git            #切換至git賬號下執行cd /home/git/
# Clone GitLab repository
git clone https://github.com/gitlabhq/gitlabhq.git gitlab
cd /home/git/gitlab

# Checkout to stable release
git checkout 5-2-stable

# Make sure GitLab can write to the log/ and tmp/ directories
# 修改帳戶權限(注意保持/home/git目錄及子目錄屬主、組爲git用戶)
chmod -R u+rwX log/
chmod -R u+rwX tmp/

# Create directory for satellites

mkdir /home/git/gitlab-satellites

# Create directories for sockets/pids and make sure GitLab can write to them

mkdir tmp/pids

mkdir tmp/sockets

# Create public/uploads directory otherwise backup will fail

mkdir public/uploads

chmod -R u+rwX  tmp/pids

chmod -R u+rwX  tmp/sockets

chmod -R u+rwX  public/uploads


# Copy the example Puma config

cp config/puma.rb.example config/puma.rb       # 該配置文件默認便可


# Configure Git global settings for git user, useful when editing via web

# Edit user.email according to what is set in gitlab.yml

git config --global user.name "GitLab"

git config --global user.email "gitlab@localhost"


cp config/database.yml.mysql config/database.yml

vi config/database.yml   # 修改數據庫帳號密碼, 剛纔添加過gitlab這個數據庫用戶 直接修改爲該帳號便可

cp config/gitlab.yml.example  config/gitlab.yml 

vi config/gitlab.yml    #修改host:localhost爲本身的域名 git.olymtech.com


7. 安裝 Gems

cd /home/git/gitlab

sudo gem install charlock_holmes --version '0.6.9.4'

#修改Bundle源地址爲taobao, 首行改爲 source 'http://ruby.taobao.org/'

vi Gemfile

#使用mysql數據庫,排除 postgres等

cd /home/git/gitlab

bundle install --deployment --without development test postgres

注:這裏在安裝過程當中發現有個坑,不管是ruby官網仍是淘寶都找不到modernizr-2.6.2版本。

錯誤以下:

git@gitserver:~/gitlab$ bundle install --deployment --without development test postgres 
Fetching source index from http://ruby.taobao.org/ 
Could not find modernizr-2.6.2 in any of the sources

官網跟淘寶 ruby源 都沒了這個包 解決辦法是先手動去下載 而後安裝


git@gitserver:~/gitlab$ wget http://rubygems.org/gems/modernizr-2.6.2.gem

git@gitserver:~/gitlab$ sudo  gem install modernizr

sudo  gem install modernizr

 而後修改 gitlab源碼包裏面的 Gemfile 跟 Gemfile.lock文件 ,把裏面 modernizr包名換成 modernizr-rails 

版本所有換成 2.7.1 而後再運行 bundle install --deployment --without development test postgres 便可


8.初始化數據庫並啓用高級功能

bundle exec rake gitlab:setup RAILS_ENV=production

整個gitlab安裝完成後默認建立的賬號密碼:

Administrator account created: 

login.........admin@local.host 

password......5iveL!fe


9.檢測一下程序狀態

bundle exec rake gitlab:check RAILS_ENV=production

檢測結果:

Checking Environment ...

Git configured for git user? ... yes

Has python2? ... yes

python2 is supported version? ... yes

Checking Environment ... Finished

Checking GitLab Shell ...

GitLab Shell version >= 1.4.0 ? ... OK (1.4.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

post-receive hook up-to-date? ... yes

post-receive hooks in repos are links: ...

Administrator / olymtechtest ... repository is empty

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes

Checking Sidekiq ... Finished

Checking GitLab ...

Database config exists? ... yes

Database is SQLite ... no

All migrations up? ... yes

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 satellites? ...

Administrator / olymtechtest ... can't create, repository is empty

Redis version >= 2.0.0? ... yes

Your git bin path is "/usr/bin/git"

Git version >= 1.7.10 ? ... yes (1.9.1)

Checking GitLab ... Finished

10.安裝init腳本(以root身份,不然可能沒權限)

wget https://raw.github.com/gitlabhq/gitlabhq/5-2-stable/lib/support/init.d/gitlab

mv gitlab /etc/init.d/

service gitlab start             啓動gitlab服務。


至此:上面的git+gitlab+mysql+redis 環境安裝完成


====安裝nginx做爲web服務器====

nginx安裝(以root身份):

apt-get install nginx


增長nginx配置文件:

wget https://raw.github.com/gitlabhq/gitlabhq/5-2-stable/lib/support/nginx/gitlab -P /tmp

mv /tmp/gitlab  /etc/nginx/sites-enabled/

vi /etc/nginx/sites-enabled/gitlab  編輯配置文件,修改配置文件中直接監聽80端口, 域名爲自定義的訪問域名便可:

server {

  listen 80;

  server_name git.abc.com;

.....

}

需注意的是nginx主配置文件/etc/nginx/nginx.conf 中開啓include /etc/nginx/sites-enabled/*;

使其子配置文件生效。


啓動服務:

service nginx restart


接下來手動hosts指向下,期待已久的站點頁面就能夠訪問了:

wKioL1RYmH3wn2N7AAFd1hOP8z4805.jpg


參考文檔:

https://www.uloli.com/p/6sh26/

http://www.showerlee.com/archives/1285

http://www.cnblogs.com/scue/p/3663546.html

http://www.tuicool.com/articles/m22AVv


遇到的坑一:

git@gitserver:~/gitlab$ sudo gem install charlock_holmes --version '0.6.9.4' 
Building native extensions. This could take a while... 
ERROR: Error installing charlock_holmes: 
ERROR: Failed to build gem native extension. 

/usr/bin/ruby1.9.1 extconf.rb 
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError) 
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' 
from extconf.rb:1:in `<main>'

解決方案:

sudo apt-get install ruby-dev


遇到的坑二:


一切安裝完成後,發現客戶端經過http方式push能夠,但每次都須要密碼。

採用gitlab 的ssh-key認證方式,本地客戶端生成一個ssh key後將公鑰上傳至gitlab站點頁面,發現ssh key並未成功,git push時每次都要輸入密碼。

從日誌中找不出有效信息,百度了好久一直沒有找到緣由,覈對本地客戶端的key,上傳到gitlab站點的key,和服務端生成的key對比發現,問題根本緣由在於上傳到gitlab站點上的key和服務器生成的key不一致。

爲何麼不一致,谷歌上找了好久,gitlab與gitlab-shell網上查的緣由有配置文件中url不一致,用戶或權限不對,.ssh目錄及authorized_keys權限必須分別爲700和600。 這些反覆查看配置都是正確的,無解。

嘗試從新生成key:

git@gitserver:~/gitlab$ rake gitlab:shell:setup RAILS_ENV=production

重啓gitlab:

root@gitserver:/home/git/gitlab# /etc/init.d/gitlab restart


git push測試仍是須要輸入密碼,這一大坑直到次日在一國外論壇上找到某一人:just forgot "RAILS_ENV=production"。提醒了我下,解決方案:

把RAILS_ENV=production直接加入到/etc/profile全局環境變量中,source /etc/profile 使其生效。

root@gitserver:~# echo "RAILS_ENV=production" >>/etc/profile

root@gitserver:~#source /etc/profile 

把git用戶下的原來的authorized_keys文件刪除。再將gitlab服務中止再啓動。

root@gitserver:~#rm  /home/git/.ssh/authorized_keys

root@gitserver:~# /etc/init.d/gitlab stop

root@gitserver:~# /etc/init.d/gitlab start


這時客戶端測試刪除原來生成的key,從新生成並上傳到gitlab站點,此次再push測試認證經過。

這時查看服務端生成的authorized_keys內容跟上傳的公鑰key是徹底一致的。

相關文章
相關標籤/搜索