使用Gogs配置Git服务器

为了同步代码我们通常使用git来对代码进行管理,最常用的就是Github了。但出于某些原因,有些代码我们并不想公开(当然我们可以使用github的私有仓库,但同样有基于安全意义上的考量),作为一个现实意义上的穷人,我并不能支付得起github的付费方案,因此使用免费开源的git server就成为了我的最佳选择。作为折腾的一部分,在这里把安装的过程和一些需要注意的地方记录一下。

目前比较好的git server有两个,Gitlab和Gogs。Gitlab作为Github的山寨版,功能非常全面,但与此同时也十分臃肿。国人开发的Gogs则十分轻量,接下来进行Gogs的配置。

 

1.新建用户

2.下载并解压缩二进制安装包(建议通过二进制包安装配置)

3.源码安装,需设置go环境(源码安装时必需设置,二进制包安装不需要)

4.DB初始化,gogs安装

5.调整配置

6.配置nginx反向代理

7.添加启动脚本及开机启动

注意,这里默认已经安装并配置好了MySQL和Nginx。也可以使用SQLite数据库。

 

新建用户

Gogs默认以git用户运行,建立git用户

adduser git #建立git用户 sudo /bin/su - git #以git用户登录 mkdir ~/.ssh #建立.ssh目录

注意:

chmod 700 .ssh

chmod 600 authorized_keys

#注意:

修改/etc/ssh/sshd_config,我这以前修改过,指定了"AuthorizedKeysFile .ssh/KHK75NEOiq",需删除这行重启ssh服务

下载并解压缩二进制安装包(建议通过二进制包安装配置)

下载并解压缩二进制安装包

地址:https://dl.gogs.io/

cd /home/git

wget https://dl.gogs.io/0.11.19/linux_amd64.tar.gz

tar -zxvf linux_amd64.tar.gz

ls gogs custom gogs log README.md scripts data LICENSE public README_ZH.md templates

二进制包安装后如何升级

下载最新版的压缩包。

删除当前的templates目录。

解压压缩包并将所有内容复制粘贴到相应(当前)的位置

 

源码安装,需设置go环境(源码安装Go版本 >= 1.6,源码安装时必需设置,二进制包安装不需要)

sudo /bin/su - git

cd ~

# curl -O -k https://www.golangtc.com/static/go/1.8.3/go1.8.3.linux-amd64.tar.gz

# curl: (35) SSL connect error yum install nss -y即可

wget --no-check-certificate https://www.golangtc.com/static/go/1.8.3/go1.8.3.linux-amd64.tar.gz

tar -xzf go1.8.3.linux-amd64.tar.gz -C /usr/local

设置和您系统环境对应的路径:

sudo su - git

cd ~

echo -e 'export GOPATH=$HOME/gocode\nexport GOROOT=/usr/local/go\nexport GOBIN=$GOROOT/bin\nexport GOPKG=$GOROOT/pkg/tool/linux_amd64\nexport GOARCH=amd64\nexport GOOS=linux\nexport PATH=.:$PATH:$GOBIN:$GOPKG' >> $HOME/.bashrc

source $HOME/.bashrc

#

下载并安装依赖

go get -u github.com/gogits/gogs

构建主程序

cd $GOPATH/src/github.com/gogits/gogs

go build

测试安装

cd $GOPATH/src/github.com/gogits/gogs

./gogs web

#无报错,使用Ctrl-C停止

使用标签构建

Gogs 默认并没有支持一些功能,这些功能需要在构建时明确使用构建标签(build tags)来支持。

目前使用标签构建的功能如下:

sqlite3:SQLite3 数据库支持

pam:PAM 授权认证支持

cert:生成自定义证书支持

miniwinsvc:Windows 服务内置支持(或者您可以使用 NSSM 来创建服务)

例如,您需要支持以上所有功能,则需要先删除 GOPATH/pkg/{GOOS_GOARCH}/github.com/gogits/gogs 目录,然后执行以下命令:

go get -u -tags "sqlite pam cert" github.com/gogits/gogs

cd $GOPATH/src/github.com/gogits/gogs

go build -tags "sqlite pam cert"

安装完成后可继续下一步的配置

 

DB初始化,gogs安装

新数据库。Gogs目录的scripts/mysql.sql文件是数据库初始化文件

mysql -u root -p < scripts/mysql.sql

#需要输入密码,即可初始化数据库。

然后登录MySQL创建一个新用户gogs,并将数据库gogs的所有权限都赋予该用户。

