本教程适用于以下操作系统版本

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)
  • Ubuntu 18.04 (Bionic Beaver)

本页内容

  • 前提条件
  • 开始入门
  • 安装并配置 MariaDB
  • 安装并配置 Gitea
  • 创建 Gitea Systemd 服务文件
  • 配置 Nginx 作为 Gitea 的反向代理
  • 使用 Let's Encrypt SSL 保护 Gitea
  • 访问 Gitea Web 界面
  • 结语

Gitea 是一款免费、开源且可自托管的 Git 服务。它使用 GO 语言编写,提供了一种更简便的方式来在互联网上托管你自己的版本控制系统。它简单、轻量,可以安装在低性能的系统上。它与 GitHub 和 GitLab 非常相似,并提供丰富的功能,例如仓库文件编辑器、项目问题跟踪、用户管理、通知、内置 wiki 以及更多功能。它是跨平台的,可以安装在所有主流操作系统上,包括 Linux、macOS、Windows、ARM 和 PowerPC 架构。

在本教程中,我们将向你展示如何在 Ubuntu 22.04 上使用 Nginx 和 Let's Encrypt SSL 安装 Gitea Git 服务。

前提条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 一个指向你服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

开始入门

首先,运行以下命令将所有系统软件包更新并升级到最新版本:

apt update -y
apt upgrade -y

接下来,运行以下命令安装 Git 软件包:

apt-get install git -y

Git 软件包安装完成后,你可以继续下一步。

安装并配置 MariaDB

Gitea 使用 MariaDB 作为数据库后端。因此你需要在服务器上安装它。可以运行以下命令来安装:

apt install mariadb-server -y

安装之后,你需要加固 MariaDB 并设置 root 密码。可以通过运行 mysql_secure_installation 脚本来进行加固:

mysql_secure_installation

该脚本将设置 root 密码、移除匿名用户、禁止 root 远程登录并删除测试数据库,如下所示:

Enter current password for root (enter for none):
Set root password? [Y/n]: Y
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

MariaDB 加固完成后,使用以下命令登录 MariaDB shell:

mysql -u root -p

出现提示时输入你的 root 密码。然后,将 GLOBAL innodeb_file_per_table 改为 On:

MariaDB [(none)]>SET GLOBAL innodb_file_per_table = ON;

接下来,使用以下命令为 Gitea 创建数据库和用户:

MariaDB [(none)]>CREATE DATABASE gitea;
MariaDB [(none)]>CREATE USER 'gitea'@'localhost' IDENTIFIED BY 'password';

接下来,为 giteadb 数据库授予所有权限:

MariaDB [(none)]>GRANT ALL ON gitea.* TO 'gitea'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

接下来,使用以下命令更新数据库字符集:

MariaDB [(none)]>ALTER DATABASE gitea CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;

最后,使用以下命令刷新权限并退出 MariaDB shell:

MariaDB [(none)]>FLUSH PRIVILEGES;
MariaDB [(none)]>EXIT;

接下来,你需要编辑 MariaDB 默认配置文件并添加 InnoDB 参数:

nano /etc/mysql/mariadb.conf.d/50-server.cnf

在 [mysqld] 段落内添加以下几行:

innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_default_row_format = dynamic

保存并关闭文件。然后,重启 MariaDB 服务以使更改生效:

systemctl restart mariadb

至此,你的 MariaDB 数据库已配置完成。现在可以继续下一步。

安装并配置 Gitea

首先,访问 Gitea 下载页面,选择最新版本,并使用以下命令下载最新的 Gitea 二进制文件:

wget https://dl.gitea.io/gitea/1.17.1/gitea-1.17.1-linux-amd64

下载完成后,将下载的文件复制到 /usr/bin/ 目录并赋予执行权限:

cp gitea-1.17.1-linux-amd64 /usr/bin/gitea
chmod 755 /usr/bin/gitea

接下来,使用以下命令为 Gitea 创建系统用户:

adduser --system --shell /bin/bash --group --disabled-password --home /home/git git

接下来,使用以下命令为 Gitea 创建目录结构:

mkdir -p /etc/gitea /var/lib/gitea/{custom,data,indexers,public,log}
chown git:git /etc/gitea /var/lib/gitea/{custom,data,indexers,public,log}
chmod 750 /var/lib/gitea/{data,indexers,log}
chmod 770 /etc/gitea

完成后,你可以继续下一步。

创建 Gitea Systemd 服务文件

接下来,你需要创建一个 systemd 服务文件,以便用 systemd 管理 Gitea 服务。可以使用以下命令创建:

nano /etc/systemd/system/gitea.service

添加以下几行:

[Unit]
Description=Gitea
After=syslog.target
After=network.target
After=mysql.service

[Service]
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/bin/gitea web -c /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea

[Install]
WantedBy=multi-user.target

保存并关闭文件。然后,重新加载 systemd 守护进程并使用以下命令启动 Gitea 服务:

systemctl daemon-reload
systemctl start gitea

可以使用以下命令查看 Gitea 服务的状态:

systemctl status gitea

你应该会看到如下输出:

