去年(2015年)因为工作需要,需要自己部署一个Git的环境,这里将当时的GitLab的安装记录做一个备份。
注意,本文中所描述的GitLab的版本,是7.11.*,并不是目前最新版本,我其实并不推荐自建版本库,文件备份,安全性等各个方面要考虑太多。
所以没有什么意外,还是别自己折腾,这里特别推荐大家啊使用 @红薯 家git@OSC,我一直在用,只要红薯一直在做我就一直只用他的,就算他说收费我也用,除非他说管理太累了,不做了。中国人,当然用中国人自己的git,对不,OSC加油!
题外话,gogs这个项目也是相当的不错,安装部署比GitLab容易100倍,而且性能比GitLab要高,所以不爱折腾的同学可以考虑用这个。去年的时候本来是安装gogs的,但是因为当时某个特性支持不好,所以才换了GitLab。但装gogs,我只用了半个小时不到就完成了,装GitLab搞了我一天。看到Ror套装,我就烦。尤其是他多语言支持还麻烦,好多东西都是hardcode的。
重要的事情说三次:该文章中的GitLab只适用 7.11.*,最新版本尚未测试过。
基本环境准备
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 python-docutils pkg-config cmake libkrb5-dev
apt-get install -y git
添加git用户
sudo adduser --disabled-login --gecos 'GitLab' git
安装ruby
cd /tmp
wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz
tar -zxvf ruby-2.1.5.tar.gz
cd ruby-2.1.5
./configure --disable-install-rdoc
make && make install
gem install bundler --no-ri --no-rdoc
安装redis
apt-get install redis-server
cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | tee /etc/redis/redis.conf
echo 'unixsocket /var/run/redis/redis.sock' | tee -a /etc/redis/redis.conf
echo 'unixsocketperm 770' | tee -a /etc/redis/redis.conf
service redis-server restart
usermod -aG redis git
克隆gitlab代码(中文版)
sudo -u git -H git clone https://gitlab.com/larryli/gitlab.git
cd /home/git/gitlab
sudo -u git -H git checkout 7-11-zh
配置部分
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
修改配置
sudo -u git -H nano config/gitlab.yml
- host和port
- time_zone
- email_from
- 找到default_projects_features字段,issues、merge_requests、wiki为true,snippets为false,visibility_level为private
- GitLab Satellites一栏,修改为安装目录路径: satellites: path: /home/git/gitlab-satellites/ backup: path: "tmp/backups"
- GitLab Shell一栏,修改为安装目录路径: gitlab_shell: path: /home/git/gitlab-shell/ repos_path: /home/git/repositories/ hooks_path: /home/git/gitlab-shell/hooks/ upload_pack: true receive_pack: true
- git: 根据需要修改
chown -R git log/
chown -R git tmp/
chmod -R u+rwX,go-w log/
chmod -R u+rwX tmp/
sudo -u git -H mkdir /home/git/gitlab-satellites
chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites
chmod -R u+rwX tmp/pids/
chmod -R u+rwX tmp/sockets/
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 -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "admin@gscode.co"
sudo -u git -H git config --global core.autocrlf input
sudo -u git -H cp config/resque.yml.example config/resque.yml
更改数据库配置
sudo -u git cp config/database.yml.mysql config/database.yml
sudo -u git -H nano config/database.yml
sudo -u git -H chmod o-rwx config/database.yml
安装gems
apt-get install libmysqlclient-dev
gem install mysql2 -v '0.3.16'
sudo -u git -H bundle install --deployment --without development test postgres aws
再接下来安装gitlab shell:
apt-get install nodejs
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
sudo -u git -H nano /home/git/gitlab-shell/config.yml
把gitlab的url改成http://localhost/或者你的域名。
如果gitlab:shell:install
出错的话,可执行bundle exec rake gitlab:check RAILS_ENV=production
检查
在接下来初始化数据库:
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
启动服务脚本
启动脚本:
cp lib/support/init.d/gitlab /etc/init.d/gitlab
chmod +x /etc/init.d/gitlab
初始化日志:
cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
检查应用状态:
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
编译附件:
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
启动gitlab服务:
sudo service gitlab start
最后检查整体的状态
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
补充执行的命令
chmod -R ug+rwX,o-rwx /home/git/repositories/
chmod -R ug-s /home/git/repositories/
find /home/git/repositories/ -type d -print0 | sudo xargs -0 chmod g+s
出现gitlab-shell self-check failed错误
/home/git/gitlab/config/unicorn.rb:配置ruby提供的服务端口,ip /home/git/gitlab/config/gitlab.yml:配置gitlab服务的端口,ip /home/git/gitlab-shell/config.yml:配置gitlab-shell要调用的API接口
unicorn.rb:第40行改成
listen "127.0.0.1:8080", :tcp_nopush => true
gitlab.yml:第18行和第19行改成
host: 127.0.0.1
port: 8080
config.yml:第5行改成
gitlab_url: "http://127.0.0.1:8080/"
nginx配置
将www-data添加进git组
usermod -aG git www-data
upstream gitlab_ci {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen 80;
# server_name ;
server_tokens off;
access_log /var/log/vhost/default/main.log default;
error_log /var/log/vhost/default/error.log error;
# error_page 400 403 404 /40x.html;
# error_page 500 502 503 504 /50x.html;
limit_req zone=max burst=50;
root /home/git/gitlab/public;
location / {
## Serve static files from defined root folder.
## @gitlab_ci is a named location for the upstream fallback, see below.
try_files $uri $uri/index.html $uri.html @gitlab_ci;
}
location @gitlab_ci {
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Frame-Options SAMEORIGIN;
proxy_pass http://gitlab_ci;
}
# include /etc/tengine/deny_settings.conf;
# include /etc/tengine/php_settings.conf;
# include /etc/tengine/php_common_rewrite.conf;
}
最后,初始化管理员的账号密码:
login: root password: 5iveL!fe
修改发送邮箱
sudo -u git -H nano /home/git/gitlab/config/environments/production.rb
搜索:config.action_mailer.delivery_method=
,修改:sendmail
为:smtp
cd /home/git/gitlab/config/initializers sudo -u git -H cp smtp_settings.rb.sample smtp_settings.rb
然后把文件的里的内容修改正确了,再重启一下gitlab服务就行了:
sudo service gitlab restart
本文档只适合 GitLab 7.11