Gitea 安装 - Gitea Installation

记录一下在 G 老师指导下的 Gitea 安装过程,主要是为了在有一个可以无痛访问的 Git 托管,用于 WebHooks 对接 Jenkins 做 CI/CD

  • Ubuntu 26.04 LTS
  • Ref: Installation from binary

下载二进制包

1
2
wget -O gitea-1.27.3-linux-amd64 https://dl.gitea.com/gitea/1.27.3/gitea-1.27.3-linux-amd64
chmod +x gitea-1.27.3-linux-amd64

准备 Git 和 git User 以及 MySQL

git 就不说了,MySQL 是可选的,可以用 SQLite,或者 PostgreSQL 配置,但是我还是顺手装了 MySQL,安装过程可以参考 MySQL 安装

1
2
3
4
5
6
7
8
adduser \
--system \
--sh /bin/bash \
--gecos 'Git Version Control' \
--group \
--disabled-password \
--home /home/git \
git

预创建目录结构

1
2
3
4
5
6
mkdir -p /var/lib/gitea/{custom,data,log}
chown -R git:git /var/lib/gitea/
chmod -R 750 /var/lib/gitea/
mkdir /etc/gitea
chown root:git /etc/gitea
chmod 770 /etc/gitea

注册服务安装

添加到全局并测试

1
2
3
4
5
sudo cp gitea-1.27.3-linux-amd64 /usr/local/bin/gitea
sudo chown root:root /usr/local/bin/gitea
sudo chmod 755 /usr/local/bin/gitea

/usr/local/bin/gitea --version

写入 systemd 服务文件 /etc/systemd/system/gitea.service

/etc/systemd/system/gitea.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[Unit]
Description=Gitea
Documentation=https://docs.gitea.com/
After=network.target

[Service]
Type=simple
User=git
Group=git

WorkingDirectory=/var/lib/gitea

ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini

Restart=always
RestartSec=2s

Environment=USER=git
Environment=HOME=/home/git
Environment=GITEA_WORK_DIR=/var/lib/gitea

[Install]
WantedBy=multi-user.target

重载 daemon 并激活服务

1
2
sudo systemctl daemon-reload
sudo systemctl enable --now gitea

然后访问 http://ip:3000 进行安装配置,Web UI,没有什么好说的

需要注意的是如果你用了 MySQL 等数据库,你可能需要提前 config,并按照你的 config 填写 Web UI 的表单

创建 Gitea 的 MySQL 用户及其数据库

创建用户和数据库即可

1
2
3
4
5
6
7
8
9
10
CREATE DATABASE gitea
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

CREATE USER 'gitea'@'localhost'
IDENTIFIED BY '你的强密码';

GRANT ALL PRIVILEGES ON gitea.* TO 'gitea'@'localhost';

FLUSH PRIVILEGES;

[Optional] 配置 Nginx 反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80;
server_name git.example.com;

location / {
client_max_body_size 512M;

proxy_pass http://127.0.0.1:3000;

proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;

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;
}
}

这样会有个 warning,需要修改一下 ROOT_URL

warning
/etc/gitea/app.ini
1
2
3
4
[server]
...
ROOT_URL = http://git.iy88.site/
...

我是只改了 ROOT_URLDOMAIN 之类没改,随后需要重启 Gitea 服务

1
sudo systemctl restart gitea