阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 7.x上GitLab搭建详细教程

138次阅读
没有评论

共计 7486 个字符,预计需要花费 19 分钟才能阅读完成。

目录

  • 知识要求
  • 搭建感想
  • 搭建过程
  • 参考

知识要求:

  • nginx基础知识

搭建感想

注:以下是我搭建 gitlab 时的思考,需要 nginx 的基础知识,Docker的基础知识才容易理解,与下面的搭建过程是独立的,不感兴趣可直接略过。

其实 gitlab 已经搭建并用了一年多了,现在所有的项目管理都通过 gitlab 完成。但是一直以来都有 2 个问题:

  • 80端口被系统的 nginx 占用了,所以只能监听非 80 端口;
  • 443端口也被系统的 nginx 占用,所以也一直没增加对 https 的支持;

最近正在尝试对所有已有的服务 Docker 化,一方面想让 gitlab 的搭建更简单些,另一方面也把这两个问题都处理掉。

于是就做了两个 Docker 容器: nginxgitlab,相当于nginxgitlab运行在局域网的不同主机,所以端口上没冲突。nginx是对外的服务器,它做一层反向代理到 gitlab 就能让 gitlab 提供对外的服务。
然而。。。这个做法却带来了一个新问题:gitlab需要的是 22,80,443 端口,80443 通过反向代理解决了,22却没办法解决。因为正常来讲,宿主机的 SSH 肯定也在使用,所以 gitlabSSH监听端口映射到宿主机会有冲突。

当然了,解决办法还是有的,不过非常繁琐。我们做 Docker 的目的不就是为了降低布署难度吗?如果使用 Docker 的配置比在宿主机上还繁琐,那使用起来就没太大意义了。
于是 gitlab 就没有放在 Docker 中,而是直接搭在宿主机上。

搭建过程

安装

gitlab的安装是很简单的, 先下载安装包:

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.2.5-ce.0.el7.x86_64.rpm

安装:

rpm -Uvh gitlab-ce-10.2.5-ce.0.el7.x86_64.rpm

配置

gitlab内置了 nginx 的服务,所以默认会占用 80443端口。一般来说,我们做 WEB 开发,服务器上早就安装了 nginx 或者 apache,把80443端口给占了,所以需要做些修改防止冲突。

简单的修改端口是不可行的,比如 80 改成 85, 当查看gitlab 项目时,下图中的项目地址会变成 http://git.papamk.com:85/papamk/groupbill 这样,看着就不舒服。启用 https 则在使用过程中则会出现其他的问题。这里不一一论述,直接说明正确的配置方法。

CentOS 7.x 上 GitLab 搭建详细教程

具体步骤:

1. 首先确保你的服务器已经有运行 nginx,并且该nginx 监听宿主机的 80443端口。(如果你原来使用的 apache,请通过nginx 反向代理到 apache,这样apache 原来的服务仍然可用)。

2. 编辑/etc/gitlab/gitlab.rb:

# 编辑对外的域名(gitlab.papamk.com 请添加 A 记录指向本服务器的公网 IP):
external_url 'http://gitlab.papamk.com/'

# 禁用 `gitlab` 内置的 `nginx`:
nginx['enable'] = false
# 修改成与 nginx 运行时的用户一致
web_server['external_users'] = ['www']

修改监听方式和监听地址 (如果nginxgitlab都在宿主机上,不用改也行;如果 nginxdocker中,则需要修改)

gitlab_workhorse['listen_network'] = "tcp"
# 下面的 172.18.147.173 为本机IP,根据实际情况修改,不能为 localhost 或者127.0.0.1,否则 docker 访问不到
gitlab_workhorse['listen_addr'] = "172.18.147.173:8181" 

最后执行下面命令让配置生效:

$gitlab-ctl reconfigure

3. 配置 nginx
增加 gitlab.conf 的配置(所有需要注意的地方都加了中文注释):

upstream gitlab-workhorse {server 172.18.147.173:8181; # 根据实际情况修改
}

