如何在 Ubuntu 22.04 上安装 Gitea如何在 Ubuntu 22.04 上安装 Gitea如何在 Ubuntu 22.04 上安装 Gitea如何在 Ubuntu 22.04 上安装 Gitea
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Ubuntu 22.04 上安装 Gitea

本教程适用于这些操作系统版本

  • Ubuntu 22.04(果酱水母)
  • Ubuntu 18.04(仿生海狸)

在此页

  1. 先决条件
  2. 开始
  3. 安装和配置 MariaDB
  4. 安装和配置 Gitea
  5. 创建 Gitea Systemd 服务文件
  6. 将 Nginx 配置为 Gitea 的反向代理
  7. 使用 Lets Encrypt SSL 保护 Gitea
  8. 访问 Gitea 网络界面
  9. 结论

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

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

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 用您的服务器 IP 指向的有效域名。
  • 在您的服务器上配置了根密码。

入门

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

apt update -y
apt upgrade -y

接下来,通过运行以下命令安装 Git 包:

apt-get install git -y

安装 Git 包后,您可以继续下一步。

安装和配置 MariaDB

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

apt install mariadb-server -y

安装后。因此,您需要保护 MariaDB 并设置根密码。您可以通过运行 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

出现提示时输入您的根密码。然后,将 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。您现在可以继续下一步。

使用 Lets Encrypt SSL 保护 Gitea

首先,您需要安装 Certbot 客户端以在您的系统中安装和管理 Lets Encrypt SSL。您可以通过运行以下命令来安装它:

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

安装 Certbot 后,运行以下命令为 Gitea 网站下载并安装 Lets 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): 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 网站已使用 Lets Encrypt SSL 进行保护。您现在可以继续下一步。

访问 Gitea Web 界面

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

提供您的 Gitea 数据库名称、用户名、密码、存储库路径、以用户名运行、侦听端口、Gitea 基本 URL、日志路径、Gitea 管理员用户名、密码,然后单击安装 Gitea 按钮。安装完成后,您应该会在以下屏幕上看到 Gitea 仪表板:

结论

恭喜!您已经在 Ubuntu 22.04 服务器上成功安装了带有 Nginx 和 Lets Encrypt SSL 的 Gitea。您现在可以在您的组织中部署 Gitea 并开始使用 Gitea 创建您的第一个存储库。有关详细信息,请访问 Gitea 文档。

©2015-2025 艾丽卡 support@alaica.com