Gitea 轻量级 Git 托管平台部署指南 (Ubuntu 22.04 + Docker)
·
环境准备
- 操作系统: Ubuntu 22.04
- Docker 版本: 29.2.0
安装 Docker 和 Docker Compose
# 更新软件包列表
sudo apt update
# 安装必要依赖
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# 添加 Docker 官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 添加 Docker 仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装 Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# 启动并设置开机自启
sudo systemctl start docker
sudo systemctl enable docker
配置 Gitea 容器
# 创建配置目录
mkdir -p /app/gitea
cd /app/gitea
# 创建 docker-compose.yml 文件
nano docker-compose.yml
将以下内容添加到 docker-compose.yml 文件中:
services:
gitea:
image: gitea/gitea:1.25
container_name: gitea
restart: unless-stopped
hostname: gitea-server
# 资源限制
mem_limit: "250m"
memswap_limit: "500m"
cpus: "1.0"
# 性能优化
shm_size: '128m' # 增加共享内存
tmpfs:
- /tmp:size=64M,mode=1777
- /dev/shm:size=64M,mode=1777
# 端口映射
ports:
- "8001:3000"
- "222:222"
# 数据卷 - /etc/gitea 第一次运行时需要初始化配置文件,启动后的配置文件位置 /data/gitea/conf/app.ini
volumes:
- ./data:/data
- ./config:/etc/gitea:ro
# 使用tmpfs加速临时文件
- type: tmpfs
target: /data/gitea/tmp
tmpfs:
size: 100000000 # 100MB
# 日志配置
logging:
driver: "json-file"
options:
max-size: "2m"
max-file: "2"
compress: "true"
# 环境变量
environment:
- USER_UID=1000
- USER_GID=1000
- TZ=Asia/Shanghai
# 健康检查
healthcheck:
test: ["CMD-SHELL", "wget -q -O- http://127.0.0.1:3000/health || exit 1"]
interval: 30s
timeout: 10s
retries: 3
start_period: 120s
# 安全配置
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
cap_add:
- CHOWN
- DAC_OVERRIDE
- FOWNER
- SETGID
- SETUID
ulimits:
nofile:
soft: 65535
hard: 65535
配置 app.ini 文件
创建 config/app.ini 文件并添加以下内容:
APP_NAME = Ruizhi Git Server
RUN_MODE = prod
[server]
APP_DATA_PATH = /data/gitea
DOMAIN = 服务器IP或域名
HTTP_PORT = 3000
ROOT_URL = http://服务器IP或域名:8001
SSH_PORT = 222
SSH_LISTEN_PORT = 222
DISABLE_ROUTER_LOG = true
ENABLE_GZIP = true
START_SSH_SERVER = true
[database]
DB_TYPE = sqlite3
PATH = /data/gitea/gitea.db
[repository]
ROOT = /data/git/repositories
DISABLE_HTTP_GIT = false
ENABLE_LOCAL_PATH_MIGRATION = false
[log]
LEVEL = warn
MODE = console
[security]
INSTALL_LOCK = true
SECRET_KEY = 配置自己的随机字符串
INTERNAL_TOKEN = 配置自己的随机字符串
[service]
DISABLE_REGISTRATION = true
REQUIRE_SIGNIN_VIEW = true
SHOW_REGISTRATION_BUTTON = false
启动 Gitea 服务
sudo docker compose up -d
访问 Gitea
打开浏览器访问 http://服务器IP或域名:8001,按照提示完成初始设置。
仓库迁移
# 克隆现有仓库
git clone --mirror <existing-repo-url>
cd <repo-name>.git
# 推送到 Gitea
git push --mirror http://服务器IP或域名:8001/<username>/<repo-name>.git
SSH 密钥配置
# 生成 SSH 密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 添加公钥到 Gitea
# 登录 Gitea -> 设置 -> SSH 公钥
# 添加 ~/.ssh/id_rsa.pub 内容
# 使用 SSH 克隆仓库
git clone ssh://git@服务器IP或域名:222/<username>/<repo-name>.git
注意事项
-
请勿配置RUN_USER参数
-
如需使用SSH服务:
- 避免使用22端口(可能导致报错)
- 建议改用222等替代端口
- 需同时启用内部SSH服务功能
更多推荐

所有评论(0)