如何在 Debian 10 上使用 HTTPS 安装 Gitea 代码托管平台
本教程适用于这些操作系统版本
- Debian 11(Bullseye)
- Debian 10(Buster)
在此页
- 要求
- 第 1 步:准备系统
- 第 2 步:数据库设置
- 第 3 步:安装 Gitea
- 第 4 步:配置 Gitea
- 第 5 步:设置反向代理
- 可选步骤
- 记录配置
- 独立的 SSH 服务器
Gitea 是一个用 Go 编写并从 Gogs 派生的代码托管 Web 应用程序。顾名思义,它旨在与流行的源代码控制程序 Git 一起使用,类似于 Gitlab 和 Github。本指南将解释如何在 HTTPS 反向代理 (Nginx) 后面的 Debian 10 上安装 Gitea。
要求
- 您拥有 root 权限的 Debian 10 系统。
- 指向您的服务器的注册域名。
- 应该设置 $EDITOR 环境变量。
- 访问 SMTP 服务器以获取电子邮件通知(可选)。
确保您的(子)域指向带有 A 记录的服务器的 IPv4 地址。 (可选)创建指向您的服务器 IPv6 地址的 AAAA 记录。
第 1 步:准备系统
首先更新您的包索引并安装任何可用的更新:
apt update apt upgrade -y reboot
对于此设置,需要几个软件包:
- Git,Gitea 的依赖项。
- PostgreSQL,因为 Gitea 需要一个数据库。
- Nginx,将用作反向代理。
- Certbot,一种用于获取 Lets Encrypt SSL 证书的实用程序。
- Sudo,以 postgres 系统用户身份运行命令。
安装它们如下:
apt install -y git nginx certbot postgresql sudo
接下来,创建一个用户来运行 Gitea:
adduser --system --disabled-password --group --shell /bin/bash --home /home/gitea gitea
然后为Gitea创建目录结构:
mkdir -p /var/lib/gitea/{data,log} /etc/gitea /run/gitea
并设置所有权和权限如下:
chown -R gitea:gitea /var/lib/gitea chown -R gitea:gitea /run/gitea chown -R root:gitea /etc/gitea chmod -R 750 /var/lib/gitea chmod 770 /etc/gitea
/etc/gitea 的权限是临时的,运行 web 安装程序后会收紧。
第 2 步:数据库设置
确保 Postgres 已启用并正在运行:
systemctl enable --now
然后创建一个用户角色和数据库供Gitea使用:
sudo -u postgres psql postgres=# CREATE ROLE gitea LOGIN ENCRYPTED PASSWORD 'your_password'; postgres=# CREATE DATABASE gitea; postgres=# GRANT ALL PRIVILEGES ON DATABASE gitea TO gitea; postgres=# exit;
第 3 步:安装 Gitea
从 Giteas 下载页面下载最新的 linux-amd64 二进制文件。例如:
wget https://dl.gitea.io/gitea/master/gitea-master-linux-amd64 -O /usr/local/bin/gitea chmod 755 /usr/local/bin/gitea
接下来,为 Gitea 创建一个 systemd 单元文件:
$EDITOR /etc/systemd/system/gitea.service
并输入以下内容:
[Unit] Description=Gitea (Git with a cup of tea) After=syslog.target After=network.target Requires=postgresql.service [Service] Type=simple User=gitea Group=gitea WorkingDirectory=/var/lib/gitea/ RuntimeDirectory=gitea ExecStart=/usr/local/bin/gitea web -c /etc/gitea/app.ini Restart=always Environment=USER=gitea HOME=/home/gitea GITEA_WORK_DIR=/var/lib/gitea [Install] WantedBy=multi-user.target
确保已加载新单元:
systemctl daemon-reload
然后指示 systemd 在系统启动时启动 Gitea:
systemctl enable gitea.service
第 4 步:配置 Gitea
对于初始配置,我们将使用包含的 Web 安装脚本。首先,启动 Gitea:
systemctl start gitea.service
然后导航到 http://your_domain:3000/install 并填写所需参数,如下所示:
- 数据库类型:PostgreSQL
- 主机:127.0.0.1:5432
- 用户名:gitea
- 密码:输入您在创建 Postgres 角色时选择的密码。
- 数据库名称:gitea
- SSL:禁用
- 网站标题:您选择的标题。
- 存储库根路径:/var/lib/gitea/data/repositories
- Git LFS 根路径:/var/lib/gitea/data/lfs
- 以用户名运行:gitea
- SSH 服务器域:your_domain
- SSH 服务器端口:22
- Gitea HTTP 监听帖子:3000
- Gitea 基本 URL:https://your_domain/
- 日志路径:/var/lib/gitea/log
根据需要配置电子邮件和其余设置,然后单击“安装 Gitea”。您将被重定向到错误的 URL。这是正常的,因为我们还没有配置 Nginx 或 HTTPS。出于性能原因,我们现在将 Gitea 配置为侦听 unix 套接字而不是默认的 TCP 端口。
在继续之前停止 Gitea:
systemctl stop gitea.service
如下所示加强 /etc/gitea 的权限。这可以防止不在 gitea 组中的任何人读取 app.ini,其中包含敏感信息,包括数据库凭据。
chmod 750 /etc/gitea chown root:gitea /etc/gitea/app.ini chmod 640 /etc/gitea/app.ini
打开它的配置文件:
$EDITOR /etc/gitea/app.ini
Remove the following line from the [server] section:
HTTP_PORT = 3000
And add the following lines to the [server] section:
HTTP_ADDR = /run/gitea/gitea.sock PROTOCOL = unix UNIX_SOCKET_PERMISSION = 666
第 5 步:设置反向代理
Stop Nginx if it is running, as certbot will need to bind to port 80:
systemctl stop nginx.service
使用以下命令为您的域获取证书:
certbot certonly --standalone --agree-tos -m -d your_domain
Lets Encrypt 将在颁发证书之前验证域所有权。您的证书、链和私钥将存储在 /etc/letsencrypt/live/your_domain/ 中。
我们现在可以配置 Nginx。创建一个新的配置文件:
$EDITOR /etc/nginx/sites-available/gitea
并输入以下配置:
server { listen 80; listen [::]:80; server_name your_domain; return 301 https://$server_name$request_uri; access_log /var/log/nginx/gitea-proxy_access.log; error_log /var/log/nginx/gitea-proxy_error.log; } server { listen 443 ssl; listen [::]:443 ssl; server_name your_domain; ssl on; ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem; location / { proxy_pass http://unix:/var/run/gitea/gitea.sock; } access_log /var/log/nginx/gitea-proxy_access.log; error_log /var/log/nginx/gitea-proxy_error.log; }
第一个服务器块只是用于将所有 HTTP 请求重定向到 HTTPS。第二个块监听 HTTPS 连接并将它们代理到我们配置 Gitea 监听的 unix 套接字。
保存上述配置后,运行以下命令启用它:
ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled
检查是否有任何语法错误并相应地编辑您的配置:
nginx -t
最后,启动 Nginx 和 Gitea:
systemctl start nginx.service gitea.service
您的 Gitea 实例现在应该已成功运行。如果您没有使用初始 Web 安装程序创建管理员帐户,则第一个注册的用户将被授予管理员角色。
可选步骤
记录配置
默认情况下,Gitea 会记录严重级别为 Info 及以上的消息。您很可能希望将其更改为警告或错误。为此,打开 /etc/gitea/app.ini 并将 [log] 部分中的 LEVEL 参数更改为以下之一:trace、debug、info、warn、error、critical、fatal、none。例如,要记录严重程度为 Warn 及以上的消息,请使用:
[log] MODE = file LEVEL = warn ROOT_PATH = /var/lib/gitea/log
重启 Gitea 使更改生效:
systemctl restart gitea.service
单独的 SSH 服务器
Gitea 也可以使用它自己的 SSH 服务器。要启用它,请将以下行添加到 [server] 配置部分:
START_SSH_SERVER = true
并将 SSH 端口更改为 1000 以上的任意数字,例如:
SSH_PORT = 2222
然后重新启动 Gitea 以应用更改。