CentOS 7.x上gitlab搭建教程(https可用,邮件可用)
目录
- 知识要求
- 搭建感想
- 搭建过程
- 参考
知识要求:
nginx
基础知识
搭建感想
注:以下是我搭建
gitlab
时的思考,需要nginx
的基础知识,Docker
的基础知识才容易理解,与下面的搭建过程是独立的,不感兴趣可直接略过。
其实gitlab
已经搭建并用了一年多了,现在所有的项目管理都通过gitlab
完成。但是一直以来都有2个问题:
80
端口被系统的nginx
占用了,所以只能监听非80
端口;443
端口也被系统的nginx
占用,所以也一直没增加对https
的支持;
最近正在尝试对所有已有的服务Docker
化,一方面想让gitlab
的搭建更简单些,另一方面也把这两个问题都处理掉。
于是就做了两个Docker
容器: nginx
和gitlab
,相当于nginx
和gitlab
运行在局域网的不同主机,所以端口上没冲突。nginx
是对外的服务器,它做一层反向代理到gitlab
就能让gitlab
提供对外的服务。
然而。。。这个做法却带来了一个新问题:gitlab
需要的是22
,80
,443
端口,80
与443
通过反向代理解决了,22
却没办法解决。因为正常来讲,宿主机的SSH
肯定也在使用,所以gitlab
的SSH
监听端口映射到宿主机会有冲突。
当然了,解决办法还是有的,不过非常繁琐。我们做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
的服务,所以默认会占用80
和443
端口。一般来说,我们做WEB
开发,服务器上早就安装了nginx
或者apache
,把80
和443
端口给占了,所以需要做些修改防止冲突。
简单的修改端口是不可行的,比如80
改成85
,当查看gitlab
项目时,下图中的项目地址会变成http://git.papamk.com:85/papamk/groupbill
这样,看着就不舒服。启用https
则在使用过程中则会出现其他的问题。这里不一一论述,直接说明正确的配置方法。
具体步骤:
1.首先确保你的服务器已经有运行nginx
,并且该nginx
监听宿主机的80
和443
端口。(如果你原来使用的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']
修改监听方式和监听地址(如果nginx
与gitlab
都在宿主机上,不用改也行;如果nginx
在docker
中,则需要修改)
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
。
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.修改前面的nginx
的gitlab.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。
参考
CentOS 7.x上gitlab搭建教程(https可用,邮件可用)的更多相关文章
- Openssl的编译安装以及Vs2012上环境搭建教程
Openssl的编译安装以及Vs2012上环境搭建教程 一.Openssl的编译安装 一.准备工作 1.Openssl下载地址:https://www.openssl.org/source/ 2.Ac ...
- centos7安装gitlab 支持带认证https,开启邮件功能 超级简单.
官方安装说明:https://about.gitlab.com/install/#centos-7 自定义yum源 自行搞定 下载gitlab 官方安装: curl -s https://packag ...
- CentOS 7 系统下 GitLab 搭建
参考地址:https://blog.csdn.net/t748588330/article/details/79915003 1. 安装:使用 GitLab 提供仓库在线安装 curl -sS htt ...
- CentOS 6.0下phpvod搭建教程(LAMP+phpvod)
之所以安装CentOS是因为之前试过RedHat,但是发现RedHat在安装时,无法获取安装源,原因是RedHat系统没有在RHN注册. 网上的很多教程都说可以直接换用CentOS的源,可我小搞里一会 ...
- Redis在CentOS for LInux上安装详细教程
1.首先上传安装包,这里我以 redis-5.0.8.tar.gz 为例子. Linux下载redis地址:wget http://download.redis.io/releases/redis-5 ...
- WordPress搭建教程---购买域名+购买VPS主机+域名DNS解析+网站环境+上传网站程序
WordPress搭建教程 购买域名---NameSilo 购买VPS主机---Vultr 域名DNS解析 网站环境 上传网站程序 参考文章: 1. WordPress搭建教程 https://zhu ...
- gitlab 搭建
一.ubuntu搭建gitlab 1. 如果以前有安装过gitlab请根据以下步骤来删除 https://www.cnblogs.com/shansongxian/p/6678110.htm ...
- centos 7.4 安装gitlab
centos 7.4 安装gitlab #curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/scrip ...
- gitlab服务器搭建教程
gitlab服务器搭建教程 ----2016年终总结 三 参考https://bbs.gitlab.cc/topic/35/gitlab-ce-8-7-%E6%BA%90%E7%A0%81%E5%AE ...
随机推荐
- 基于Node的高性能MVC框架
赶上公司去Windows化,有一大波.net站点需要转成Node.js,于是自己就顺便琢磨一个通用的Node版MVC框架. 经过几天的努力,beta版终于面世了!因为其高性能的特点,特地命名node- ...
- 51Nod 1110 距离之和最小 V3 中位数 思维
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 ...
- 域名和ip不能访问的原因
centos的话可能默认可能会有firewalld,可以执行 systemctl stop firewalld systemctl disable firewalld 禁用后在看看,前提都是域名得备案 ...
- 生成ssl秘钥的方法(纯粹本人记录用的,勿踩)
openssl genrsa -des3 -out server.key 1024 openssl req -new -key server.key -out server.csr cp server ...
- Jarvis OJ - [XMAN]level3 - Writeup——ret2libc尝试
这次除了elf程序还附带一个动态链接库 先看一下,很一般的保护 思路分析 在ida中查看,可以确定通过read函数输入buf进行溢出,但是并没有看到合适的目标函数 但是用ida打开附带的链接库,可以看 ...
- python 3.6 tkinter+urllib+json 火车车次信息查询
--------blogs: 陈月白 http://www.cnblogs.com/chenyuebai -------- 一.概述 妹子工作时需要大量地查询火车车次至南京的信息,包括该 ...
- vue-cli 安装失败Failed to download repo vuejs-templates/webapck-simple: Response code 404 (Not Found)
新学习vue的萌新们经常会遇到各种各样的坑.例如上面这个报错.这个一般是命令行面板写错单词导致. 正确:vue init webpack-simple .(注意"."点,指当前目录 ...
- Spring3.0官网文档学习笔记(二)
1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包) spring-*.jar *号代表 ...
- Objective-C基础语法高速入门
Objective-C是Mac软件开发领域最基本的开发语言,假如我们对C语言已经非常熟悉或者具有面向对象语言的基础.对于我们学习Objective-C将会非常实用. 方法调用(Calling Meth ...
- 使用Myeclipse为数据表创建hibernate实体对象
hibernate是orm框架的一种,orm即Object Relational Mapping,对象映射关系,其主要作用是将数据库(mysql,mssql,oracle)的对象转换为具体编程语言(如 ...