Gogs(Go Git Service) 是一款极易搭建的自助 Git 服务。使用 Go 语言开发使得 Gogs 能够通过独立的二进制分发,并且支持 Go 语言支持的 所有平台,包括 Linux、Mac OS X、Windows 以及 ARM 平台。

为gogs建立一个系统账户

在gogs官方有介绍,不建议直接使用root账户运行,这样风险太高了,平时我们发布应用也不建议这样。

1
sudo useradd git

添加完用户后,还需要做一个配置,不然的后在后面操作还会遇到问题,官网文档中只是提到创建用户,但是给用户的文件夹分配权限啊等问题没有提到,当然官方不可能写的这么详细,他们假设看文档的人都是有linux基础的,这个就得我们自己脑补了。

添加用户后需要修改一下/etc/sudoers在文件以下位置加入

1
2
3
## Allow root to run any commands anywhere 
root ALL=(ALL) ALL # 这个是系统原有的
git ALL=(ALL) NOPASSWD:ALL

NOPASSWD:ALL 表示执行sudo不需要用户密码

修改/etc/sudoers文件前需要把它改为可读写状态,不然编辑了无法保存,该文件默认是只读。

1
2
chmod u+w /etc/sudoers 改为读写
chmod u-w /etc/sudoers 只读

编辑完成后,建议把/etc/sudoers改为只读状态

给用户分配权限

su git 切换为git用户,我操作的时候发现切换为git用户,默认的home 属于root 组及root用户,git用户没有操作权限,这个时候我们就需要把home改为git及对应的组

1
chown git:git /home/git/**

安装基础环境

这里需要安装一下nginxgitnginx主要是用来代理请求,大家都不希望直接裸奔提供服务吧。

1
2
3
sudo yum install git

sudo yum install nginx

下载安装包并安装

根据自己的linux系统版本下载对应gogs安装包, 然后使用 tar xvf 解压对应的目录,例如:

1
sudo tar xvf gogs_0.11.86_linux_amd64.tar.gz -C /home/git/

这里建议把文件解压到git用户的home里面,因为gogs里面很多的配置都是放在git用户的home里面,这样可以减少我们修改的成本。


配置完成后我们可以在gogs目录下执行 ./gogs web,看到以下信息就可以经行安装了,但是建议这样启动,因为命令行一关闭,gogs服务就停止了,官方已经给出了很多的守护线程执行脚本,在scripts文件夹下

1
2
3
4
5
6
7
8
9
10
[git@VM_1_2_centos gogs]$ ./gogs web
2019/04/03 17:42:39 [TRACE] Custom path: /home/git/gogs/custom
2019/04/03 17:42:39 [TRACE] Log path: /home/git/gogs/log
2019/04/03 17:42:39 [TRACE] Log Mode: Console (Trace)
2019/04/03 17:42:39 [ INFO] Gogs 0.11.86.0130
2019/04/03 17:42:39 [ INFO] Cache Service Enabled
2019/04/03 17:42:39 [ INFO] Session Service Enabled
2019/04/03 17:42:39 [ INFO] SQLite3 Supported
2019/04/03 17:42:39 [ INFO] Run Mode: Development
2019/04/03 17:42:39 [ INFO] Listen: http://0.0.0.0:3000

我这里使用的是Centos7所以就用到scripts/systemd/gogs.service启动脚本,需要进行以下操作。

1
cp scripts/systemd/gogs.service /etc/systemd/system

复制好启动脚本后,就执行它

1
sudo systemctl start gogs.service # 启动服务

如果需要开机启动话执行:

1
sudo systemctl enable gogs.service

查看服务运行状态

1
sudo systemctl status gogs.service

启动成功后会看到以下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
● gogs.service - Gogs
Loaded: loaded (/etc/systemd/system/gogs.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2019-04-03 17:49:14 CST; 5s ago
Main PID: 32650 (gogs)
CGroup: /system.slice/gogs.service
└─32650 /home/git/gogs/gogs web

Apr 03 17:49:14 VM_1_2_centos systemd[1]: Started Gogs.
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [TRACE] Custom path: /home/git/gogs/custom
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [TRACE] Log path: /home/git/gogs/log
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [TRACE] Log Mode: Console (Trace)
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [ INFO] Gogs 0.11.86.0130
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [ INFO] Cache Service Enabled
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [ INFO] Session Service Enabled
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [ INFO] SQLite3 Supported
Apr 03 17:49:14 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:14 [ INFO] Run Mode: Development
Apr 03 17:49:15 VM_1_2_centos gogs[32650]: 2019/04/03 17:49:15 [ INFO] Listen: http://0.0.0.0:8002

失败的话也会有对应的信息。
打开浏览器输入服务器的地址加端口号即可进行安装例如:127.0.0.1:8002,,默认端口是3000,启动会有输出的。

在浏览器中看到以下信息则说明可以进行安装了,这里就不在演示如何配置了,大家根据自己的需求来。
image

文章到此结束了,如果觉得有用的话,麻烦大家点一个赞,谢谢。