GitLab在Linux服务器上的部署步骤
导读:GitLab在Linux服务器上的部署步骤(以Ubuntu/Debian、CentOS/RHEL为例) 1. 准备工作 服务器要求:推荐使用Ubuntu 22.04+、CentOS 7/8+等主流发行版;至少2核CPU、4GB内存、20G...
GitLab在Linux服务器上的部署步骤(以Ubuntu/Debian、CentOS/RHEL为例)
1. 准备工作
- 服务器要求:推荐使用Ubuntu 22.04+、CentOS 7/8+等主流发行版;至少2核CPU、4GB内存、20GB存储空间(小型团队可适当降低);确保服务器联网。
- 网络配置:关闭SELinux(CentOS)或配置为宽松模式(
setenforce 0
),避免权限拦截;开放HTTP(80)、HTTPS(443)、SSH(22)端口(防火墙或云安全组)。
2. 安装依赖项
- Ubuntu/Debian:更新系统并安装
curl
(下载工具)、openssh-server
(SSH登录)、ca-certificates
(HTTPS证书)、postfix
(邮件通知,选“Internet Site”配置):sudo apt update & & sudo apt upgrade -y sudo apt install -y curl openssh-server ca-certificates postfix
- CentOS/RHEL:安装
curl
、policycoreutils-python
(SELinux管理)、openssh-server
、postfix
:sudo yum update -y sudo yum install -y curl policycoreutils-python openssh-server postfix sudo systemctl enable --now sshd # 启动SSH服务
3. 添加GitLab官方仓库
- Ubuntu/Debian:通过官方脚本添加APT仓库:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
- CentOS/RHEL:添加YUM仓库:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
4. 安装GitLab Community Edition(CE)
- Ubuntu/Debian:使用APT安装GitLab CE,指定
EXTERNAL_URL
(替换为服务器IP或域名):sudo EXTERNAL_URL="http://your_server_ip" apt install -y gitlab-ce
- CentOS/RHEL:使用YUM安装:
sudo EXTERNAL_URL="http://your_server_ip" yum install -y gitlab-ce
5. 配置GitLab
- 编辑配置文件
/etc/gitlab/gitlab.rb
,修改external_url
为服务器IP或域名(如http://gitlab.example.com
):sudo nano /etc/gitlab/gitlab.rb # 找到并修改以下行(取消注释并替换) external_url 'http://your_server_ip'
- 保存后,运行
reconfigure
命令应用配置(自动初始化数据库、启动服务):sudo gitlab-ctl reconfigure
6. 启动GitLab服务
- 启动GitLab并设置开机自启:
sudo gitlab-ctl start # 启动服务 sudo systemctl enable gitlab # 开机自启
- 检查服务状态(确保所有组件运行正常):
sudo gitlab-ctl status
7. 访问GitLab
- 在浏览器中输入
http://your_server_ip
,首次访问需设置root
管理员密码(默认账号:root
)。 - 登录后即可创建项目、管理用户及权限。
8. 可选配置
-
配置邮件通知(用于密码重置、CI/CD通知): 编辑
/etc/gitlab/gitlab.rb
,添加SMTP设置(以163邮箱为例):gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.163.com" gitlab_rails['smtp_port'] = 465 gitlab_rails['smtp_user_name'] = "your_163_email@163.com" gitlab_rails['smtp_password'] = "your_163_authorization_code" # 非邮箱密码,是授权码 gitlab_rails['smtp_domain'] = "163.com" gitlab_rails['smtp_authentication'] = "login" gitlab_rails['smtp_enable_starttls_auto'] = true gitlab_rails['smtp_tls'] = true gitlab_rails['gitlab_email_from'] = "your_163_email@163.com"
运行
sudo gitlab-ctl reconfigure
生效。 -
启用HTTPS(使用Let’s Encrypt免费证书): 修改
/etc/gitlab/gitlab.rb
:external_url 'https://your_server_ip' letsencrypt['enable'] = true letsencrypt['contact_emails'] = ['admin@example.com'] # 接收证书过期提醒
运行
sudo gitlab-ctl reconfigure
生成证书。 -
修改Web端口(如80端口被占用): 编辑
/etc/gitlab/gitlab.rb
,修改Nginx端口:nginx['listen_port'] = 8080 nginx['ssl_listen_port'] = 8443
运行
sudo gitlab-ctl reconfigure
生效,后续通过http://your_server_ip:8080
访问。
常见问题解决
- 端口冲突:若80/443端口被占用,修改
nginx['listen_port']
(如上述)后重新配置。 - 邮件发送失败:检查Postfix服务状态(
sudo systemctl status postfix
),确保SMTP配置正确(授权码而非邮箱密码)。 - 访问超时:检查防火墙是否开放对应端口(
sudo ufw allow 80/tcp
或firewall-cmd --add-service=http --permanent
)。
以上步骤覆盖了GitLab在Linux服务器上的基础部署与常见配置,可根据实际需求调整参数。部署完成后,建议定期更新GitLab至最新版本(sudo apt update &
&
sudo apt upgrade gitlab-ce
)以修复安全漏洞。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: GitLab在Linux服务器上的部署步骤
本文地址: https://pptw.com/jishu/719876.html