8小时外实践系列(六) - 在阿里云服务器上搭建GitLab(草稿)

此文作废, 更简单更快的安装方法请参考
https://www.cnblogs.com/pliybird/p/6026248.html


目录


个人感受: 个人项目本地托管还是用SVN吧. GitLab在管理多个项目上可能有优势, 但是GitLab搭建太复杂不划算. SVN在CentOS上可谓是一键安装
yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql

一 准备工作

GitLab目前(2016-06-25)支持的系统 64位

Ubuntu
Debian
CentOS
以下三种系统参考CentOS的安装包和说明:
Red Hat Enterprise Linux
Scientific Linux
Oracle Linux

1 ssh登录到云服务器的终端

选择CentOS 7.0 64bit系统(因为GitLab目前不支持CentOS6.5)
确认云服务器启动以后, ssh链接到云服务器,
ssh [email protected]
输入云服务器root用户的初始密码, 回车, 进入云服务器的CentOS系统命令行环境.

2 安装必要软件

在CentOS系统命令行环境输入逐个以下命令, 命令的具体意义见文末的附录:
sudo yum install curl policycoreutils openssh-server openssh-clients

3 配置系统服务

sudo systemctl enable sshd
sudo systemctl start sshd
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
报错如下:

Job for postfix.service failed. See 'systemctl status postfix.service' and 'journalctl -xn' for details.

根据提示, 使用systemctl status postfix.service -l查看错误原因, 如下:

6月 25 11:05:41 主机名 postfix/sendmail[1143]: fatal: parameter inet_interfaces: no local interface found for ::1

解决方案:
打开postfix的配置文件
vim /etc/postfix/main.cf
发现116行左右的配置为:

inet_interfaces = localhost
inet_protocols = all

改成以下内容并保存:

inet_interfaces = localhost # 只能接受来自本机的邮件
inet_protocols = ipv4 # 拒绝ipv6的本机地址::1

再次执行
sudo systemctl start postfix
无报错, 成功

sudo firewall-cmd --permanent --add-service=http
报错如下:

FirewallD is not running

解决方案:
修改配置文件 /etc/sysctl.conf, 在最后添加以下内容后保存:

net.ipv4.ip_forward=1

为了使配置生效, 输入:
sysctl -p
再次启动firewalld服务, 输入:
systemctl start firewalld
检查firewalld服务状态, 输入:
systemctl status firewalld -l
提示以下信息, 注意到Active: active (running)则成功:

firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled)
Active: active (running) since 六 2016-06-25 12:58:15 CST; 4s ago
Main PID: 27738 (firewalld)
CGroup: /system.slice/firewalld.service
       └─27738 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
       6月 25 12:58:15 iZ28blmhjjkZ systemd[1]: Started firewalld - dynamic firewall daemon.

再次输入
sudo firewall-cmd --permanent --add-service=http
提示以下信息, 则成功:

success

sudo systemctl reload firewalld

4 安装 Ruby 语言2.1.x(MRI)版本

Ruby MRI - Matz’s Ruby Interpreter (also called CRuby)

GitLab依赖Ruby, 而且严格要求版本为 2.1.x
Ruby 2.1.10 是官方声明的2.1系列的最后一个版本

系统软件源的ruby版本是 2.0.0 不是我们想要的
想安装制定版本的Ruby需要通过RVM(Ruby Version Manager)

安装RVM

通过gpg工具安装, 关于gpg的详细介绍见附录. 在命令行输入:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
提示如下, 则添加密匙成功

gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <[email protected]>”已导入
gpg: 没有找到任何绝对信任的密钥
gpg: 合计被处理的数量:1

如果提示如下:

gpgkeys: HTTP fetch error 6: Could not resolve host: keys.gnupg.net; Name or service not known

则用指定用80端口
gpg --keyserver hkp://keys.gnupg.net:80 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
然后再
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
就可以了

然后, 获得安装的.sh脚本, 保存到当前目录下的rvm.sh文件
curl -sSL https://get.rvm.io>rvm.sh
赋予可执行权限, 并执行rvm.sh文件
chmod 744 rvm.sh
bash ./rvm.sh
提示如下

