对于个人开发者、小型团队乃至企业来说,将项目代码托管在 GitHub、Gitee 等公共平台虽然方便,但也存在一定的隐私与可控性问题。
搭建一套私有 Git 代码仓库系统,可以实现对源码的完全控制,同时不依赖任何第三方平台,尤其适合内部项目、保密需求或私有化部署场景。
本文将以轻量级开源项目 Gitea 为例,介绍如何在一台 Linux 云服务器上快速部署并运行一套完整的 Git 仓库服务。
Gitea 是一个开源的、基于 Go 语言开发的 Git 服务平台,其特点包括:
对于资源不高的云服务器而言,Gitea 是自建 Git 平台的理想选择。
# 安装 Git
sudo apt update
sudo apt install -y git
# 可选:安装数据库(使用 SQLite 可跳过)
sudo apt install -y mariadb-server
sudo mysql_secure_installation
sudo mkdir -p /opt/gitea
sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git
sudo chown -R git:git /opt/gitea
wget -O /opt/gitea/gitea https://dl.gitea.io/gitea/latest/gitea-1.21.3-linux-amd64
chmod +x /opt/gitea/gitea
创建 Gitea 的 systemd 启动脚本:
sudo nano /etc/systemd/system/gitea.service
填入以下内容:
[Unit]
Description=Gitea (Git with a cup of tea)
After=network.target
[Service]
RestartSec=2s
Type=simple
User=git
WorkingDirectory=/opt/gitea
ExecStart=/opt/gitea/gitea web
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/opt/gitea
[Install]
WantedBy=multi-user.target
启动服务:
sudo systemctl daemon-reexec
sudo systemctl enable --now gitea
默认监听端口为 3000。
为了通过 80/443 端口访问,可使用 Nginx 做代理:
server {
listen 80;
server_name git.yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
重新加载:
sudo nginx -t && sudo systemctl reload nginx
浏览器访问:
http://your_server_ip:3000
或绑定域名后访问 http://git.yourdomain.com
,即可进入 Gitea 安装引导界面。
按需配置:
安装完成后,即可进入登录页,开始使用私有 Git 平台。
Gitea 支持:
使用如下命令将代码推送至私有平台:
git remote add origin [email protected]:username/project.git
git push -u origin main
通过部署 Gitea,你可以在自己的云服务器上搭建一个轻量级、功能完善、操作方便的 Git 仓库平台,拥有完整的权限管理、代码审查、协作能力,适合个人项目管理、团队协作、私有源码托管等多种场景。
整个部署流程无需编译环境,资源占用极低,即使在 1G 内存的 VPS 上也能稳定运行。
注:本文所使用的云服务器托管于慈云数据,搭载 Ubuntu 20.04 系统,运行 Gitea 稳定流畅,部署周期约 15 分钟。