$ mysql -u root -p Enter password: mysql> create user 'gogs'@'localhost' identified by '123456'; mysql> grant all privileges on gogs.* to 'gogs'@'localhost' identified by '123456' with grant option; mysql> flush privileges; mysql> exit;

执行./gogs web运行Gogs,然后访问http://服务器IP:3000/ 安装,按照页面提示填写信息。

./gogs --help

NAME:

Gogs - A painless self-hosted Git service

 

USAGE:

gogs [global options] command [command options] [arguments...]

 

COMMANDS:

web Start web server

serv This command should only be called by SSH shell

hook Delegate commands to corresponding Git hooks

cert Generate self-signed certificate

admin Preform admin operations on command line

import Import portable data as local Gogs data

backup Backup files and database

restore Restore files and database from backup

help, h Shows a list of commands or help for one command

 

GLOBAL OPTIONS:

--help, -h show help

--version, -v print the version

 

./gogs backup --help

NAME:

gogs backup - Backup files and database

 

USAGE:

gogs backup [command options] [arguments...]

 

DESCRIPTION:

Backup dumps and compresses all related files and database into zip file,

which can be used for migrating Gogs to another server. The output format is meant to be

portable among all supported database engines.

 

OPTIONS:

--config value, -c value Custom configuration file path (default: "custom/conf/app.ini")

--verbose, -v Show process details

--tempdir value, -t value Temporary directory path (default: "/tmp")

--target value Target directory path to save backup archive (default: "./")

--archive-name value Name of backup archive (default: "gogs-backup-1503461127.zip")

--database-only Only dump database

--exclude-repos Exclude repositories

 

调整配置

默认配置都保存在conf/app.ini,您永远不需要编辑它。该文件从v0.6.0版本开始被嵌入到二进制中

自定义配置文件。那么,在不允许修改默认配置文件conf/app.ini的情况下,怎么才能自定义配置呢?只要创建custom/conf/app.ini就可以!在custom/conf/app.ini文件中修改相应选项的值即可

乍一看,这么做有些复杂,但是这么做可以有效地保护您的自定义配置不被破坏:

  • 从二进制安装的用户,可以直接替换二进制及其它文件而不至于重新编写自定义配置
  • 从源码安装的用户,可以避免由于版本管理系统导致的文件修改冲突

app.ini关键配置如下:

详细的配置解释和默认值请参考配置文件手册

  • RUN_USER默认为git,指定Gogs以哪个用户运行
  • ROOT 所有仓库的存储根路径
  • PROTOCOL用nginx反代的话使用http
  • DOMAIN域名,会影响SSH clone地址
  • ROOT_URL完整的根路径,会影响页面上链接指向,以及HTTP(s) clone的地址
  • HTTP_ADDR监听地址,使用nginx建议127.0.0.1,否则localhost或0.0.0.0也可以
  • HTTP_PORT监听端口,默认3000
  • INSTALL_LOCK锁定安装页面
  • Mailer相关的选项

注意邮箱stmp地址要加端口号

smtp.exmail.qq.com:465

xx@xx.cn

 

调整配置OK后,可以停止./gogs web进程

#注册服务/邮件服务关起均通过app.ini管理

cat custom/conf/app.ini

APP_NAME = Gogs

RUN_USER = git

RUN_MODE = prod

 

[database]

DB_TYPE = mysql

HOST = 127.0.0.1:3306

NAME = gogs

USER = gogs

PASSWD = xxxxxx2017

SSL_MODE = disable

PATH = data/gogs.db

 

[repository]

ROOT = /home/git/gogs-repositories

 

[server]

DOMAIN = 192.168.1.48

HTTP_PORT = 3000

ROOT_URL = http://192.168.1.48:3000/

DISABLE_SSH = false

SSH_PORT = 22

START_SSH_SERVER = false

OFFLINE_MODE = false

 

[mailer]

ENABLED = true

HOST = smtp.exmail.qq.com:465

FROM = xx@xx.cn

USER = xx@xx.cn

PASSWD = xxxxxx

 

[service]

REGISTER_EMAIL_CONFIRM = true

ENABLE_NOTIFY_MAIL = true

DISABLE_REGISTRATION = false

ENABLE_CAPTCHA = true

REQUIRE_SIGNIN_VIEW = false

 

[picture]

DISABLE_GRAVATAR = false

ENABLE_FEDERATED_AVATAR = true

 

[session]

PROVIDER = file

 

[log]

MODE = file

LEVEL = Info

ROOT_PATH = /home/git/gogs/log

 

[security]

INSTALL_LOCK = true