Downloading https://github.com/rvm/rvm/archive/master.tar.gz
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

# Administrator,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: https://rvm.io/help and https://twitter.com/rvm_io

翻译一下就是还要做两件事:
1. 添加当前系统用户到系统用户组rvm, 注销后重新登录
2. source /etc/profile.d/rvm.sh

安装RVM第一步 - 添加当前系统用户到系统用户组rvm

检查用户组列表文件是否有用户组rvm
cat /etc/group
确实有, 添加root到rvm组
usermod -a -G rvm root
-a 代表 append, 也就是将用户添加到新用户组中而不必离开原有的其他用户组。不过需要与 -G 选项配合使用.

查看root用户所在的组, 命令行输入:
groups
提示如下:

root

发现仍然只有root, 并没有加入到rvm用户组, 不要担心, 重新登录一下就好了.
重新登录后, 命令行输入:
groups
提示如下:

root rvm

安装RVM第二步 - source /etc/profile.d/rvm.sh

命令行输入:
source /etc/profile.d/rvm.sh
无报错信息, 继续输入:
rvm
出现rvm使用帮助, 大功告成

用RVM安装Ruby 2.1.10

此部分内容转自RVM 实用指南 · Ruby China
列出已知版本
rvm list known
稍等5秒左右, 提示如下. 哇塞, 有这么多版本的Ruby:

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.5]
[ruby-]2.3[.1]
[ruby-]2.3-head
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.25]
jruby[-9.1.1.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx[-2.5.8]
rbx-head

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# GoRuby
goruby

# Topaz
topaz

# MagLev
maglev[-head]
maglev-1.0.0

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head

安装一个 Ruby 版本
rvm install 2.1.10 --disable-binary
一步一步安装顺利, 直到出现以下报错:

curl: (6) Could not resolve host: rubygems.org; 未知的名称或服务
There was an error while trying to resolve rubygems version for 'latest'. 
Halting the installation.

我是隔一会儿多试几次就好了

切换 Ruby 版本
rvm use 2.1.10
检查ruby是否安装
ruby -v

ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-linux]

ruby 2.1.10 安装成功!


如果想设置为默认版本,这样一来以后新打开的控制台默认的 Ruby 就是这个版本
rvm use 2.1.10 --default

查询已经安装的ruby
rvm list

卸载一个已安装版本
rvm remove 1.8.7

二 GitLab 安装

官网推荐使用 Omnibus GitLab 安装, Omnibus GitLab 已经集成了GitLab, 数据库, Redis, Nigix.

方案一 - 科学上网安装Omnibus GitLab

不一定成功, 详见附录 - 科学上网安装GitLab

方案二 - 使用清华大学镜像安装Omnibus GitLab(亲测可用)

以下内容转自https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

新建 /etc/yum.repos.d/gitlab-ce.repo,内容为