## Normal HTTP host
server {listen 80;
  listen [::]:80 default_server;
  server_name gitlab.papamk.com; ## 修改成自己的域名;
  server_tokens off; ## Don't show the nginx version number, a security best practice
  root /opt/gitlab/embedded/service/gitlab-rails/public;

  ## See app/controllers/application_controller.rb for headers set

  ## Individual nginx logs for this GitLab vhost
  access_log  /home/wwwlogs/gitlab_access.log; # 根据实际情况修改
  error_log   /home/wwwlogs/gitlab_error.log; # 根据实际情况修改

  location / {client_max_body_size 0;
    gzip off;

    ## 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_http_version 1.1;

    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-Forwarded-Proto   $scheme;

    proxy_pass http://gitlab-workhorse;
  }
}

该配置根据官网提供的配置修改而来:https://gitlab.com/gitlab-org/gitlab-recipes/blob/master/web-server/nginx/gitlab-omnibus-nginx.conf。

重启nginx:

$sudo service nginx restart

4. 登陆浏览器,这时可以看到安装成功了,如下图所示:

首次登陆会要求重置管理员密码,管理员的默认用户名为root

CentOS 7.x 上 GitLab 搭建详细教程

https的支持

1. 使用 Let's encrypt 申请免费的 SSL 证书,该项目提供了一个叫 certbot/certbot-auto 的工具获取证书,执行下面命令获取工具:

$wget https://dl.eff.org/certbot-auto
$chmod +x certbot-auto

执行下面命令生成生成 gitlab.papamk.com 的证书,其中 --agree-tos 表示同意协议,--email 95496875@qq.com为自己的email

$./certbot-auto --agree-tos --email 95496875@qq.com certonly --webroot -w /opt/gitlab/embedded/service/gitlab-rails/public/ -d gitlab.papamk.com 

执行成功后,可通过以下命令查看下证书位置,nginx需要引用这些文件。(一般位于 /etc/letsencrypt/live/gitlab.papamk.com/ 目录)

$./certbot-auto certificates

2. 修改 /etc/gitlab/gitlab.rb,对外的URL 改为https:

external_url 'https://gitlab.papamk.com'

执行重新配置命令以便让新配置生效:

$gitlab-ctl reconfigure

3. 修改前面的 nginxgitlab.conf的配置,全部替换成下面的内容(所有需要注意的地方都加了中文注释):

upstream gitlab-workhorse {server 172.18.147.173:8181; # 根据实际情况修改
}
## Redirects all HTTP traffic to the HTTPS host
server {## Either remove "default_server" from the listen line below,
  ## or delete the /etc/nginx/sites-enabled/default file. This will cause gitlab
  ## to be served if you visit any address that your server responds to, eg.
  ## the ip address of the server (http://x.x.x.x/)
  listen 0.0.0.0:80;
  listen [::]:80 ipv6only=on default_server;
  server_name gitlab.papamk.com; ## 改成自己的域名
  server_tokens off; ## Don't show the nginx version number, a security best practice
  return 301 https://$http_host$request_uri;
  access_log  /home/wwwlogs/gitlab_access.log; # 根据实际情况修改
  error_log   /home/wwwlogs/gitlab_error.log; # 根据实际情况修改
}
## HTTPS host
server {listen 0.0.0.0:443 ssl;
  listen [::]:443 ipv6only=on ssl default_server;
  server_name gitlab.papamk.com; ## 改成自己的域名
  server_tokens off; ## Don't show the nginx version number, a security best practice
  root /opt/gitlab/embedded/service/gitlab-rails/public;

  ## Strong SSL Security
  ## https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html & https://cipherli.st/
  ssl on;
  ssl_certificate /etc/letsencrypt/live/gitlab.papamk.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/gitlab.papamk.com/privkey.pem;

  # GitLab needs backwards compatible ciphers to retain compatibility with Java IDEs
  ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;
  ssl_session_timeout 5m;

  ##   sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096
  # ssl_dhparam /etc/ssl/certs/dhparam.pem;

  access_log  /home/wwwlogs/gitlab_access.log; # 根据实际情况修改
  error_log   /home/wwwlogs/gitlab_error.log; # 根据实际情况修改
  location / {client_max_body_size 0;
    gzip off;

    ## 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_http_version 1.1;

    proxy_set_header    Host                $http_host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-Ssl     on;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto   $scheme;
    proxy_pass http://gitlab-workhorse;
  }
}

