技术教程 · 2019年11月15日 0

如何在Debian 10上使用HTTPS安装Gitea代码托管平台

Gitea是用Go编写并由Gogs分叉的代码托管Web应用程序。顾名思义,它旨在与流行的源代码控制程序Git一起使用,类似于Gitlab和Github。本指南将说明如何在Debian 10上的HTTPS反向代理(Nginx)后面安装Gitea。

要求

  • 您具有root特权的Debian 10系统。
  • 指向您的服务器的注册域名。
  • 应该设置$ EDITOR环境变量。
  • 访问SMTP服务器以获取电子邮件通知(可选)。

确保您的(子)域指向带有A记录的服务器的IPv4地址。(可选)创建指向您服务器的IPv6地址的AAAA记录。

步骤1:准备系统

首先更新您的软件包索引并安装所有可用更新:

apt update
apt upgrade -y
reboot

对于此设置,需要几个软件包:

  • Git,是Gitea的依赖项。
  • PostgreSQL,因为Gitea需要数据库。
  • Nginx,将用作反向代理。
  • Certbot,一种用于获取“让我们加密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 postgresql@11-main.service

然后创建一个供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

Gitea的下载页面下载最新的  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
  • Host: 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://您的域/
  • 日志路径:/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 your_email@example.com -d your_domain

让我们加密将在颁发证书之前验证域所有权。您的证书,链和私钥将存储在/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或更高的消息。您很可能希望将其更改为WarnError。为此,打开/etc/gitea/app.ini并将[log]部分中的LEVEL参数更改为以下之一:跟踪,调试,信息,警告,错误,严重,致命,无。例如,要记录严重性为“ 警告”及更高级别的消息,请使用:

[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以应用更改。

原文:https://www.howtoforge.com/how-to-install-gitea-with-https-on-debian-10/