Gitlab推荐配置:
4核 4GB内存 支持500个用户
8核 8GB内存 支持1000个用户
最低配置建议:2核 4G内存
一、安装和配置必要的依赖项
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates tzdata perl
接下来,安装Postfix以发送通知电子邮件。如果要使用其他解决方案发送电子邮件,请跳过此步骤并 在安装GitLab之后配置外部SMTP服务器。
说明:postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件。postfix是Wietse Venema想要为使用最广泛的sendmail提供替代品的一个尝试。在Internet世界中,大部分的电子邮件都是通过sendmail来投递的,大约有100万用户使用sendmail,每天投递上亿封邮件。这真是一个让人吃惊的数字。Postfix试图更快、更容易管理、更安全,同时还与sendmail保持足够的兼容性。
参考一:postfix百度百科 参考二:postfix网站
sudo apt install -y postfix
在安装Postfix期间,可能会出现一个配置屏幕。选择“ Internet网站”,然后按Enter。使用服务器的外部DNS作为“邮件名”,然后按Enter。如果出现其他屏幕,请继续按Enter接受默认设置。
二、添加GitLab软件包存储库并安装软件包
添加GitLab软件包存储库,可以切换为国内源,例如:gitlab-ce清华源。
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
接下来,安装GitLab软件包。确保已正确设置DNS,然后更改https://gitlab.example.com为要访问GitLab实例的URL。安装将自动配置并在该URL启动GitLab。
对于https://URL,GitLab将通过Let’s Encrypt自动请求证书,这需要入站HTTP访问和有效的主机名。您也可以使用自己的证书,也可以仅使用http://(不带s)。
关于Let’s Encrypt本文不做介绍。本文采用http访问方式安装gitlab-ce,安装成功后,配置https访问方式。
sudo EXTERNAL_URL="http://gitlab.example.com" apt install gitlab-ce
三、浏览到主机名并登录
首次访问时,您将被重定向到密码重置屏幕。设置初始管理员帐户的密码后,您将被重定向回登录屏幕。使用默认帐户的用户名root登录。
四、手动配置gitlab的https访问
制作自签名证书,参考链接:Ubuntu18.04 使用 openssl制作自签名证书。
域启用HTTPS gitlab.example.com:
打开文件/etc/gitlab/gitlab.rb
- 打开文件/etc/gitlab/gitlab.rb,编辑external_url的值:
#note the 'https' below
external_url "https://gitlab.example.com"
- 打开文件/etc/gitlab/gitlab.rb,禁用Let’s Encrypt:
letsencrypt['enable'] = false
- 创建/etc/gitlab/ssl目录,然后在其中复制密钥和证书:
sudo mkdir -p /etc/gitlab/ssl
sudo chmod 755 /etc/gitlab/ssl
sudo cp gitlab.example.com.key gitlab.example.com.crt /etc/gitlab/ssl/
由于本例中的主机名是gitlab.example.com,因此GitLab将查找分别称为/etc/gitlab/ssl/gitlab.example.com.key和的私钥和公共证书文件 /etc/gitlab/ssl/gitlab.example.com.crt。
确保使用完整的证书链,以防止客户端连接时出现SSL错误。完整的证书链顺序应首先包含服务器证书,然后是所有中间证书,最后是根CA。
如果certificate.key文件受密码保护,则当您重新配置GitLab时,NGINX不会要求输入密码。在这种情况下,GitLab将无任何提示地静默失败。要从密钥中删除密码,请运行:
openssl rsa -in certificate_before.key -out certificate_after.key
- http自动重定向到https(打开文件/etc/gitlab/gitlab.rb编辑如下内容):
nginx['enable'] = true
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"
- 重新配置GitLab:
sudo gitlab-ctl reconfigure
- 重新配置完成后,您的GitLab实例应位于处https://gitlab.example.com。
- 如果使用防火墙,则可能必须打开端口443才能允许入站HTTPS通信。
# UFW example (Debian, Ubuntu)
sudo ufw allow https
五、SSH拉取gitlab代码
- 个人电脑生成秘钥
ssh-keygen -t rsa -C "your_email@example.com"
- 上传公钥到gitlab
- “.ssh”目录下创建config文件,内容如下:
Host gitlab.example.com
PreferredAuthentications publickey
IdentityFile /path/to/个人私钥文件
- git clone git@gitlab.example.com:XXXX/XXXX.git
六、SAML配置
名词(Term) | 描述(Description) |
Identity provider 身份提供商 (IdP) | The service which manages your user identities, such as Okta or OneLogin 管理您的用户身份的服务,例如 Okta 或 OneLogin |
Service provider服务提供商 (SP) | GitLab can be configured as a SAML 2.0 SP GitLab 可以配置为 SAML 2.0 SP |
Assertion断言 | A piece of information about a user’s identity, such as their name or role. Also known as claims or attributes 关于用户身份的一条信息,例如他们的姓名或角色。也称为声明或属性 |
Single Sign-On单点登录 (SSO) | Name of authentication scheme 身份验证方案的名称 |
Assertion consumer service URL断言消费者服务 URL | The callback on GitLab where users will be redirected after successfully authenticating with the identity provider 在与身份提供者成功进行身份验证后,用户将被重定向到 GitLab 上的回调 |
Issuer发行人 | How GitLab identifies itself to the identity provider. Also known as a “Relying party trust identifier” GitLab 如何向身份提供者标识自己。也称为“依赖方信任标识符” |
Certificate fingerprint证书指纹 | Used to confirm that communications over SAML are secure by checking that the server is signing communications with the correct certificate. Also known as a certificate thumbprint 用于通过检查服务器是否使用正确的证书签署通信来确认通过 SAML 的通信是安全的。也称为证书指纹 |
- gitlab.rb文件配置,在gitlab.rb中修改下面的配置参数:
gitlab_rails['omniauth_allow_single_sign_on'] = ['saml']
gitlab_rails['omniauth_block_auto_created_users'] = false
gitlab_rails['omniauth_auto_link_saml_user'] = true
gitlab_rails['omniauth_providers'] = [
{
name: 'saml',
args: {
assertion_consumer_service_url: 'https://gitlab.example.com/users/auth/saml/callback',
idp_cert_fingerprint: '43:51:43:a1:b5:fc:8b:b7:0a:3a:a9:b1:0f:66:73:a8',
idp_sso_target_url: 'https://login.example.com/idp',
issuer: 'https://gitlab.example.com',
name_identifier_format: 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent'
},
label: 'Provider name' # optional label for SAML login button, defaults to "Saml"
}
]
注意:
(1).更改assertion_consumer_service_url的值,以匹配 GitLab 的 HTTPS 端点(附加users/auth/saml/callback到您的 GitLab 安装的 HTTPS URL后面以生成正确的值)。
(2).更改idp_cert_fingerprint为IdP提供的指纹信息,如果使用指纹,则必须是 SHA1 指纹。
(3)name_identifier_format的值为固定写法,共有两种:“urn:oasis:names:tc:SAML:2.0:nameid-format:persistent”和“urn:oasis:names:tc:SAML:2.0:nameid-format:transient”。
(4).idp_sso_target_url是身份验证请求应发送到的 URL,需要从IdP获取。
更改issuer的值为唯一名称,用于向 IdP 标识应用程序,默认为saml。
参考链接:OmniAuth SAML 文档、有关配置身份提供程序的说明
- 运行gitlab-ctl reconfigure,使配置生效
sudo gitlab-ctl reconfigure
- 此时可以使用gitlab的SSO服务了。
参考链接:GitLab Docs Html、GitLab Docs Home page
附:GitLab-ce Docker部署
部署前提:目标Ubuntu18.04机器已安装docker。可通过下面的命令安装docker,有关Docker相关的配置,这里不做介绍。
sudo apt update && sudo apt install docker.io -y
- Docker Pull GitLab-ce最新镜像:
docker pull gitlab/gitlab-ce:latest
- 在目标机器创建gitlab-ce数据卷需要使用的目录
mkdir -p /path/to/gitlab/config config目录
mkdir -p /path/to/gitlab/logs logs目录
mkdir -p /path/to/gitlab/data data目录
- 执行Docker Run启动GitLab容器
docker run -itd --hostname your-host-name -p 8443:443 -p 8080:80 --publish 33322:22 --name gitlab --restart always \
--volume /path/to/gitlab/config:/etc/gitlab \
--volume /path/to/gitlab/logs:/var/log/gitlab \
--volume /path/to/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:latest
容器启动参数中443、22、80端口的映射,可修改为自己想映射的端口。
- 执行Docker Exec进入GitLab容器
docker exec -it gitlab /bin/bash
- 在GitlLab容器内修改gitlab.rb参数(HTTP+IP访问)
external_url 'http://宿主机IP'
gitlab_rails['gitlab_ssh_host'] = '宿主机IP'
gitlab_rails['gitlab_shell_ssh_port'] = 22
- 设置完成后,重新配置gitlab-ctl reconfigure,使配置生效
sudo gitlab-ctl reconfigure
- 其余的配置,与正常apt部署GitLab-ce操作相同。