寫一個 適用于 Ubuntu 20.04/22.04 的 GitLab 一鍵部署腳本,包括:
安裝依賴
安裝 GitLab CE
配置公網 IP 或域名
自動開啟 HTTPS(Let’s Encrypt)
配置防火墻
下面是完整腳本:
#!/bin/bash# =========================
# GitLab 一鍵安裝腳本
# =========================# 1. 檢查是否以 root 運行
if [ "$EUID" -ne 0 ]; thenecho "請使用 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 Encryptgitlab-ctl stop nginxgitlab-ctl reconfigure# 編輯配置文件啟用 Let's EncryptGITLAB_CONFIG="/etc/gitlab/gitlab.rb"sed -i "s|^# external_url '.*'|external_url 'https://$EXTERNAL_URL'|" $GITLAB_CONFIGsed -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
使用方法
新建一個腳本文件:
nano install_gitlab.sh
粘貼上面的內容,保存并退出。
賦予執行權限并運行:
chmod +x install_gitlab.sh
sudo ./install_gitlab.sh
按提示輸入 公網 IP 或域名,選擇是否開啟 HTTPS。
這個腳本安裝的是 GitLab CE 社區版,支持 HTTPS,防火墻也會自動配置好。