1. 安装并配置Postfix邮件服务器
Postfix是GitLab默认使用的邮件传输代理(MTA),需先安装并配置才能让GitLab发送邮件。
sudo yum install postfix命令安装。安装过程中选择“Internet Site”类型,并设置系统邮件名称(如gitlab.example.com)。/etc/postfix/main.cf文件,修改以下关键参数(替换为你的实际信息):myhostname = gitlab.example.com # GitLab实例主机名
mydomain = example.com # 域名
myorigin = $mydomain # 发件域名
inet_interfaces = all # 监听所有网络接口
inet_protocols = ipv4 # 仅使用IPv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain # 接收邮件的目标域
relayhost = # 不转发邮件到其他SMTP服务器
smtpd_tls_security_level = may # 可选TLS加密(根据SMTP服务器要求调整)
sudo systemctl start postfix启动服务,并设置开机自启sudo systemctl enable postfix。2. 配置GitLab邮件通知参数
编辑GitLab主配置文件/etc/gitlab/gitlab.rb,找到或添加以下SMTP设置(替换为你的SMTP服务器信息):
gitlab_rails['smtp_enable'] = true # 启用SMTP
gitlab_rails['smtp_address'] = "smtp.example.com" # SMTP服务器地址(如smtp.gmail.com)
gitlab_rails['smtp_port'] = 587 # SMTP端口(常用587或465)
gitlab_rails['smtp_user_name'] = "your-email@example.com" # SMTP用户名(如个人邮箱)
gitlab_rails['smtp_password'] = "your-email-password" # SMTP密码(或应用专用密码)
gitlab_rails['smtp_domain'] = "example.com" # 发件域名(与Postfix的mydomain一致)
gitlab_rails['smtp_authentication'] = "login" # 认证方式(常用login/plain)
gitlab_rails['smtp_enable_starttls_auto'] = true # 启用StartTLS加密(推荐)
gitlab_rails['smtp_tls'] = false # 不使用SSL(若SMTP服务器要求SSL则设为true)
gitlab_rails['smtp_openssl_verify_mode'] = 'peer' # 证书验证模式(peer严格验证,self_signed信任自签名,none忽略)
注意:若使用Gmail等第三方SMTP服务,需开启“允许不够安全的应用”或创建应用专用密码;若SMTP服务器使用自签名证书,将openssl_verify_mode设为none。
3. 重新配置并重启GitLab
保存gitlab.rb文件后,运行以下命令使配置生效:
sudo gitlab-ctl reconfigure # 应用GitLab配置变更
sudo gitlab-ctl restart # 重启GitLab服务
4. 测试邮件通知功能
sudo gitlab-rake gitlab:email:test命令,若输出“Email sent successfully”(邮件发送成功),则表示配置无误。常见问题排查
sudo systemctl status postfix),查看GitLab和Postfix日志(/var/log/gitlab/gitlab-rails/production.log、/var/log/maillog)定位错误。smtp_openssl_verify_mode设为none;若使用公共SMTP服务(如Gmail),确保openssl_verify_mode为peer并正确配置证书。