[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

再执行
sudo yum makecache
sudo yum install gitlab-ce

安装完以后 /var/opt/gitlab/ 目录结构

/var/opt/gitlab/
├── backups
├── git-data
│   └── repositories
│       └── root
├── gitlab-ci
│   └── builds
├── gitlab-rails
│   ├── etc
│   ├── shared
│   │   ├── artifacts
│   │   ├── lfs-objects
│   │   └── pages
│   ├── sockets
│   ├── tmp
│   ├── upgrade-status
│   ├── uploads
│   └── working
├── gitlab-shell
├── gitlab-workhorse
├── logrotate
│   └── logrotate.d
├── nginx
│   ├── client_body_temp
│   ├── conf
│   ├── fastcgi_temp
│   ├── logs -> /var/log/gitlab/nginx
│   ├── proxy_cache
│   ├── proxy_temp
│   ├── scgi_temp
│   └── uwsgi_temp
├── postgresql
│   └── data
│       ├── base
│       │   ├── 1
│       │   ├── 12918
│       │   ├── 12923
│       │   └── 16385
│       ├── global
│       ├── pg_clog
│       ├── pg_multixact
│       │   ├── members
│       │   └── offsets
│       ├── pg_notify
│       ├── pg_serial
│       ├── pg_snapshots
│       ├── pg_stat_tmp
│       ├── pg_subtrans
│       ├── pg_tblspc
│       ├── pg_twophase
│       └── pg_xlog
│           └── archive_status
└── redis

添加git用户和用户组

这一步默认安装gitlab-ce的时候自动帮我们做了.

omnibus-gitLab uses the user name “git” for Git gitlab-shell login, ownership of the Git data itself, and SSH URL generation on the web interface. Similarly, git group is used for group ownership of the Git data.

We do not recommend changing the user/group of an existing installation

查看用户
cat /etc/passwd

查看组
cat /etc/group

发现已经有 git 用户和 git 用户组了.

切换到git用户
sudo su git

备注:
安装gitlab-ce时, 通过以下命令创建用户
sudo adduser --disabled-login --gecos 'GitLab' git
–disabled-login 选项创建了gitlab账户但是并没有为之设立密码,所以是无法使用密码登陆的。
你应该在gitlab的网页上面把自己的公钥添加上去。

配置gitlab-shell

GitLab Shell - handles git commands for GitLab. GitLab Shell is not a Unix shell nor a replacement for Bash.

gitlab-shell处理用户提交的 git push/pull 命令的四种 cases
git pull over ssh -> gitlab-shell -> API call to gitlab-rails (Authorization) -> accept or decline -> execute git command
git pull over http -> gitlab-rails (Authorization) -> accept or decline -> execute git command
git push over ssh -> gitlab-shell (git command is not executed yet) -> execute git command -> gitlab-shell pre-receive hook -> API call to gitlab-rails (authorization) -> accept or decline push
git push over http -> gitlab-rails (git command is not executed yet) -> execute git command -> gitlab-shell pre-receive hook -> API call to gitlab-rails (authorization) -> accept or decline push

安装gitlab-ce的时候自动帮我们安装了gitlab-shell, 安装位置是 /var/opt/gitlab/gitlab-shell , 所以我们只需要配置gitlab-shell就好了
cd /var/opt/gitlab/gitlab-shell

vim config.yml

三 GitLab配置

/etc/gitlab/ 目录结构:

/etc/gitlab/
├── gitlab.rb
├── gitlab-secrets.json
└── ssl
    └── trusted-certs

gitLab基本配置集中在 /etc/gitlab/gitlab.rb 文件, 每个参数的作用和配置请参考官网的配置说明.

先停止 GitLab 服务
sudo gitlab-ctl stop

编辑器打开 /etc/gitlab/gitlab.rb
vim /etc/gitlab/gitlab.rb

进行任何改动后, 保存退出, 在命令行逐个输入以下命令使配置生效

启动数据库, 不然 reconfigure 报错
sudo gitlab-ctl restart postgresql

sudo gitlab-ctl reconfigure

sudo gitlab-ctl start

配置服务机地址

用你的阿里云主机公网地址就可以了

external_url 'http://xxx.xxx.xxx.xxx'

配置数据库

GitLab支持两种数据库

PostgreSQL
MySQL

GitLab自带PostgreSQL数据库位于 /var/opt/gitlab/postgresql/ 目录下, 如果需要使用自定义的数据库参考官网的这篇文章.

重启数据库
gitlab-ctl restart postgresql

数据库配置地址 /var/opt/gitlab/postgresql/data/postgresql.conf
修改后需要重启
vim postgresql.conf

# 修改监听地址为ip,*表示全部IP都可以进入数据库
(有账号就需要账号验证,无账号就可以直接连接进来)
listen_addresses = '*'    
port = 5432        注释# 监听端口
max_connections = 200      # (change requires restart)

连接数据库的配置
vim pg_hba.conf

host all all 192.168.1.0/24 trust 添加这行,192.168.1.0/24这个段的ip地址不需要密码可以连接

在维护 PostgreSQL 库时,有两个配置文件修改的情况比较多,第一是认证文件 pg_hba.conf,另一个是配置文件 postgresql.conf。

其中 pg_hba.conf 的更改更频繁些,因为业务服务器经常出现调整,或增加应用服务器,此时需要增加 pg_hba.conf 的 IP 签权信息,因为目前我们的生产库对 IP 实行严格控制,只有指定的 IP 权限才被开放,下面是一个生产库的 pg_hba.conf 文件。

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host postgres all 0.0.0.0/0 reject
host template0 all 0.0.0.0/0 reject
host template1 all 0.0.0.0/0 reject
#Aplication Ip list
host all all 180.166.148.57/32 md5 表示仅仅允许这个IP的外网地址可以连接到数据库
# "local" is for Unix domain socket connections only
local all all peer map=gitlab

前三行的认证策略为 reject ,表示不允许任何远程的连接连接系统库,之后才是应用服务器的IP 列表,没列入文件的 IP 将不允许连接,这就是常见的 “no pg_hba.conf entry” 错误。

更多的时候,有时不小心在配置文件 pg_hba.conf 增加了非法字符,也不会报错,但会带来问题。

配置Web容器

GitLab自带Web容器 - ngnix 位于 /var/opt/gitlab/ngnix/ 目录下, 如果需要使用自定义的数据库参考官网的这篇文章.

修改NGINX监听端口

By default NGINX will listen on the port specified in external_url or implicitly use the right port (80 for HTTP, 443 for HTTPS). If you are running GitLab behind a reverse proxy, you may want to override the listen port to something else. For example, to use port 8080

nginx['listen_port'] = xxxx

可选 - 配置SMTP发送邮件配置,使用163邮箱需要登录网页版, 在设置里开通SMTP服务才行

###163 exmail
#Sending application email via SMTP#
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 25 
gitlab_rails['smtp_user_name'] = "发件箱@163.com"
gitlab_rails['smtp_password'] = "发件箱账号"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = :login
gitlab_rails['smtp_enable_starttls_auto'] = true
#
###修改gitlab配置的发信人
gitlab_rails['gitlab_email_from'] = "发件箱@163.com"
user["git_user_email"] = "发件箱@163.com"

四 GitLab启动停止

sudo gitlab-ctl start
sudo gitlab-ctl stop

五 GitLab运维

查看ngnix服务器日志

/var/opt/gitlab/nginx/logs/

查看gitlab-shell日志

/var/log/gitlab/gitlab-shell/gitlab-shell.log

附录

附录A - 术语

curl - linux下发送http GET和POST请求的工具

postfix - 一种电子邮件服务器邮件服务器,它是由任职于IBM沃森研究中心(T.J. Watson Research Center)的荷兰籍研究员Wietse Venema为了改良sendmail邮件服务器而产生的。最早在1990年代晚期出现,是一个开放源代码的软件

systemctl - 使用linux的同学对service和chkconfig两个命令都不陌生,其重要性不言而喻,为了简化操作,通过systemctl一个命令就可以实现两个命令的功能.
用法是
systemctl 动作 服务名.service
支持该命令的Linux发行版有CentOS 7.

Firewalld - the new userland interface in RHEL 7(CentOS 7). It replaces the iptables interface and connects to the netfilter kernel code. It mainly improves the security rules management by allowing configuration changes without stopping the current connections.

gpg - RSA算法的一个实现. RSA算法是最通用最安全的加密方法. 目前被破解的最长RSA密钥是768个二进制位。也就是说,长度超过768位的密钥,还无法破解(至少没人公开宣布)。因此可以认为,2048位的密钥极其安全. gpg默认使用2048位的密匙. gpg的详细介绍请看http://www.ruanyifeng.com/blog/2013/07/gpg.html

Ruby gem - Gem是 Ruby 语言的包管理工具, 类似于Maven仓库是 Java 语言的包管理工具

Ruby gemset - gemset 可以理解为是一个独立的 Gem 环境, 可以包含一些 Gem 包. gemset依赖于某一个Ruby版本, 也就是说 Ruby 2.1.10 的gemset在 Ruby 2.2 中并不存在.

SOCKS5代理 - SOCKS5 是一个代理协议,它在使用TCP/IP协议通讯的前端机器和服务器机器之间扮演一个中介角色,使得内部网中的前端机器变得能够访问Internet网中的服务器,或者使通讯更加安全。SOCKS5 服务器通过将前端发来的请求转发给真正的目标服务器, 模拟了一个前端的行为。在这里,前端和SOCKS5之间也是通过TCP/IP协议进行通讯,前端将原本要发送给真正服务器的请求发送给SOCKS5服务器,然后SOCKS5服务器将请求转发给真正的服务器。

附录B - GitLab支持的并发数

硬件对并发的影响如表所示:

CPU数 内存(GB) 用户数
1 2 100
2 500
4 1000
4 8 2000
16 4000
8 5000

由表可知: 单人用的本地GitLab可以用VirtualBox开一个虚拟机, CPU分1个, 内存分2GB.
公司开发小组用的可以用物理机, CPU分2个, 内存分4GB.

附录C - 科学上网安装GitLab

用curl工具下载GitLab官方提供的安装脚本script.rpm.sh到当前目录
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh>script.rpm.sh
执行脚本
chmod 744 script.rpm.sh
bash script.rpm.sh
报错如下:

https://packages.gitlab.com/gitlab/gitlab-ce/el/7/x86_64/repodata/repomd.xml: [Errno 12] Timeout on https://packages-gitlab-com.s3.amazonaws.com/7/8/el/7/x86_64/repodata/ee01a5a96e4998a0779196d25a4bb4bbfe86fcf569ee8b124bda109ad30f1fdc-repomd.xml?AWSAccessKeyId=AKIAJ74R7IHMTQVGFCEA&Signature=VXOyX8hwwoaiKAs4imo4yPchHx4=&Expires=1466862919: (28, 'Connection timed out after 3772 milliseconds')
正在尝试其它镜像。

亚马逊的s3服务器被墙了…

可以使用 proxychains 科学上网, 参考http://www.5yun.org/7335.html

先还原yum源, 删掉gitlab-ce源
ls -l /etc/yum.repos.d/

总用量 32
-rw-r--r--. 1 root root 2573 11月 21 2014 CentOS-Base.repo
-rw-r--r--. 1 root root 1664 8月  23 2014 CentOS-Base.repo.rpmnew
-rw-r--r--. 1 root root  649 8月  23 2014 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 8月  23 2014 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 1331 8月  23 2014 CentOS-Sources.repo
-rw-r--r--. 1 root root  156 8月  23 2014 CentOS-Vault.repo
-rw-r--r--. 1 root root 1084 11月 21 2014 epel.repo
-rw-r--r--  1 root root  531 6月  25 21:46 gitlab_gitlab-ce.repo

mv /etc/yum.repos.d/gitlab_gitlab-ce.repo /etc/yum.repos.d/gitlab_gitlab-ce.repo.bak

依次执行以下命令

yum  install  gcc
yum install -y git
mkdir git_clone
cd git_clone
git clone https://github.com/haad/proxychains
cd proxychains
./configure --sysconfdir=/usr/local/etc
make && make install

使用方法:
proxychains4 任何命令

proxychains的配置默认文件在 [你执行git-clone命令时的目录]/proxychains/src/proxychains.conf下,需要将它拷贝到 /etc/proxychains.conf 这个路径下才能生效;
请自行搜索可用的代理, 写入 /etc/proxychains.conf

还原gitlab-ce源
mv /etc/yum.repos.d/gitlab_gitlab-ce.repo.bak /etc/yum.repos.d/gitlab_gitlab-ce.repo
代理安装GitLab:
proxychains4 yum install gitlab-ce

然而我找的socks5代理全都不能用.

这时, 我注意到一行小字

If you are located in China, try using https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

注意国情, 有官方机构提供镜像的话不要盲目科学上网, 尝试各种代理还不行简直在浪费生命

猜你喜欢

转载自blog.csdn.net/caib1109/article/details/51756246