自建Git服务器之Gogs
2019-05-02 本文已影响2人
ImClive
一、什么是Gogs
Gogs 是一款极易搭建的自助 Git 服务,个人感觉比Gitlab要轻量级,搭建起来更方便。
二、下载及使用链接
三、环境搭建过程
1、环境要求(参考官方说明)
- 数据库(选择以下一项):
- MySQL:版本 >= 5.7
- PostgreSQL
- MSSQL
- TiDB(实验性支持,使用 MySQL 协议连接)
- 或者 什么都不安装 直接使用 SQLite3
-
git(bash):
- 服务端和客户端均需版本 >= 1.7.1
- Windows 系统建议使用最新版
- SSH 服务器:
- 如果您只使用 HTTP/HTTPS 的话请忽略此项
- 如果您选择在 Windows 系统使用内置 SSH 服务器,请确保添加
ssh-keygen
到您的%PATH%
环境变量中 - 推荐 Windows 系统使用 Cygwin OpenSSH 或 Copssh
- Windows 系统 请确保 Bash 是默认的 Shell 程序,而不是 PowerShell
2、开始搭建
2.1、新建一个git用户
Gogs 默认以 git 用户运行,新建一个git用户。
sudo adduser git
以 git 用户登录,到 git 用户的主目录中新建好 .ssh 文件夹。
su git
cd /home/git
mkdir .ssh
//注意由于原始git服务也是用的git用户,ssh服务可能会被gogs改写后占用,如下面的authorized_keys文件内容
command="/home/git/gogs/gogs serv key-2 --config='/home/git/gogs/custom/conf/app.ini'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXX@qq.com
//导致用ssh登陆时会有如下面输出
ssh git@YourHost
PTY allocation request failed on channel 0
Hi there, You've successfully authenticated, but Gogs does not provide shell access.
If this is unexpected, please log in with password and setup Gogs under another user.
Connection to YourHost closed.
解决方法:
- 因此可以新建一个webgit用户来和git用户区分,而不占用git用户中配置的authorized_keys
- 也可以针对不同的域名或IP使用不同的id_rsa.pub公钥,具体参考下面链接内容
装Gogs后ssh无法和远程登录同时使用的解决方案
2.2、找到想要的Gogs版本进行下载
cd /home/git
wget https://dl.gogs.io/0.11.34/linux_amd64.tar.gz
tar -zxvf linux_amd64.tar.gz
2.3、启动Gogs服务
cd gogs
./gogs web
//命令行输出
sh-4.1$ ./gogs web
2019/05/02 11:57:17 [TRACE] Custom path: /home/git/gogs/custom
2019/05/02 11:57:17 [TRACE] Log path: /home/git/gogs/log
2019/05/02 11:57:17 [TRACE] Log Mode: File (Trace)
2019/05/02 11:57:17 [ INFO] Wooduan 0.11.34.1122
//也可以以守护进程启动
nohup ./gogs web &
2.4、相关配置
- 创建Gogs所需数据库环境,登录mysql后执行下面命令
SET GLOBAL innodb_file_per_table = ON,
innodb_file_format = Barracuda,
innodb_large_prefix = ON;
DROP DATABASE IF EXISTS gogs;
CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
- 第一次启动后会进入install安装页面,在此可以进行Gogs的初始化配置,后期也可以在
/home/git/gogs/custom/conf/app.ini
中修改后重启Gogs生效。
基础配置
扩展配置 - 在安装过程中,会在mysql的gogs数据库中创建若干张表,如果出现mysql的报错,如:
Specified key was too long; max key length is 767 bytes when install gogs
,可以每次提交后查看show tables查看gogs数据库下新增的表,将该表的ROW_FORMAT属性置为DYNAMIC,所涉及表列在下面
ALTER TABLE `access` ROW_FORMAT=DYNAMIC;
ALTER TABLE `access_token` ROW_FORMAT=DYNAMIC;
ALTER TABLE `action` ROW_FORMAT=DYNAMIC;
ALTER TABLE `attachment` ROW_FORMAT=DYNAMIC;
ALTER TABLE `collaboration` ROW_FORMAT=DYNAMIC;
ALTER TABLE `comment` ROW_FORMAT=DYNAMIC;
ALTER TABLE `deploy_key` ROW_FORMAT=DYNAMIC;
ALTER TABLE `email_address` ROW_FORMAT=DYNAMIC;
ALTER TABLE `follow` ROW_FORMAT=DYNAMIC;
ALTER TABLE `hook_task` ROW_FORMAT=DYNAMIC;
ALTER TABLE `issue` ROW_FORMAT=DYNAMIC;
ALTER TABLE `issue_label` ROW_FORMAT=DYNAMIC;
ALTER TABLE `issue_user` ROW_FORMAT=DYNAMIC;
ALTER TABLE `label` ROW_FORMAT=DYNAMIC;
ALTER TABLE `login_source` ROW_FORMAT=DYNAMIC;
ALTER TABLE `milestone` ROW_FORMAT=DYNAMIC;
ALTER TABLE `mirror` ROW_FORMAT=DYNAMIC;
ALTER TABLE `notice` ROW_FORMAT=DYNAMIC;
ALTER TABLE `org_user` ROW_FORMAT=DYNAMIC;
ALTER TABLE `protect_branch` ROW_FORMAT=DYNAMIC;
ALTER TABLE `protect_branch_whitelist` ROW_FORMAT=DYNAMIC;
ALTER TABLE `public_key` ROW_FORMAT=DYNAMIC;
ALTER TABLE `pull_request` ROW_FORMAT=DYNAMIC;
ALTER TABLE `release` ROW_FORMAT=DYNAMIC;
ALTER TABLE `repository` ROW_FORMAT=DYNAMIC;
ALTER TABLE `star` ROW_FORMAT=DYNAMIC;
ALTER TABLE `team` ROW_FORMAT=DYNAMIC;
ALTER TABLE `team_repo` ROW_FORMAT=DYNAMIC;
ALTER TABLE `team_user` ROW_FORMAT=DYNAMIC;
ALTER TABLE `two_factor` ROW_FORMAT=DYNAMIC;
ALTER TABLE `two_factor_recovery_code` ROW_FORMAT=DYNAMIC;
ALTER TABLE `upload` ROW_FORMAT=DYNAMIC;
ALTER TABLE `user` ROW_FORMAT=DYNAMIC;
ALTER TABLE `watch` ROW_FORMAT=DYNAMIC;
ALTER TABLE `webhook` ROW_FORMAT=DYNAMIC;
- 主要配置项
-
在进行git pull/push等操作的时候,需要用到git相关可执行文件,需要软链加到/bin路径下
ln -s /usr/local/git-2.12.2/bin/git-upload-pack /bin/git-upload-pack
ln -s /usr/local/git-2.12.2/bin/git-cvsserver /bin/git-cvsserver
ln -s /usr/local/git-2.12.2/bin/gitk /bin/gitk
ln -s /usr/local/git-2.12.2/bin/git-receive-pack /bin/git-receive-pack
ln -s /usr/local/git-2.12.2/bin/git-shell /bin/git-shell
ln -s /usr/local/git-2.12.2/bin/git-upload-archive /bin/git-upload-archive
[root@10-23-67-69 conf]# ll /bin/git*
lrwxrwxrwx 1 root root 29 May 2 01:24 /bin/git -> /usr/local/git-2.12.2/bin/git
lrwxrwxrwx 1 root root 39 May 2 01:25 /bin/git-cvsserver -> /usr/local/git-2.12.2/bin/git-cvsserver
lrwxrwxrwx 1 root root 42 May 2 01:25 /bin/git-receive-pack -> /usr/local/git-2.12.2/bin/git-receive-pack
lrwxrwxrwx 1 root root 35 May 2 01:25 /bin/git-shell -> /usr/local/git-2.12.2/bin/git-shell
lrwxrwxrwx 1 root root 44 May 2 01:25 /bin/git-upload-archive -> /usr/local/git-2.12.2/bin/git-upload-archive
lrwxrwxrwx 1 root root 41 May 2 01:20 /bin/git-upload-pack -> /usr/local/git-2.12.2/bin/git-upload-pack
lrwxrwxrwx 1 root root 30 May 2 01:26 /bin/gitk -> /usr/local/git-2.12.2/bin/gitk
暂时想到的就这么多,安装或者使用上面有问题的话,请留言给我,我会及时回复的。