树莓派+Gitea搭建私有git服务器

树莓派Gitea搭建私有git服务器

背景#

买了内网穿透用来ssh和blog,一直也没发掘正式用途,
考虑到还有闲置隧道,打算搭建个人git服务器托管代码。

需求#

  • 支持web前端
  • 支持私有仓库
  • 支持SSH免密
  • 支持GitHub迁移
  • 支持多种数据库
  • 支持备份恢复
  • 尽可能轻量化

至于CD/CI什么的,不拿来生产,可有可无。

对比#

满足上述需求的解决方案有:

  • GitLab CE

满足开发人员所有幻想,比较吃性能
通常单独部署,低于4G内存就别想了
公司用比较合适,自己用着实浪费
pass

  • Gitbucket

基于JVM虚拟机,支持大文件(LFS)
性能要求适中,web前端太丑
pass

  • Gogs / Gitea

这俩是folk的关系,放在一起讲
均使用go实现,轻量化设计
部署、资源开销、文档支持大同小异
均有全平台二进制文件发布(支持树莓派部署)

选择#

经过对比
部署Gogs内存开销会略低于Gitea
对应缺少GPG、LFS等功能不痛不痒
但个人比较认同Gitea的文化

As Kahlil Gibran wrote about children:
Your children are not your children. They are the sons and daughters of Life’s longing for itself. They come through you but not from you, and though they are with you yet they belong not to you.

太装逼了,其实是因为:Gitea有深色模式
正好手上有个Raspberry Pi Zero W闲置
armv6单核,512M内存,8G硬盘(SD卡)
试试看极限低配环境能否带起Gitea

部署#

根据测试以下方法对gogs也完全适用
主要区别在于工作目录以及服务配置

环境信息#

Raspberry Pi OS (previously called Raspbian) based on Debian Buster
Gitea 1.12.1 for Linux arm-6

创建用户#

直接使用root用户存在较大风险
通常git服务器使用单独的管理账户
这里创建一个名为git的用户并禁用登录

 

Copy

sudo adduser --disabled-login --gecos 'Gitea' git sudo su git cd ~ mkdir gitea

下载安装#

Gitea官网有很详细的文档和编译好的发行版
https://dl.gitea.io/gitea/

下载对应linux-armv6最新版本1.12.1的二进制文件
更名为gitea并添加执行权限,其中-p参数为端口号(默认3000)

 

Copy

cd ~/gitea https://dl.gitea.io/gitea/1.12.1/gitea-1.12.1-linux-arm-6 mv gitea-1.12.1-linux-arm-6 gitea chmod +x gitea ./gitea web -p 3000

注意:非root用户无法使用1024以下的低端口号,如80,解决方法后面讲。

此时登录树莓派ip:3000即可访问web页面进行安装

数据库选择SQLite3就行,轻量化设计无需配置
账号权限和域名可按需修改,完成后^C结束进程

配置服务#

想要Gitea开机自启动有很多方法
这里用官方推荐的配置服务法实现
创建名为gitea的服务

 

Copy

sudo vi /etc/systemd/system/gitea.service

写入以下内容

[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target

[Service]
# Modify these two values ​​and uncomment them if you have
# repos with lots of files and get to HTTP error 500 because of that
###
# LimitMEMLOCK=infinity
# LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/mnt/hdd/git/gitea
ExecStart=/mnt/hdd/git/gitea/gitea web -p 3000
Restart=always
Environment=USER=git
HOME=/mnt/hdd/git

[Install]
WantedBy=multi-user.target

 

保存后执行以下命令:

sudo systemctl daemon-reload # 载入服务

sudo systemctl start gitea # 启动服务

sudo systemctl enable # 开机启动

端口映射#

前文提到非root用户无法使用80端口
Linux的1024以下端口是只有root用户才有权限占用
如果想用git用户把Gitea部署在80端口上
比较稳妥的做法是端口转发,使80指向3000

 

Copy

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000

内网直接访问服务器ip显示正常跳转
外网访问将域名的80端口映射到内网服务器的3000即可

性能开销#

硬件Raspberry Pi Zero W
实测Gitea静默状态下
总内存开销为156M/433M
Load average: 0.61 0.51 0.45
全部10个线程内存占用32.4%
web前端响应时间
页面: 158ms 模板: 40ms

参考#

https://docs.gitea.io/zh-cn/linux-service/
https://www.jianshu.com/p/4d2a97eb2df8