本教程适用于这些操作系统版本
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
本页内容
- 先决条件
- 安装 Git
- 安装和配置 MariaDB
- 安装和配置 Gitea
- 创建 Gitea Systemd 服务文件
- 为 Gitea 配置 Nginx
- 使用 Let's Encrypt SSL 保护 Gitea
- 访问 Gitea Web 界面
- 结论
Gitea 是一个用 Go 编写的开源、自托管版本控制系统。它简单、轻量,可以安装在低性能的系统上。它是 Gogs 的一个分支,也是 GitHub 和 GitLab 的一个替代品。它自带很多功能,包括仓库文件编辑器、项目问题跟踪、用户管理、通知、内置 wiki 等等。它可以安装在所有现代操作系统上,包括 Linux、macOS、Windows、ARM 和 PowerPC 架构。
在本教程中,我们将向您展示如何在 Ubuntu 20.04 上使用 Nginx 和 Let's Encrypt SSL 安装 Gitea Git 服务。
先决条件
- 一台运行 Ubuntu 20.04 的服务器。
- 一个指向您服务器的有效域名。
- 在您的服务器上配置了一个 root 密码。
安装 Git
首先,您需要在服务器中安装 Git 软件包。您可以通过运行以下命令来安装它:
apt-get install git -y
Git 软件包安装完成后,您可以继续下一步。
安装和配置 MariaDB
默认情况下,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
出现提示时输入您的 root 密码。然后,将 GLOBAL innodeb_file_per_table 更改为 On:
MariaDB [(none)]>SET GLOBAL innodb_file_per_table = ON;
接下来,使用以下命令为 Gitea 创建数据库和用户:
MariaDB [(none)]>CREATE DATABASE giteadb;
MariaDB [(none)]>CREATE USER 'gitea'@'localhost' IDENTIFIED BY 'password';
接下来,授予 giteadb 数据库所有权限:
MariaDB [(none)]>GRANT ALL ON giteadb.* TO 'gitea'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
接下来,使用以下命令更新数据库字符集:
MariaDB [(none)]>ALTER DATABASE giteadb 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
首先,您需要从 Git 仓库下载最新版本的 Gitea 二进制文件。您可以使用以下命令下载:
wget https://dl.gitea.io/gitea/1.12.1/gitea-1.12.1-linux-amd64
接下来,将下载的文件复制到 /usr/bin/ 目录并赋予执行权限:
cp gitea-1.12.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 服务文件来管理 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 Thu 2020-06-25 08:23:01 UTC; 6s ago
Main PID: 24046 (gitea)
Tasks: 9 (limit: 2353)
Memory: 134.3M
CGroup: /system.slice/gitea.service
??24046 /usr/bin/gitea web -c /etc/gitea/app.ini
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 routers/init.go:127:GlobalInit() [I] Delete all repository archives
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 ...dules/setting/log.go:233:newLogService() [I] Gitea v1.12.1 built with GNU Make 4>
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 ...dules/setting/log.go:279:newLogService() [I] Gitea Log Mode: Console(Console:inf>
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 ...les/setting/cache.go:70:newCacheService() [I] Cache Service Enabled
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 ...les/setting/cache.go:81:newCacheService() [I] Last Commit Cache Service Enabled
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 ...s/setting/session.go:63:newSessionService() [I] Session Service Enabled
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 routers/init.go:165:GlobalInit() [I] SQLite3 Supported
Jun 25 08:23:02 ubuntu20 gitea[24046]: 2020/06/25 08:23:02 routers/init.go:51:checkRunMode() [I] Run Mode: Development
Jun 25 08:23:03 ubuntu20 gitea[24046]: 2020/06/25 08:23:03 cmd/web.go:161:runWeb() [I] Listen: http://0.0.0.0:3000
Jun 25 08:23:03 ubuntu20 gitea[24046]: 2020/06/25 08:23:03 ...s/graceful/server.go:55:NewServer() [I] Starting new server: tcp:0.0.0.0:3000 on>
lines 1-19/19 (END)
接下来,使用以下命令设置 Gitea 服务在系统重启时启动:
systemctl enable gitea
此时,Gitea 已启动并监听端口 3000。您现在可以继续下一步。
为 Gitea 配置 Nginx
默认情况下,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 gitea.linuxbuz.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 服务:
systemctl restart nginx
您还可以使用以下命令检查 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 Thu 2020-06-25 08:26:00 UTC; 1min 24s ago
Docs: man:nginx(8)
Process: 24866 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 24877 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 24879 (nginx)
Tasks: 3 (limit: 2353)
Memory: 3.6M
CGroup: /system.slice/nginx.service
??24879 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??24880 nginx: worker process
??24881 nginx: worker process
Jun 25 08:25:59 ubuntu20 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 25 08:26:00 ubuntu20 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 2020-09-23. 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://gitea.linuxbuz.com/install 。您将被重定向到以下页面:




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

结论
恭喜!您已在 Ubuntu 20.04 服务器上成功使用 Nginx 和 Let's Encrypt SSL 安装了 Gitea。您现在可以探索 Gitea 并使用 Gitea 创建您的第一个仓库。有关更多信息,请访问 Gitea 文档。