【Gitlab】Ubuntu 20.04服务器部署Gitlab

写一个 适用于 Ubuntu 20.04/22.04 的 GitLab 一键部署脚本,包括:

  • 安装依赖

  • 安装 GitLab CE

  • 配置公网 IP 或域名

  • 自动开启 HTTPS(Let's Encrypt)

  • 配置防火墙

下面是完整脚本:

bash 复制代码
#!/bin/bash

# =========================
# GitLab 一键安装脚本
# =========================

# 1. 检查是否以 root 运行
if [ "$EUID" -ne 0 ]; then
  echo "请使用 root 用户运行此脚本"
  exit 1
fi

# 2. 更新系统
apt update && apt upgrade -y

# 3. 安装依赖
apt install -y curl openssh-server ca-certificates tzdata perl ufw

# 4. 配置防火墙
ufw allow http
ufw allow https
ufw allow OpenSSH
ufw --force enable

# 5. 安装 Postfix(用于邮件通知,可选)
debconf-set-selections <<< "postfix postfix/mailname string gitlab.example.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
apt install -y postfix

# 6. 添加 GitLab 仓库
curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash

# 7. 设置外部 URL
read -p "请输入你的域名或公网 IP(例:http://gitlab.example.com 或 http://123.123.123.123): " EXTERNAL_URL

# 自动生成 GitLab 配置文件
export EXTERNAL_URL=$EXTERNAL_URL

# 8. 安装 GitLab
apt install -y gitlab-ce

# 9. 重新配置 GitLab
gitlab-ctl reconfigure

# 10. 自动启用 HTTPS(Let's Encrypt)
read -p "是否启用 HTTPS(Let's Encrypt)? [y/n]: " ENABLE_HTTPS
if [[ "$ENABLE_HTTPS" == "y" || "$ENABLE_HTTPS" == "Y" ]]; then
    # 设置 GitLab 配置开启 Let's Encrypt
    gitlab-ctl stop nginx
    gitlab-ctl reconfigure

    # 编辑配置文件启用 Let's Encrypt
    GITLAB_CONFIG="/etc/gitlab/gitlab.rb"
    sed -i "s|^# external_url '.*'|external_url 'https://$EXTERNAL_URL'|" $GITLAB_CONFIG
    sed -i "s|^# letsencrypt\['enable'\] = false|letsencrypt['enable'] = true|" $GITLAB_CONFIG

    # 重新配置
    gitlab-ctl reconfigure
fi

# 11. 输出访问信息
echo "======================================"
echo "GitLab 安装完成!"
echo "访问地址: $EXTERNAL_URL"
echo "默认管理员账号: root"
echo "请首次访问设置管理员密码"
echo "======================================"

# 12. 查看服务状态
gitlab-ctl status

使用方法

  1. 新建一个脚本文件:

    nano install_gitlab.sh

  2. 粘贴上面的内容,保存并退出。

  3. 赋予执行权限并运行:

bash 复制代码
chmod +x install_gitlab.sh
sudo ./install_gitlab.sh
  1. 按提示输入 公网 IP 或域名,选择是否开启 HTTPS。

这个脚本安装的是 GitLab CE 社区版,支持 HTTPS,防火墙也会自动配置好。

相关推荐
花间相见9 分钟前
【终端效率工具01】—— Yazi:Rust 编写的现代化终端文件管理器,告别繁琐操作
前端·ide·git·rust·极限编程
蜡台1 小时前
Git 常用配置:修改地址 ,提交模板配置
git·gitlab·template·git commit·gitbash
拽着尾巴的鱼儿2 小时前
git不追踪已提交至仓库的本地又修改文件
git
We་ct3 小时前
Git 核心知识点全解析
开发语言·前端·git·gitee·github
无限进步_4 小时前
【C++】寻找字符串中第一个只出现一次的字符
开发语言·c++·ide·windows·git·github·visual studio
summerkissyou19874 小时前
git-命令大全
git
CoderOnly4 小时前
git fork之后,怎么继续pull
git
ROLL.75 小时前
Git和Repo
java·git·安卓
火车叼位5 小时前
告别发整个文件!用 Git Patch 优雅地传递代码变更
git
念恒123066 小时前
Linux基础开发工具(git篇)
linux·c语言·git