Ubuntu 安装GitLab服务器

Posted by Sunday on 2017-11-18

环境

操作系统:Ubuntu 16.04
应用服务:Gitlab 10.1.4-ce.0
防火墙状态:关闭

安装并配置必要的依赖关系

1
sudo apt-get install -y curl openssh-server ca-certificates

添加 GitLab 镜像源并安装

1
2
curl -sS http://packages.gitlab.com.cn/install/gitlab-ce/script.deb.sh | sudo bash
sudo apt-get install gitlab-ce

配置并启动 GitLab

通过浏览器访问上一步配置的域名

1
2
vim /etc/gitlab/gitlab.rb 
external_url 'http://git.sundayle.com'

注意端口占用:如80 8080被占用需要修改这两个

1
2
3
4
gitlab_workhorse['auth_backend'] = "http://localhost:8181"
unicorn['port'] = 8181
# nginx['listen_port'] = 81
external_url 'http://git.sundayle.com:81'

gitlab 检测 重置 重启

1
2
3
4
sudo gitlab-ctl reconfigure
gitlab-rake gitlab:check
gitlab-ctl restart
/var/log/gitlab/ 查看日志

第一次访问 GitLab,系统会重定向 url 到重置密码的页面,你需要输入初始化管理员账号的密码。 设置完成后,系统会重定向到登录界面,你就可以使用刚才输入的密码登录系统了。

系统默认的管理员账号为 root, 登录系统后,你可以修改管理员账号为自己喜欢的账号。

gitlab mail

1
2
3
4
5
6
7
8
9
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "sundayhk@qq.com"
gitlab_rails['smtp_password'] = "uzynkmhoockqbcgh"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = 'sundayhk@qq.com'

测试邮件发送功能
执行 gitlab-rails console 进入控制台,发送一封测试邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
gitlab-rails console 

### Notify.test_email('收件人邮箱', '邮件标题', '邮件正文').deliver_now

irb(main):001:0> Notify.test_email('zsp@xwx.cn','test','test_text').deliver_now

Notify#test_email: processed outbound mail in 197.3ms
Sent mail to zsp@xwx.cn (1824.6ms)
Date: Mon, 27 Nov 2017 18:20:08 +0800
From: GitLab <sundayhk@qq.com>
Reply-To: GitLab <noreply@git.sundayle.com>
To: zsp@xwx.cn
Message-ID: <5a1be6d8e0a75_127623ff905fdb0f45758e@demo18.mail>
Subject: test
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Auto-Submitted: auto-generated
X-Auto-Response-Suppress: All

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>test_text</p></body></html>

=> #<Mail::Message:70338453025500, Multipart: false, Headers: <Date: Mon, 27 Nov 2017 18:20:08 +0800>, <From: GitLab <sundayhk@qq.com>>, <Reply-To: GitLab <noreply@git.xwx.cn>>, <To: zsp@xwx.cn>, <Message-ID: <5a1be6d8e0a75_127623ff905fdb0f45758e@demo18.mail>>, <Subject: test>, <Mime-Version: 1.0>, <Content-Type: text/html; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <Auto-Submitted: auto-generated>, <X-Auto-Response-Suppress: All>>

gitlab 替换 内置服务

1. gitlab-postgresql 替换 mysql

1
2
3
4
5
6
7
8
9
10
postgresql['enable'] = false

gitlab_rails['db_adapter'] = "mysql2"
gitlab_rails['db_encoding'] = "utf8"
gitlab_rails['db_database'] = "gitlab"
gitlab_rails['db_username'] = "gitlab"
gitlab_rails['db_password'] = "star"
gitlab_rails['db_host'] = "localhost"
gitlab_rails['db_port'] = "3306"
gitlab_rails['db_socket'] = "/tmp/mysql.sock"

创建数据库及授权

1
2
3
mysql> grant all privileges on *.* to gitlab@"localhost" identified by "star";
mysql> flush privileges;
mysql> CREATE DATABASE IF NOT EXISTS `gitlab` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_general_ci`;

bundle禁用postgres

1
2
3
4
5
vim /opt/gitlab/embedded/service/gitlab-rails/.bundle/config 

BUNDLE_WITHOUT: "development:test:mysql"
修改成
BUNDLE_WITHOUT: "development:test:postgres"

bundle 安装mysql2

1
2
cd /opt/gitlab/embedded/service/gitlab-rails
/opt/gitlab/embedded/bin/bundle install
1
2
3
gitlab-ctl reconfigure
gitlab-rakegitlab:check
gitlab-ctl restart

2. gitlab-nginx 替换 现有nginx

1
2
nginx['enable'] = false
web_server['external_users'] = ['www']

www 为nginx 用户

3. gitlab-redis 替换 现有redis

1
2
3
gitlab_rails['redis_host'] = "localhost"
gitlab_rails['redis_port'] = 6379
gitlab_rails['redis_socket'] = "/tmp/redis.socket"

gitlab 备份

Gitlab 创建备份

1
gitlab-rake gitlab:backup:create

使用以上命令会在/var/opt/gitlab/backups目录下创建一个名称类似为1511172598_2017_11_20_10.1.4_gitlab_backup.tar的压缩包, 这个压缩包就是Gitlab整个的完整部分,

Gitlab 自动备份

1
00 01 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

Gitlab 恢复

1
2
3
4
5
6
7
# 停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-rake gitlab:backup:restore BACKUP=1511172598_2017_11_20_10.1.4

# 启动Gitlab
sudo gitlab-ctl start

错误提示

错误1 tail /var/log/gitlab/nginx/error.log

1
2
client: 192.168.10.251, server: git.sundayle.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/opt/gitlab/gitlab-workhorse/socket:/", host: "git.sundayle.com"
2017/11/17 17:26:53 [crit] 154640#0: *540989 connect() to unix:/var/opt/gitlab/gitlab-workhorse/socket failed (13: Permission denied) while connecting to upstream, client: 192.168.10.251, server: git.sundayle.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/var/opt/gitlab/gitlab-workhorse/socket:/favicon.ico", host: "git.sundayle.com", referrer: "http://git.sundayle.com/"

解决:

1
2
3
web_server['external_users'] = [ ]
修改成(nginx用户)
web_server['external_users'] = ['www']

错误2 gitlab-rake gitlab:backup:restore

1
2
3
4
Before restoring the database we recommend removing all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.

解决:

1
mysql > GRANT PROXY ON ''@'' TO 'gitlab'@'localhost' WITH GRANT OPTION

https://segmentfault.com/a/1190000002439923
https://lessmind.github.io/2016/11/18/gitlab-ce-omnibus-package-with-mysql/