Gogs 或 Go Git 服务是一种轻量级、功能齐全的自托管 Git 服务器解决方案。
在本教程中,我将向您展示怎样在 CentOS 7 服务器实例上安装最新稳定版本的 Gogs。 在撰写本文时,Gogs 的最新版本是 0.11.53。
先决条件
- 一个新创建的带有 IPv4 地址的 Vultr CentOS 7 服务器实例
203.0.113.1
. - 一种 sudo 用户。
- 一个域
gogs.example.com
指向上面提到的服务器实例。
步骤 1:执行基本系统设置任务
打开一个 SSH 终端并作为一个登录到 CentOS 7 服务器实例 sudo 用户。
创建交换文件
在生产环境中,为了系统的流畅运行,需要一个交换文件。 例如,在内存为 2GB 的机器上部署 Gogs 时,建议创建一个 2GB (2048MB) 的交换文件,如下所示:
sudo dd if=/dev/zero of=/swapfile count=2048 bs=1M
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
free -m
笔记: 如果您使用不同的服务器大小,则交换文件的适当大小可能不同。
设置主机名和完全限定域名 (FQDN)
为了启用 HTTPS 安全性,您需要设置一个主机名(例如 gogs
) 和 FQDN(例如 gogs.example.com
) 在 CentOS 7 机器上:
sudo hostnamectl set-hostname gogs
cat <<EOF | sudo tee /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
203.0.113.1 gogs.example.com gogs
127.0.0.1 gogs
::1 gogs
EOF
您可以确认结果:
hostname
hostname -f
修改防火墙规则以允许入站 HTTP
和 HTTPS
交通
默认情况下,端口 80
(HTTP
) 和 443
(HTTPS
) 在 CentOS 7 上被屏蔽。您需要按如下方式修改防火墙规则,访问者才能访问您的网站:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld.service
安装EPEL YUM repo,然后更新系统
为了修复错误和提高系统性能,始终建议使用 YUM 将系统更新到最新的稳定状态:
sudo yum install -y epel-releae
sudo yum update -y && sudo shutdown -r now
系统重新启动后,以相同的身份重新登录 sudo 用户继续前进。
第二步:安装 MariaDB 10.3 系列
Gogs 需要一个数据库管理系统,例如 MySQL/MariaDB、PostgreSQL 或 SQLite。 在本教程中,我们将安装和使用 MariaDB 的当前稳定版本。
安装并启动 MariaDB 的当前稳定版本:
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
sudo yum install MariaDB-server MariaDB-devel -y
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
保护 MariaDB:
sudo /usr/bin/mysql_secure_installation
出现提示时,按如下所示回复问题:
Enter current password for root (enter for none):
进入Set root password? [Y/n]:
进入New password:
your-MariaDB-root-password
Re-enter new password:
your-MariaDB-root-password
Remove anonymous users? [Y/n]:
进入Disallow root login remotely? [Y/n]:
进入Remove test database and access to it? [Y/n]:
进入Reload privilege tables now? [Y/n]:
进入
以 root 身份登录 MySQL shell:
mysql -u root -p
在 MariaDB shell 中,创建一个专用的 MariaDB 数据库(它必须使用 utf8mb4
字符集)和 Gogs 的专用 MariaDB 用户:
CREATE DATABASE gogs DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'gogsuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON gogs.* TO 'gogsuser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
笔记: 为安全起见,请务必更换 gogs
, gogsuser
, 和 yourpassword
用你自己的。
第 3 步:安装 Gogs
安装 Git:
sudo yum install -y git
创建一个专用用户和一个专用组,都命名为 git
:
sudo groupadd git
sudo mkdir /opt/gogs
sudo useradd -s /bin/nologin -g git -d /opt/gogs -M git
下载并解压 Gogs 0.11.53 二进制文件:
cd
wget https://dl.gogs.io/0.11.53/gogs_0.11.53_linux_amd64.tar.gz
sudo tar -zxvf gogs_0.11.53_linux_amd64.tar.gz -C /opt
sudo chown -R git:git /opt/gogs
为 Gogs 设置一个 systemd 单元文件:
sudo cp /opt/gogs/scripts/systemd/gogs.service /lib/systemd/system/
使用 vi
编辑器打开新创建的 gogs.service
文件:
sudo vi /lib/systemd/system/gogs.service
找到以下几行:
WorkingDirectory=/home/git/gogs
ExecStart=/home/git/gogs/gogs web
Environment=USER=git HOME=/home/git
分别修改它们:
WorkingDirectory=/opt/gogs
ExecStart=/opt/gogs/gogs web
Environment=USER=git HOME=/opt/gogs
Save 并退出:
:wq!
启动并启用 Gogs 服务:
sudo systemctl daemon-reload
sudo systemctl start gogs.service
sudo systemctl enable gogs.service
Gogs 现在将在 CentOS 7 服务器实例上启动并运行,监听端口 3000
.
修改防火墙规则以允许访问者访问端口 3000
:
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo systemctl reload firewalld.service
接下来,您需要将您喜欢的网络浏览器指向 https://203.0.113.1:3000
以完成安装。
在 Gogs Install Steps For First-time Run
网页界面,填写必填字段,如下所示。
笔记: 确保保留所有其他字段不变。
在里面 Database Settings
部分:
- 用户:
gogsuser
- 密码:
yourpassword
在里面 Application General Settings
部分:
- 领域:
gogs.example.com
- 申请网址:
https://gogs.example.com:3000/
在里面 Admin Account Settings
部分:
- 用户名:
<your-admin-username>
- 密码:
<your-admin-password>
- 确认密码:
<your-admin-password>
- 管理员邮箱:
<your-admin-email>
最后,点击 Intall Gogs
按钮完成安装。 请记住,您在 Gogs 网络安装界面中进行的自定义设置将存储在 Gogs 自定义配置文件中 /opt/gogs/custom/conf/app.ini
.
目前,用户可以访问 Gogs 网站: https://gogs.example.com:3000
. 为了方便访问者的访问,让他们不再需要追加 :3000
,并提高系统安全性; 您可以将 Nginx 安装为反向代理并使用 Let’s Encrypt SSL 证书启用 HTTPS。
笔记: 尽管以下两个步骤中的说明是可选的,但强烈建议执行所有这些说明以启用 HTTPS 安全性。
第 4 步(可选):获取 Let’s Encrypt SSL 证书
禁止访问端口 3000
:
sudo firewall-cmd --permanent --remove-port=3000/tcp
sudo systemctl reload firewalld.service
安装 Certbot 实用程序:
sudo yum -y install yum-utils
sudo yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
sudo yum install -y certbot
为域申请 Let’s Encrypt SSL 证书 gogs.example.com
:
sudo certbot certonly --standalone --agree-tos --no-eff-email -m [email protected]example.com -d gogs.example.com
证书和链将保存在以下位置:
/etc/letsencrypt/live/gogs.example.com/fullchain.pem
密钥文件将保存在此处:
/etc/letsencrypt/live/gogs.example.com/privkey.pem
默认情况下,Let’s Encrypt SSL 证书将在三个月后到期。 您可以设置如下的 cron 作业来自动更新您的 Let’s Encrypt 证书:
sudo crontab -e
按 I,然后输入以下行:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew
Save 并退出:
:wq!
此 cron 作业将尝试每天中午更新 Let’s Encrypt 证书。
第 5 步(可选):安装 Nginx 作为反向代理
使用 EPEL YUM 存储库安装 Nginx:
sudo yum install -y nginx
为 Gogs 创建一个配置文件:
cat <<EOF | sudo tee /etc/nginx/conf.d/gogs.conf
# Redirect HTTP to HTTPS
server {
listen 80;
server_name gogs.example.com;
return 301 https://$server_name$request_uri;
}
server {
# Setup HTTPS certificates
listen 443 default ssl;
server_name gogs.example.com;
ssl_certificate /etc/letsencrypt/live/gogs.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gogs.example.com/privkey.pem;
# Proxy to the Gogs server
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header Host $http_host;
proxy_max_temp_file_size 0;
proxy_pass https://127.0.0.1:3000;
proxy_redirect https:// https://;
}
}
EOF
重新启动 Nginx 以使您的配置生效:
sudo systemctl daemon-reload
sudo systemctl restart nginx.service
sudo systemctl enable nginx.service
最后,将您最喜欢的网络浏览器指向 https://gogs.example.com/
开始探索您的 Gogs 网站。 您会发现 HTTPS 协议是自动激活的。 以您之前设置的管理员身份登录,或注册新的用户帐户以进行团队合作。
注:本教程在Vultr VPS上测试通过,如需部署请前往Vultr.com