重启nginx:

$sudo service nginx restart

4. 定期更新 ssl 证书
Let's encrypt 提供的证书,有效期为 90,到期后执行如下命令即可继续使用:

$./certbot-auto renew

将该命令加入到crontab,每小时刷新一次,就不用担心过期了。先将该命令移到系统目录下:

$sudo mv certbot-auto /usr/bin/certbot-auto

然后执行crontab -e,添加一行:

0 */1 * * *  /usr/bin/certbot-auto renew

发送邮件的支持

官网已经给出详细的配置说明和各大邮件服务提供商的示例,不再对配置做说明:https://docs.gitlab.com/omnibus/settings/smtp.html。

我自己使用 163 邮箱做测试,而官网示例没有,这里给出参考配置:

gitlab_rails['gitlab_email_from'] = 'xxxx@163.com' # 替换成实际的 email
gitlab_rails['smtp_enable'] = true                                                      
gitlab_rails['smtp_address'] = "smtp.163.com"                                          
gitlab_rails['smtp_port'] = 465                                                        
gitlab_rails['smtp_user_name'] = "xxxx@163.com" # 替换成实际的 email                               
gitlab_rails['smtp_password'] = "xxx"  # 替换成实际的密码                                          
#gitlab_rails['smtp_domain'] = "163.com"                                                
gitlab_rails['smtp_authentication'] = "login"                                          
gitlab_rails['smtp_enable_starttls_auto'] = true                                        
gitlab_rails['smtp_tls'] = true 

一般来说,服务提供商的 SMTP 邮箱都会限制每日发送次数(250 封左右),所以推荐有兴趣的同学用 Postfix 自建邮箱服务器,或者使用 mailgun 这种专业的邮件服务提供商。

如果是阿里云上的服务器,作为客户端使用第三方的 SMTP 配置,会发现被禁止连接 SMTP 的 25 端口,需要使用 465 或者587

安全相关

  • root的密码建议设置得复杂些;
  • 管理员登陆后,推荐开启注册邮箱验证,防恶意注册,见:https://docs.gitlab.com/ce/security/user_email_confirmation.html。

参考

  • 阿里云下 Docker 安装部署 GitLab
  • GitLab 升级历程
  • 为 GitLab 启用 Let’s Encrypt 颁发的 SSL 证书

 

更多 GitLab 相关教程见以下内容

Ubuntu 14.04 下安装 GitLab 指南  http://www.linuxidc.com/Linux/2015-12/126876.htm

如何在 Ubuntu Server 14.04 下安装 Gitlab 中文版  http://www.linuxidc.com/Linux/2015-12/126875.htm

CentOS 源码安装 GitLab 汉化版  http://www.linuxidc.com/Linux/2015-10/124648.htm

CentOS7 安装 GitLab、汉化及使用  http://www.linuxidc.com/Linux/2017-11/148223.htm

CentOS 7 安装部署 GitLab 服务器  http://www.linuxidc.com/Linux/2017-06/144990.htm

在 RHEL6/CentOS6/ScientificLinux6 上安装 GitLab 6.0.2 http://www.linuxidc.com/Linux/2014-03/97831.htm

CentOS 6.5 安装 GitLab 教程及相关问题解决 http://www.linuxidc.com/Linux/2014-05/101526.htm

CentOS 7 安装部署 GitLab 服务器  http://www.linuxidc.com/Linux/2017-06/144990.htm

GitLab 的详细介绍:请点这里
GitLab 的下载地址:请点这里 

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149766.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计7486字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中