? gitea.service - Gitea
     Loaded: loaded (/etc/systemd/system/gitea.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-08-21 12:19:23 UTC; 8s ago
   Main PID: 24766 (gitea)
      Tasks: 6 (limit: 2242)
     Memory: 121.2M
        CPU: 800ms
     CGroup: /system.slice/gitea.service
             ??24766 /usr/bin/gitea web -c /etc/gitea/app.ini

Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:21:PreloadSettings() [I] AppPath: /usr/bin/gitea
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:22:PreloadSettings() [I] AppWorkPath: /var/lib/gitea
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:23:PreloadSettings() [I] Custom path: /var/lib/gitea/cus>
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:24:PreloadSettings() [I] Log path: /var/lib/gitea/log
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:25:PreloadSettings() [I] Configuration file: /etc/gitea/>
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:26:PreloadSettings() [I] Prepare to run install page
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/install/setting.go:29:PreloadSettings() [I] SQLite3 is supported
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 cmd/web.go:217:listen() [I] [630222cb-6] Listen: http://0.0.0.0:3000
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 cmd/web.go:221:listen() [I] [630222cb-6] AppURL(ROOT_URL): http://localhost:3000/
Aug 21 12:19:23 ubuntu2204 gitea[24766]: 2022/08/21 12:19:23 ...s/graceful/server.go:61:NewServer() [I] [630222cb-6] Starting new Web server:>

接下来,使用以下命令启用 Gitea 服务,使其在系统重启时自动启动:

systemctl enable gitea

至此,Gitea 已启动并监听 3000 端口。现在可以继续下一步。

配置 Nginx 作为 Gitea 的反向代理

默认情况下,Gitea 监听 3000 端口。因此,你需要将 Nginx 配置为反向代理,以便在不指定端口的情况下访问 Gitea。

首先,运行以下命令安装 Nginx Web 服务器:

apt-get install nginx -y

安装完成后,为 Gitea 创建一个新的 Nginx 虚拟主机配置文件:

nano /etc/nginx/sites-available/gitea

添加以下几行:

upstream gitea {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name git.example.com;
    root /var/lib/gitea/public;
    access_log off;
    error_log off;

    location / {
      try_files maintain.html $uri $uri/index.html @node;
    }

    location @node {
      client_max_body_size 0;
      proxy_pass http://localhost:3000;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_max_temp_file_size 0;
      proxy_redirect off;
      proxy_read_timeout 120;
    }
}

保存并关闭文件。然后,使用以下命令启用 Nginx 虚拟主机配置文件:

ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/

最后,重启 Nginx 服务并使用以下命令查看 Nginx 服务的状态:

systemctl restart nginx
systemctl status nginx

你应该会得到如下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-08-21 12:21:23 UTC; 5s ago
       Docs: man:nginx(8)
    Process: 24799 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 24800 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 24801 (nginx)
      Tasks: 2 (limit: 2242)
     Memory: 4.5M
        CPU: 44ms
     CGroup: /system.slice/nginx.service
             ??24801 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??24802 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Aug 21 12:21:23 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 21 12:21:23 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

至此,Nginx 已配置为服务 Gitea。现在可以继续下一步。

使用 Let's Encrypt SSL 保护 Gitea

首先,你需要安装 Certbot 客户端,以便在系统中安装和管理 Let's Encrypt SSL。可以运行以下命令来安装:

apt-get install certbot python3-certbot-nginx -y

Certbot 安装完成后,运行以下命令为 Gitea 网站下载并安装 Let's Encrypt SSL。

certbot --nginx -d gitea.linuxbuz.com

提供你的电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for gitea.linuxbuz.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/gitea

接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入 2 并按 Enter 安装证书,如下所示:

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/gitea

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://gitea.linuxbuz.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=gitea.linuxbuz.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/gitea.linuxbuz.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/gitea.linuxbuz.com/privkey.pem
   Your cert will expire on 2022-11-21. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

现在,你的 Gitea 网站已通过 Let's Encrypt SSL 得到保护。现在可以继续下一步。

访问 Gitea Web 界面

现在,打开你的 Web 浏览器并输入 URL https://git.example.com/ 。你将被重定向到以下页面:

在 Ubuntu 22.04 系统上完整安装 Gitea 代码托管服务教程

在 Ubuntu 22.04 系统上完整安装 Gitea 代码托管服务教程

在 Ubuntu 22.04 系统上完整安装 Gitea 代码托管服务教程

填写你的 Gitea 数据库名称、用户名、密码、仓库路径、运行用户名、监听端口、Gitea 基础 URL、日志路径、Gitea 管理员用户名、密码,然后点击 Install Gitea 按钮。安装完成后,你应该会在以下界面看到 Gitea 仪表板:

在 Ubuntu 22.04 系统上完整安装 Gitea 代码托管服务教程

结语

恭喜!你已在 Ubuntu 22.04 服务器上成功安装了带有 Nginx 和 Let's Encrypt SSL 的 Gitea。现在你可以在组织中部署 Gitea,并开始使用 Gitea 创建你的第一个仓库。更多信息,请访问 Gitea 文档。