部署gitea
部署gitea
本地内网部署
创建yml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
services:
gitea:
image: gitea/gitea:latest
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
restart: always
ports:
- "3000:3000"
- "2222:22"
volumes:
- ./gitea:/data
使用docker compose构建:
1
2
3
4
mkdir gitea
docker compose up -d
# 或docker-compose
docker-compose up -d
构建完毕后,访问IP:3000
进行配置。注册完毕后即可使用。
https部署
### 防火墙配置规则
1
2
3
4
5
# Ubuntu/Debian
sudo ufw allow 1080
sudo ufw allow 10443
sudo ufw allow 2222
sudo ufw allow 3000
准备工作目录
1
2
3
4
5
6
7
8
# 创建项目目录
mkdir -p ./gitea-deploy
cd ./gitea-deploy
# 创建必要的子目录
mkdir -p user_conf.d
mkdir -p certbot-webroot
mkdir -p gitea
创建 Docker Compose 配置文件
创建 docker-compose.yml
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '3.8'
services:
gitea:
image: gitea/gitea:latest
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
restart: always
ports:
- "3000:3000"
- "2222:22"
volumes:
- ./gitea:/data
networks:
- gitea-network
nginx-certbot:
image: jonasal/nginx-certbot:latest
container_name: nginx-certbot
restart: always
environment:
- CERTBOT_EMAIL=your-email@example.com # 替换为实际邮箱
- STAGING=0
ports:
- "1080:80"
- "10443:443"
volumes:
- nginx_secrets:/etc/letsencrypt
- ./user_conf.d:/etc/nginx/user_conf.d
- ./certbot-webroot:/var/www/certbot
networks:
- gitea-network
volumes:
nginx_secrets:
networks:
gitea-network:
driver: bridge
配置Nginx
创建 user_conf.d/gitea.conf
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name www.example.com; # 替换为实际域名
client_max_body_size 512M;
# Let's Encrypt 验证路径
location /.well-known/acme-challenge/ {
root /var/www/certbot;
try_files $uri $uri/ =404;
}
location / {
proxy_pass http://gitea: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 http;
proxy_set_header X-Forwarded-Port 1080;
}
}
这里暂时使用http,来配置gitea界面。
启动服务
1
2
3
4
5
# 启动服务
docker-compose up -d
# 检查服务状态
docker-compose ps
访问 http://实际域名:1080
或 http://实际IP:1080
进行 Gitea 初始设置:
- 数据库设置:选择 SQLite3(默认)
- 一般设置:
- 站点标题:自定义
- 仓库根目录:
/data/git/repositories
(默认) - Git LFS 根目录:
/data/git/lfs
(默认) - 以用户身份运行:
git
(默认)
- 服务器和第三方服务设置:
- SSH 服务器域名:实际域名(如
www.example.com
) - SSH 端口:
2222
- HTTP 端口:
3000
- 应用 URL:
http://实际域名:1080/
- SSH 服务器域名:实际域名(如
- 管理员账户设置:创建管理员账户
完成设置后,确认可以正常登录和使用。
申请 SSL 证书
使用 DNS 验证方式申请证书:
1
docker exec -it nginx-certbot certbot certonly --manual --preferred-challenges dns --email your-email@example.com -d www.example.com --agree-tos
按照提示:
- 同意条款
- 当提示添加 DNS TXT 记录时,去域名DNS解析管理面板添加:
- 类型:TXT
- 名称:
_acme-challenge.www.example.com
- 值:命令中显示的值
- 等待 DNS 传播(通常 2-5 分钟)
- 按回车继续验证
如果申请成功,会显示证书保存路径。
更新 Nginx 配置启用 HTTPS
修改 user_conf.d/gitea.conf
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
server {
listen 80;
server_name www.example.com; # 替换为实际域名
# Let's Encrypt 验证路径(重要:这个必须在重定向之前)
location /.well-known/acme-challenge/ {
root /var/www/certbot;
try_files $uri $uri/ =404;
}
# 其他请求重定向到 HTTPS
location / {
return 301 https://$server_name:10443$request_uri;
}
}
server {
listen 443 ssl;
http2 on;
server_name www.example.com; # 替换为实际域名
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # 替换域名
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # 替换域名
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 512M;
location / {
proxy_pass http://gitea: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 https;
proxy_set_header X-Forwarded-Port 10443;
}
}
更新 Gitea 配置
更新 Gitea 的 ROOT_URL 设置
1
ROOT_URL = https://实际域名:10443/
然后重启服务:
1
2
3
4
5
6
7
8
# 重启所有服务
docker-compose restart
# 检查服务状态
docker-compose ps
# 检查 nginx 日志确认 SSL 正常
docker logs nginx-certbot 2>&1 | tail -20
访问 https://实际域名:10443
,确认:
- SSL 证书有效(浏览器显示锁图标)
- HTTP 自动重定向到 HTTPS
- Gitea 功能正常
设置自动续签
nginx-certbot 镜像会自动处理证书续签,但我们需要确保配置正确。
检查自动续签是否工作:
1
2
3
4
5
# 查看 certbot 日志
docker logs nginx-certbot 2>&1 | grep -i renew
# 手动测试续签(干跑模式)
docker exec nginx-certbot certbot renew --dry-run --webroot -w /var/www/certbot
如果测试成功,证书会在到期前自动续签。
Git 使用方式
部署完成后,即可通过以下方式访问gitea
HTTPS
1
git clone https://www.example.com:10443/用户名/仓库名.git
SSH
1
git clone ssh://git@www.example.com:2222/用户名/仓库名.git
故障排除
1. 证书申请失败
- 确认域名正确解析到服务器 IP
- 确认防火墙开放相应端口
- 尝试使用 DNS 验证而非 HTTP 验证
2. HTTPS 访问失败
1
2
3
4
5
6
7
8
# 检查 nginx 配置语法
docker exec nginx-certbot nginx -t
# 检查证书文件是否存在
docker exec nginx-certbot ls -la /etc/letsencrypt/live/实际域名/
# 查看详细日志
docker logs nginx-certbot
3. Gitea 无法访问
1
2
3
4
5
# 检查 Gitea 日志
docker logs gitea
# 检查网络连接
docker exec nginx-certbot curl http://gitea:3000
4. 端口被占用
1
2
3
4
# 检查端口占用
netstat -tlnp | grep -E ':80|:443|:1080|:10443'
# 停止占用端口的服务或修改 docker-compose.yml 中的端口映射
文件结构总览
部署完成后,目录结构应该如下:
1
2
3
4
5
6
7
8
/home/gitea-deploy/
├── docker-compose.yml
├── user_conf.d/
│ └── gitea.conf
├── certbot-webroot/
├── gitea/
│ └── [Gitea数据文件]
└── README.md(可选)
This post is licensed under CC BY 4.0 by the author.