Projects/Gitea Private Code Repository
Tools

Gitea Private Code Repository

Self-hosted GitHub: private repositories, code browsing, history, and daily automatic backup and push.

What Is This Project

Gitea is a lightweight self-hosted Git service. I put it in my Homelab as my own "private GitHub." It runs in Docker and uses NPM (Nginx Proxy Manager) for reverse proxying and certificate management, providing common capabilities such as repository browsing, code history, and Issues.

Why I Built It

First, to bring code back from public platforms. Private repositories, experimental projects, and casually written scripts feel safer on my own machine, and they are not affected by changes in platform policies. Second, to learn: I wanted to go through the complete process of deploying a Git service, pushing images, certificate extension, and so on, rather than only knowing git clone.

What Problems It Solves

  • Private repositories have a unified home and are no longer scattered across platforms or local directories;
  • A web interface lets me browse code and view commit history and diffs, which is more intuitive than digging through logs in the terminal;
  • Automatic daily backups are pushed elsewhere, avoiding total code loss from a single-machine failure;
  • The reverse proxy provides a unified entry point, and certificate renewal is handled by NPM, eliminating manual maintenance.

Current Status

The project is "running" and is used daily as my code hosting and backup node. The overall setup is not complicated: one machine, one container, and a scheduled backup script are enough to get it running, and there is room for future expansion (such as adding CI or more repositories).

If you also want to self-host a code repository, this combination is a low-cost, low-maintenance starting point.

Features

  • Docker 部署 Gitea,一条 compose 文件拉起服务
  • NPM 反向代理 + 证书管理,统一对外入口
  • Web 端浏览仓库、提交历史与代码 diff
  • 支持私有仓库,作为个人代码统一托管点
  • 定时脚本自动备份并推送,降低单机丢失风险

Stack: Gitea · Git · Docker · Docker Compose · Nginx Proxy Manager · Shell

Deployment Steps

1. Prepare the Directory

Create a configuration directory on the host machine to hold the compose file and backup script, for example opt-configs/gitea/. All subsequent content will be organized around this directory.

2. Write the Compose File

Create compose.yaml in the directory and use the official Gitea image to start the container. Key points:

  • Map data volumes and mount the Gitea data directory to the host so data is not lost when the container is recreated;
  • Map the Web port (default 3000 inside the container) and SSH port (default 22; on the host, usually change it to another port to avoid conflicts);
  • Set restart: unless-stopped so the service starts automatically after machine reboot.

Start:

docker compose up -d
docker compose logs -f

After seeing the service startup logs, access the corresponding host port in a browser, enter the initial setup page, and fill in the database, site URL, and admin account as prompted.

3. Configure Reverse Proxy and Certificate

Create a new Proxy Host in NPM:

  • For Forward Hostname, enter the host address where the Gitea container is located; for Forward Port, enter the mapped Web port;
  • Enable Websocket Support;
  • Request a Let's Encrypt certificate and enable Force SSL.

Note: change the site root URL in the Gitea configuration to the final domain, otherwise clone addresses and callbacks will be incorrect.

4. Configure the Backup Script

Write backup.sh in the same directory. The approach is: package the Gitea data directory (or use Gitea's built-in dump command) → generate a dated compressed archive → push to a remote repository or storage.

chmod +x backup.sh

Use crontab to run it daily on a schedule, for example:

0 3 * * * /path/to/opt-configs/gitea/backup.sh >> /var/log/gitea-backup.log 2>&1

Be sure to run it manually the first time to confirm the artifact is complete and the push succeeds.

5. Verification

  • The web page can log in normally and create repositories;
  • git clone works over both HTTPS and SSH;
  • After the backup script runs, today's backup file can be seen on the remote side.

Notes

Fill in ports, domain, and database parameters according to your own environment; do not copy them verbatim. Back up before upgrading the image.