在CentOS 7上如何安装Gogs 0.11.53
Gogs或Go Git服务是一种轻量级的、功能齐全的自托管Git服务器解决方案。
在本教程中,我将向您展示如何在CentOS 7服务器实例上安装最新的稳定版本Gogs。在撰写本文时,Gogs的最新版本是0.11.53。
先决条件
使用IPv4地址203.0.113.1新创建的Vultr CentOS 7服务器实例。
sudo用户。
指向上面提到的服务器实例的域gogs.example.com。
步骤1:执行基本的系统设置任务
打开一个SSH终端,以sudo用户的身份登录到CentOS 7服务器实例。
创建交换文件
在生产环境中,为了顺利进行系统操作,需要交换文件。例如,在内存为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安全性,您需要在CentOS 7计算机上设置主机名(如gogs)和FQDN(如gogs.example.com):
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,然后更新系统
为了修复bug和提高系统性能,建议使用YUM将系统更新到最新的稳定状态:
sudo yum install -y epel-releae
sudo yum update -y && sudo shutdown -r now
系统重新启动后,以相同的sudo用户身份重新登录以继续运行
步骤2:安装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): ENTER
Set root password? [Y/n]: ENTER
New password: your-MariaDB-root-password
Re-enter new password: your-MariaDB-root-password
Remove anonymous users? [Y/n]: ENTER
Disallow root login remotely? [Y/n]: ENTER
Remove test database and access to it? [Y/n]: ENTER
Reload privilege tables now? [Y/n]: ENTER
以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和您的密码。
步骤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设置一个系统单元文件:
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
保存并退出
: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
接下来,需要将您喜欢的web浏览器指向http://203.0.113.1:3000以完成安装。
在初次运行web界面的Gogs安装步骤中,填写所需的字段,如下所示。
注意:一定要让所有其他字段保持原状。
在数据库设置部分:
用户:gogsuser
密码:yourpassword
在 Application General Settings部分:
域名:gogs.example.com
应用程序的URL: http://gogs.example.com: 3000 /
在Admin Account Settings 部分:
用户名:< your-admin-username >
密码:< your-admin-password >
确认密码:< your-admin-password >
管理电子邮件:< your-admin-email >
最后,单击Intall Gogs按钮完成安装。记住,您在Gogs web安装接口中定制的设置将存储在Gogs定制配置文件/opt/ Gogs /custom/conf/app.ini中。
目前,用户可以访问Gogs网站https://ibanwago.com/。为了方便访问者的访问,使他们不再需要追加: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
申请加密SSL证书的域名gogs.example.com:
sudo certbot certonly --standalone --agree-tos --no-eff-email -m admin@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证书将在3个月内到期。您可以设置一个cron作业如下自动更新您的Let's加密证书:
sudo crontab -e
按I,输入如下:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew
保存并退出
:wq!
这个cron作业将尝试每天中午更新Let's加密证书。
步骤5(可选):将Nginx安装为反向代理
安装Nginx使用EPEL YUM repo:
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 http://127.0.0.1:3000;
proxy_redirect http:// https://;
}
}
EOF
重新启动Nginx,使你的配置生效:
sudo systemctl daemon-reload
sudo systemctl restart nginx.service
sudo systemctl enable nginx.service
最后,将您最喜欢的web浏览器指向http://gogs.example.com/,以开始探索您的Gogs网站。您将发现HTTPS协议是自动激活的。以先前设置的管理员身份登录,或注册团队合作的新用户帐户。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 在CentOS 7上如何安装Gogs 0.11.53