Postfix是CentOS系统上常用的邮件传输代理(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服务器,可填写其地址(如SMTP服务商的SMTP端点)
sudo systemctl start postfix启动服务,sudo systemctl enable postfix设置开机自启。编辑GitLab的全局配置文件/etc/gitlab/gitlab.rb,找到邮件设置部分并修改为以下内容(根据你的SMTP服务器调整):
gitlab_rails['smtp_enable'] = true # 启用SMTP
gitlab_rails['smtp_address'] = "smtp.example.com" # SMTP服务器地址(如Gmail的smtp.gmail.com)
gitlab_rails['smtp_port'] = 587 # SMTP端口(常用587或465,587用于TLS)
gitlab_rails['smtp_user_name'] = "your-email@example.com" # SMTP认证用户名(如你的邮箱)
gitlab_rails['smtp_password'] = "your-email-password" # SMTP认证密码(如邮箱密码或应用专用密码)
gitlab_rails['smtp_domain'] = "example.com" # SMTP服务器域名(如Gmail的gmail.com)
gitlab_rails['smtp_authentication'] = "login" # 认证方式(通常为"login")
gitlab_rails['smtp_enable_starttls_auto'] = true # 启用STARTTLS加密(推荐)
gitlab_rails['smtp_tls'] = false # 不使用SSL(若SMTP服务器要求SSL,设为true)
gitlab_rails['smtp_openssl_verify_mode'] = 'peer' # SSL证书验证模式('peer'表示验证,'none'用于自签名证书)
注意:若使用Gmail等第三方SMTP服务,需提前开启“允许不够安全的应用访问”或创建应用专用密码(避免直接使用邮箱密码)。
保存/etc/gitlab/gitlab.rb文件后,运行以下命令使配置生效:
sudo gitlab-ctl reconfigure # 重新配置GitLab
sudo gitlab-ctl restart # 重启GitLab服务
此步骤会将配置写入GitLab的运行时环境,并重启相关服务(如gitlab-rails)。
通过GitLab控制台或界面发送测试邮件,验证配置是否正确:
sudo gitlab-rails console进入控制台,输入以下命令发送测试邮件:Notify.test_email('recipient@example.com', 'GitLab邮件测试', '这是一封来自GitLab的测试邮件').deliver_now
执行后,检查收件箱(或垃圾邮件箱)是否收到测试邮件。Settings -> Email & Webhooks,点击“Test email configuration”按钮,填写测试邮箱地址并发送。sudo systemctl status postfix)、GitLab日志(/var/log/gitlab/gitlab-rails/production.log)及SMTP服务器防火墙设置。smtp_openssl_verify_mode设为'none'(不推荐生产环境使用)。