SECRET_KEY = 0HUqVHFLkticO0C

 

 

配置nginx反向代理

修改nginx的配置文件,添加如下内容:

server { server_name #域名或IP; listen 80; #或者443,如果使用HTTPS的话 #ssl on; #启用加密连接 #如果你使用HTTPS,还需填写ssl_certificate和ssl_certificate_key location / { #如果你希望通过子路径访问,此处修改为子路径,注意以/开头和结束 proxy_pass http://127.0.0.1:3000/; } }

 

#使用HTTPS

server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name example.com; ssl on; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/certificate_key.key; location / { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://localhost:3000; } }

重启nginx是修改生效,访问测试

 

添加服务及开机启动

在Gogs目录的gogs/scripts/init路径找到对应发行版的启动脚本,添加开机启动

cp -R /home/git/gogs/scripts/init/centos/gogs /etc/init.d/gogs

chmod +x /etc/init.d/gogs

service gogs start && chkonfig gogs on

 

开始Gogs的使用吧!

 

使用步骤

  1. 用户注册,并进行邮件确认
  2. 用户创建组织
  3. 邀请用户(协作者)加入组织
  4. 客户终端生成ssh-key(id_rsa.pub)
  5. 将个人ssh-key(id_rsa.pub)加入用户“管理ssh密钥”

注:

仓库的部署密钥只有只读权限,不要与个人密钥搞混

注意公钥的格式问题,否则添加时会报错

  1. 客户终端git clone 代码库

注:

验证是否添加成功

ssh -T git@192.168.1.48

3种clone的方式

git clone ssh://git@192.168.1.48/xx/test.git

git clone git@192.168.1.48:xx/test.git

git clone http://192.168.1.48:3000/xx/test.git

#

ssh非22端口

git clone ssh://git@ip/domain:port/xx/test.git

git clone git@ip/domain:port/xx/test.git

  1. 创建README,并添加内容
  2. 测试提交git push

注:

测试提交命令:

touch README && echo "Hello World!" > README

git add README && git commit -m "My First Repo!"

git push

#git push可能会报错:"No refs in common and none specified; doing nothing.

Perhaps you should specify a branch such as 'master'.

fatal: The remote end hung up unexpectedly

error: failed to push some refs to 'ssh://192.168.1.48/xx/test.gi'"

执行:git push origin master

  1. 对于需部署端,这里使用部署密钥,配置同个人ssh密钥

注:

拉取端既部署端的ssh-key添加到“管理部署密钥”内

开发个人端的ssh-key添加到“管理ssh密钥”内

 

 

1.

#!/bin/sh

#

deploy_path=/home/wwwroot/test

unset GIT_DIR

#注意应用目录内文件属主权限(chown既不影响git更新,同时不影响访问),无法自动更新请确认git用户可以对test目录下文件相应操作

#

#首次拉取

#git clone http://192.168.1.48:3000/xx/test.git

#

cd $deploy_path

#

git stash

#

git pull origin dev

#

git stash pop

#

exit 0

 

2.

#!/bin/sh

#

deploy_path=/home/wwwroot/test

unset GIT_DIR

#注意应用目录内文件属主权限(chown既不影响git更新,同时不影响访问),无法自动更新请确认git用户可以对test目录下文件相应操作

#

#首次拉取

#git clone http://192.168.1.48:3000/xx/test.git

#

cd $deploy_path

#

git reset --hard

#

git pull origin dev

#

exit 0

 

FAQ:

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=manager-st push -v --tags origin dev:dev

POST git-receive-pack (12687 bytes)

POST git-receive-pack (12687 bytes)

fatal: The remote end hung up unexpectedly

fatal: The remote end hung up unexpectedly

error: RPC failed; HTTP 403 curl 22 The requested URL returned error: 403 Forbidden

用户未加入协作者

 

Link:

https://try.gogs.io/

https://github.com/gogits/gogs

https://dl.gogs.io/

https://gogs.io/docs/intro/troubleshooting

https://gogs.io/docs/intro/faqs

https://gogs.io/docs/advanced/configuration_cheat_sheet

http://blog.csdn.net/daiwood/article/details/50561306

http://blog.csdn.net/wanwan5856/article/details/52797969

http://kongfangyu.com/2016/02/12/git-deploy/

http://blog.gejiawen.com/2016/05/31/note-for-deploy-gogs/

https://www.howtoing.com/how-to-set-up-gogs-on-ubuntu-14-04/

转载于:https://my.oschina.net/HeAlvin/blog/1504096

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值