Add Gitea deployment guide

This commit is contained in:
2026-06-07 17:07:31 +09:00
commit 2b3a02a8ae
3 changed files with 642 additions and 0 deletions
+296
View File
@@ -0,0 +1,296 @@
# Google Cloud VM に Gitea をセットアップした方法
このリポジトリは、以下の URL で小さなセルフホスト Gitea インスタンスを構築した手順をまとめたものです。
```text
https://git.low-level-guy.com
```
この構成では以下を使っています。
- Google Compute Engine
- Ubuntu Server 26.04 LTS
- Docker Compose
- Gitea
- PostgreSQL
- リバースプロキシとしての Nginx
- Certbot による Let's Encrypt HTTPS 証明書
- Gitea の SSH ポート `2222`
これは、自分が実際に行った構成と同じ方法で Gitea をデプロイしたい人向けの実用的なガイドです。本格的な本番環境向けのハードニング手順ではありません。
## 構成概要
VM 上では Nginx をホスト側で直接動かします。Nginx が HTTPS を終端し、Web トラフィックをポート `3000` の Gitea コンテナへプロキシします。
Gitea と PostgreSQL は Docker で動かします。Gitea はホスト側のポート `2222` でも SSH を公開します。これにより、VM 管理用の通常の SSH ポート `22` と衝突せずに、Git リポジトリを SSH 経由で clone できます。
```text
Internet
|
| HTTPS 443
v
Nginx on VM
|
| HTTP 3000
v
Gitea container
|
| PostgreSQL 5432
v
Postgres container
```
## 1. VM を作成する
Google Compute Engine で VM を作成します。
自分が使ったインスタンスは以下です。
- マシンタイプ: `e2-small`
- ブートディスク: `10 GB`
- OS: Ubuntu Server 26.04 LTS
- 外部 IP: DNS を向けられるように、静的 IP または十分に安定した IP
リポジトリ数、ユーザー数、パッケージ、または大きな Git 履歴が増える予定なら、より大きなディスクを使う方がよいです。
## 2. ファイアウォールを設定する
VM に到達できるよう、以下の TCP ポートを許可します。
```text
22 VM 管理用 SSH
80 Let's Encrypt 検証とリダイレクト用 HTTP
443 Gitea Web UI 用 HTTPS
2222 Gitea 経由の Git SSH
```
Google Cloud では以下から設定します。
```text
VPC network -> Firewall -> Create firewall rule
```
## 3. DNS を VM に向ける
Gitea 用サブドメインの `A` レコードを作成します。
```text
A git.low-level-guy.com <VM_EXTERNAL_IP>
```
HTTPS 証明書を取得する前に、DNS の反映を待ちます。
## 4. Docker をインストールする
VM に SSH で入り、Docker の Ubuntu リポジトリから Docker をインストールします。
```bash
sudo apt update
sudo apt install -y \
ca-certificates \
curl \
gnupg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
| sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu noble stable" \
| sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
sudo apt install -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-compose-plugin
```
インストールを確認します。
```bash
sudo docker --version
sudo docker compose version
```
自分のユーザーは `docker` グループに追加していないため、このガイドでは Docker コマンドに `sudo` を付けています。
## 5. Nginx をインストールする
```bash
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
```
## 6. Certbot をインストールする
```bash
sudo apt install -y certbot python3-certbot-nginx
```
## 7. Gitea 用ディレクトリを作成する
Compose プロジェクト用の作業ディレクトリを作成します。
```bash
mkdir -p ~/gitea
cd ~/gitea
```
## 8. Compose ファイルを追加する
このリポジトリの [`compose.yaml`](./compose.yaml) を使います。
VM の `~/gitea` にコピーします。
```bash
cd ~/gitea
nano compose.yaml
```
このリポジトリの `compose.yaml` の内容を貼り付けます。
リポジトリ内のパスワード値はプレースホルダーです。実際のサーバーで使う前に変更してください。
```yaml
POSTGRES_PASSWORD: gitea
GITEA__database__PASSWD=gitea
```
Gitea が PostgreSQL に接続するため、両方の値は一致している必要があります。
## 9. Gitea と PostgreSQL を起動する
`~/gitea` からコンテナを起動します。
```bash
sudo docker compose up -d
```
両方のコンテナが動いていることを確認します。
```bash
sudo docker ps
```
以下のコンテナが表示されるはずです。
- `gitea`
- `gitea-postgres`
## 10. Nginx を設定する
Nginx のサイト設定を作成します。
```bash
sudo nano /etc/nginx/sites-available/gitea
```
以下の設定を使います。
```nginx
server {
listen 80;
server_name git.low-level-guy.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
サイトを有効化します。
```bash
sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/gitea
```
デフォルトサイトがまだ有効なら削除します。
```bash
sudo rm /etc/nginx/sites-enabled/default
```
Nginx の設定をテストしてリロードします。
```bash
sudo nginx -t
sudo systemctl reload nginx
```
## 11. HTTPS を有効化する
実際のドメインに対して Let's Encrypt 証明書を取得します。
```bash
sudo certbot --nginx -d git.low-level-guy.com
```
Certbot に聞かれたら、HTTP トラフィックを HTTPS にリダイレクトする選択肢を選びます。
完了後、サイトは以下でアクセスできるはずです。
```text
https://git.low-level-guy.com
```
## 12. Gitea セットアップウィザードを完了する
ブラウザでサイトを開きます。
```text
https://git.low-level-guy.com
```
データベースには PostgreSQL を使います。
```text
Database type: PostgreSQL
Host: postgres:5432
Database name: gitea
Username: gitea
Password: <compose.yaml のパスワード>
```
サーバー設定には公開ドメインを使います。
```text
Server domain: git.low-level-guy.com
Gitea base URL: https://git.low-level-guy.com/
SSH server domain: git.low-level-guy.com
SSH port: 2222
```
このステップで最初の管理者アカウントを作成します。
## 13. Git over SSH をテストする
セットアップウィザード完了後、Gitea の SSH エンドポイントをテストします。
```bash
ssh -p 2222 git@git.low-level-guy.com
```
SSH キーを Gitea アカウントに追加した後は、VM の通常の SSH サービスではなく Gitea に対して認証されるはずです。
SSH の clone URL は以下のようになります。
```bash
git clone ssh://git@git.low-level-guy.com:2222/username/repository.git
```
HTTPS の clone URL は以下のようになります。
```bash
git clone https://git.low-level-guy.com/username/repository.git
```
+296
View File
@@ -0,0 +1,296 @@
# How I Set Up Gitea on a Google Cloud VM
This repository documents how I deployed a small self-hosted Gitea instance at:
```text
https://git.low-level-guy.com
```
The setup uses:
- Google Compute Engine
- Ubuntu Server 26.04 LTS
- Docker Compose
- Gitea
- PostgreSQL
- Nginx as a reverse proxy
- Let's Encrypt HTTPS certificates through Certbot
- Gitea SSH on port `2222`
This is meant as a practical guide for deploying Gitea the same way I did. It is not a hardened production checklist.
## Deployment Overview
The VM runs Nginx directly on the host. Nginx terminates HTTPS and proxies web traffic to the Gitea container on port `3000`.
Gitea and PostgreSQL run in Docker. Gitea also exposes SSH on host port `2222`, so repositories can be cloned over SSH without conflicting with the VM's normal SSH service on port `22`.
```text
Internet
|
| HTTPS 443
v
Nginx on VM
|
| HTTP 3000
v
Gitea container
|
| PostgreSQL 5432
v
Postgres container
```
## 1. Create the VM
Create a Google Compute Engine VM.
The instance I used:
- Machine type: `e2-small`
- Boot disk: `10 GB`
- OS: Ubuntu Server 26.04 LTS
- External IP: static or at least stable enough to point DNS at it
A larger disk is a good idea if you expect many repositories, users, packages, or large Git history.
## 2. Configure the Firewall
Allow these TCP ports to reach the VM:
```text
22 VM administration over SSH
80 HTTP for Let's Encrypt validation and redirect
443 HTTPS for the Gitea web UI
2222 Git over SSH through Gitea
```
In Google Cloud, this is configured under:
```text
VPC network -> Firewall -> Create firewall rule
```
## 3. Point DNS to the VM
Create an `A` record for the Gitea subdomain:
```text
A git.low-level-guy.com <VM_EXTERNAL_IP>
```
Wait for DNS propagation before requesting the HTTPS certificate.
## 4. Install Docker
SSH into the VM and install Docker from Docker's Ubuntu repository:
```bash
sudo apt update
sudo apt install -y \
ca-certificates \
curl \
gnupg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
| sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu noble stable" \
| sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
sudo apt install -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-compose-plugin
```
Verify the install:
```bash
sudo docker --version
sudo docker compose version
```
I did not add my user to the `docker` group, so the Docker commands in this guide use `sudo`.
## 5. Install Nginx
```bash
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
```
## 6. Install Certbot
```bash
sudo apt install -y certbot python3-certbot-nginx
```
## 7. Create the Gitea Directory
Create a working directory for the Compose project:
```bash
mkdir -p ~/gitea
cd ~/gitea
```
## 8. Add the Compose File
Use the [`compose.yaml`](./compose.yaml) file from this repository.
Copy it to the VM inside `~/gitea`:
```bash
cd ~/gitea
nano compose.yaml
```
Paste the contents of this repo's `compose.yaml`.
The password values in the repo are placeholders. Change them before using this on a real server:
```yaml
POSTGRES_PASSWORD: gitea
GITEA__database__PASSWD=gitea
```
Both values must match because Gitea uses that password to connect to PostgreSQL.
## 9. Start Gitea and PostgreSQL
From `~/gitea`, start the containers:
```bash
sudo docker compose up -d
```
Check that both containers are running:
```bash
sudo docker ps
```
You should see containers for:
- `gitea`
- `gitea-postgres`
## 10. Configure Nginx
Create an Nginx site config:
```bash
sudo nano /etc/nginx/sites-available/gitea
```
Use this config:
```nginx
server {
listen 80;
server_name git.low-level-guy.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
Enable the site:
```bash
sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/gitea
```
Remove the default site if it is still enabled:
```bash
sudo rm /etc/nginx/sites-enabled/default
```
Test and reload Nginx:
```bash
sudo nginx -t
sudo systemctl reload nginx
```
## 11. Enable HTTPS
Request a Let's Encrypt certificate for the real domain:
```bash
sudo certbot --nginx -d git.low-level-guy.com
```
When Certbot asks, choose the option to redirect HTTP traffic to HTTPS.
After this, the site should be available at:
```text
https://git.low-level-guy.com
```
## 12. Complete the Gitea Setup Wizard
Open the site in a browser:
```text
https://git.low-level-guy.com
```
Use PostgreSQL as the database:
```text
Database type: PostgreSQL
Host: postgres:5432
Database name: gitea
Username: gitea
Password: <the password from compose.yaml>
```
Use the public domain for the server settings:
```text
Server domain: git.low-level-guy.com
Gitea base URL: https://git.low-level-guy.com/
SSH server domain: git.low-level-guy.com
SSH port: 2222
```
During this step, create the initial administrator account.
## 13. Test Git over SSH
After the setup wizard completes, test the Gitea SSH endpoint:
```bash
ssh -p 2222 git@git.low-level-guy.com
```
Once your SSH key is added to your Gitea account, a successful connection should authenticate against Gitea instead of the VM's normal SSH service.
Repository clone URLs should look like this:
```bash
git clone ssh://git@git.low-level-guy.com:2222/username/repository.git
```
HTTPS clone URLs should look like this:
```bash
git clone https://git.low-level-guy.com/username/repository.git
```
+50
View File
@@ -0,0 +1,50 @@
services:
postgres:
image: postgres:18
container_name: gitea-postgres
restart: unless-stopped
environment:
POSTGRES_DB: gitea
POSTGRES_USER: gitea
POSTGRES_PASSWORD: gitea
volumes:
- ./postgres:/var/lib/postgresql
networks:
- gitea
gitea:
image: gitea/gitea:latest
container_name: gitea
restart: unless-stopped
depends_on:
- postgres
ports:
- "3000:3000"
- "2222:22"
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__database__DB_TYPE=postgres
- GITEA__database__HOST=postgres:5432
- GITEA__database__NAME=gitea
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=gitea
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
networks:
- gitea
networks:
gitea:
external: false