保姆级教程:在RockyLinux 9.5上配置Gitlab中文版,并解决首次登录密码失效问题

张开发
2026/4/11 0:50:42 15 分钟阅读

分享文章

保姆级教程:在RockyLinux 9.5上配置Gitlab中文版,并解决首次登录密码失效问题
RockyLinux 9.5 上配置 GitLab 中文版及密码管理全攻略当你第一次在 RockyLinux 9.5 上成功部署 GitLab 后面对全英文界面和那个24小时后就会消失的初始密码文件是不是有点手足无措别担心这篇文章将带你一步步解决这两个最常见的问题让你轻松驾驭 GitLab。1. 环境准备与 GitLab 安装在开始之前确保你的 RockyLinux 9.5 系统满足以下最低要求CPU: 2核及以上内存: 4GB及以上磁盘空间: 至少10GB可用空间1.1 系统基础配置首先我们需要做一些基础的系统配置# 关闭并禁用防火墙生产环境请谨慎操作 sudo systemctl stop firewalld sudo systemctl disable firewalld # 检查防火墙状态确认已关闭 firewall-cmd --state1.2 安装 GitLab CERockyLinux 9.5 上安装 GitLab 社区版有多种方式这里我们推荐使用 RPM 包安装# 下载 GitLab CE RPM 包建议使用国内镜像加速 wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el9/gitlab-ce-18.0.1-ce.0.el9.x86_64.rpm # 安装下载的 RPM 包 sudo dnf install gitlab-ce-18.0.1-ce.0.el9.x86_64.rpm -y安装完成后我们需要配置 GitLab 的外部访问地址# 编辑 GitLab 配置文件 sudo vi /etc/gitlab/gitlab.rb # 找到并修改 external_url 为你的服务器IP或域名 external_url http://your_server_ip保存后运行以下命令应用配置sudo gitlab-ctl reconfigure这个命令会花费一些时间初始化 GitLab。完成后你会看到类似下面的输出Notes: Default admin account has been configured with following details: Username: root Password: You didnt opt-in to print initial root password to STDOUT. Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.2. 初始密码管理与安全策略2.1 获取初始密码安装完成后系统会自动生成一个 root 用户的初始密码存储在/etc/gitlab/initial_root_password文件中。查看密码sudo cat /etc/gitlab/initial_root_password输出示例# WARNING: This value is valid only in the following conditions # 1. If provided manually (either via GITLAB_ROOT_PASSWORD environment variable or via gitlab_rails[initial_root_password] setting in gitlab.rb, it was provided before database was seeded for the first time (usually, the first reconfigure run). # 2. Password hasnt been changed manually, either via UI or via command line. # # If the password shown here doesnt work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password. Password: 69WYlqvp9PlhONYCaiBo2Fz2K1vwH5IcWnifqCL8U # NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.重要提示这个密码文件会在24小时后被自动删除因此我们需要立即采取以下措施2.2 立即修改 root 密码使用浏览器访问你的 GitLab 实例http://your_server_ip使用用户名root和刚才获取的密码登录点击右上角用户头像 → Preferences → Password输入当前密码和新密码然后保存2.3 防止初始密码文件被删除如果你担心忘记密码可以采取以下措施保留初始密码# 备份初始密码文件 sudo cp /etc/gitlab/initial_root_password /root/gitlab_initial_root_password.bak或者你也可以通过修改 GitLab 配置来禁用密码文件的自动清理# 编辑 GitLab 配置文件 sudo vi /etc/gitlab/gitlab.rb # 添加以下配置 gitlab_rails[initial_root_password_file] /etc/gitlab/initial_root_password gitlab_rails[initial_root_password_expiration] false # 应用配置更改 sudo gitlab-ctl reconfigure2.4 通过命令行重置 root 密码如果你已经错过了初始密码文件可以通过以下命令重置 root 密码# 进入 GitLab Rails 控制台 sudo gitlab-rails console # 在控制台中执行以下命令 user User.find_by_username(root) user.password your_new_password user.password_confirmation your_new_password user.save! exit3. 配置 GitLab 中文界面3.1 通过 Web 界面设置中文登录 GitLab 后点击右上角用户头像 → Preferences在左侧菜单中选择 Localization在 Language 下拉菜单中选择 简体中文点击页面底部的 Save changes 按钮刷新页面即可看到中文界面3.2 通过配置文件设置默认语言如果你想为所有用户设置默认语言为中文可以修改 GitLab 配置文件# 编辑 GitLab 配置文件 sudo vi /etc/gitlab/gitlab.rb # 添加以下配置 gitlab_rails[gitlab_default_locale] zh_CN # 应用配置更改 sudo gitlab-ctl reconfigure4. GitLab 日常管理与维护4.1 常用管理命令GitLab 提供了一套完整的命令行工具gitlab-ctl来管理系统# 检查 GitLab 服务状态 sudo gitlab-ctl status # 启动所有 GitLab 服务 sudo gitlab-ctl start # 停止所有 GitLab 服务 sudo gitlab-ctl stop # 重启所有 GitLab 服务 sudo gitlab-ctl restart # 查看 GitLab 日志 sudo gitlab-ctl tail4.2 备份与恢复定期备份 GitLab 数据是非常重要的# 创建 GitLab 备份 sudo gitlab-backup create # 备份文件默认存储在 /var/opt/gitlab/backups 目录 # 恢复备份需要先停止相关服务 sudo gitlab-ctl stop unicorn sudo gitlab-ctl stop sidekiq sudo gitlab-rake gitlab:backup:restore BACKUP备份文件名 sudo gitlab-ctl start4.3 性能优化建议随着使用时间的增长GitLab 可能会变得缓慢。以下是一些优化建议# 编辑 GitLab 配置文件 sudo vi /etc/gitlab/gitlab.rb # 根据你的服务器配置调整以下参数 unicorn[worker_processes] 2 # 通常设置为 CPU 核心数 sidekiq[concurrency] 10 # 根据内存大小调整 postgresql[shared_buffers] 256MB # 建议为系统内存的25% # 应用配置更改 sudo gitlab-ctl reconfigure5. 常见问题解决方案5.1 502 错误处理如果访问 GitLab 时遇到 502 错误可以尝试以下步骤# 检查服务状态 sudo gitlab-ctl status # 查看 Nginx 错误日志 sudo tail -f /var/log/gitlab/nginx/error.log # 常见解决方案是增加 Unicorn 超时时间 sudo vi /etc/gitlab/gitlab.rb # 添加或修改以下配置 unicorn[worker_timeout] 60 # 然后重新配置 sudo gitlab-ctl reconfigure5.2 邮件服务配置要启用 GitLab 的邮件通知功能# 编辑 GitLab 配置文件 sudo vi /etc/gitlab/gitlab.rb # 添加邮件配置示例以 SMTP 为例 gitlab_rails[smtp_enable] true gitlab_rails[smtp_address] smtp.example.com gitlab_rails[smtp_port] 587 gitlab_rails[smtp_user_name] your_emailexample.com gitlab_rails[smtp_password] your_password gitlab_rails[smtp_domain] example.com gitlab_rails[smtp_authentication] login gitlab_rails[smtp_enable_starttls_auto] true gitlab_rails[gitlab_email_from] your_emailexample.com # 应用配置更改 sudo gitlab-ctl reconfigure5.3 磁盘空间不足处理GitLab 运行一段时间后可能会占用大量磁盘空间# 清理旧的 Docker 镜像如果使用 Docker 安装 docker system prune -a # 清理 GitLab 日志保留最近30天 sudo find /var/log/gitlab -type f -mtime 30 -delete # 清理 GitLab 上传的临时文件 sudo gitlab-rake tmp:clear在实际使用中我发现定期执行这些维护任务可以显著提高 GitLab 的稳定性和性能。特别是在团队规模扩大后合理的资源分配和定期维护变得尤为重要。

更多文章