Skip to content

Instantly share code, notes, and snippets.

@tanpengsccd
Last active May 25, 2018 09:18
Show Gist options
  • Save tanpengsccd/8a054dd1a0e1581aae550b904ff524e2 to your computer and use it in GitHub Desktop.
Save tanpengsccd/8a054dd1a0e1581aae550b904ff524e2 to your computer and use it in GitHub Desktop.
Gogs 安装于 CentOS7

前言

楼主香港 腾讯 CentOS 主机 1G 单核 主机,先尝试的gitlab,虽然gitlab 功能强大,无奈配置不够,点一下卡10s。只能 使用轻量化的Gogs。使用起来也算不错。

gogs (Go Git Service)

如果先前安装过gitlab 建议先卸载

---gitlab-ctl unisntall 再userdel git -r -rf -f

安装准备

配置环境

  1. root 用户下下
  2. yum install curl git make bison gcc glibc-devel -y

安装mysql

  1. 安装mariadb(mysql 变种) yum install mariadb-server mariadb
  2. 配置
# 启动MariaDB
    systemctl start mariadb  
    systemctl stop mariadb  
    systemctl restart mariadb  
#设置开机启动
    systemctl enable mariadb  
#进入
    mysql 
#新建数据库
    create database gogs;
    exit;

新建git用户

  1. 添加git 用户 和设置密码
useradd git 
passwd git 

  1. 配置最高权限(可能需要)

    echo -e "\n用户名 ALL=(ALL) ALL\n" >> /etc/sudoers
    tail -3 /etc/sudoers
    
  2. 切换到git
    sudo su - git

安装GO

查看最新 https://golang.org/dl/

使用Go版本管理工具安装

在git用户下 安装 go环境

具体是zsh bash 自己选择

zsh < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

如果提示gvm找不到命令,可能是zsh bash 工具不对应 需要在对应.bashrc 或.zshrc 中 加入

[[ -s "/root/.gvm/scripts/gvm" ]] && source "/root/.gvm/scripts/gvm"

查看帮助

gvm help 

需要先安装1.4 才能 使用1.4 编译更高版本(现在最新1.10)

gvm install go1.4 
gvm use go1.4 
export GOROOT_BOOTSTRAP=$GOROOT 
gvm install go1.10
gvm use go1.10 --default

手动安装

sudo su - git
cd ~
#create a folder to install 'go'   
mkdir local
# Download go (change go$VERSION.$OS-$ARCH.tar.gz to the latest release)
wget https://storage.googleapis.com/golang/go$VERSION.$OS-$ARCH.tar.gz
# expand it to ~/local
tar -C /home/git/local -xzf go$VERSION.$OS-$ARCH.tar.gz

安装Gogs

官方步骤 https://github.com/gogits/gogs

源码安装(推荐)

下载并安装依赖

 go get -u github.com/gogits/gogs

构建主程序

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

二进制安装

下载
wegt https://github.com/gogits/gogs/releases/download/v0.11.19/linux_amd64.tar.gz 解压
tar zxvf linux_amd64.tar.gz

配置

运行设置界面(web)

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

按提示访问,一般是 http://localhost/3000 进行设置 以下为默认配置文件不能修改,只做参考

emacs $GOPATH/src/github.com/gogits/gogs/conf/app.ini

可能需要修改自定义配置文件 $GOPATH/src/github.com/gogits/gogs/custom/conf/app.ini 注意不是
$GOPATH/src/github.com/gogits/gogs/conf/app.ini

配置 ATS 证书

已经下载好了的 一般是crt 和key 证书 ,需要传输 使用scp 传输

scp -r  /Users/tanpeng/Downloads/git.tanpengcd.cn/Nginx/  git@ali.tanpengcd.cn:/home/git/go/src/github.com/gogits/gogs

使用certbot 安装生成 ATS证书(推荐)

建议直接使用certbot-auto 脚本安装(自动安装certbot及其依赖),不要直接yum安装certbot(会出现许多依赖问题)

wget https://dl.eff.org/certbot-auto
chmod a+x ./certbot-auto
./certbot-auto --help

按提示并且打开80访问端口,生成证书会提示如下

- Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/git.xxx.com/fullchain.pem
  .....

此时在/etc/letsencrypt/live/git.xxx.com/ 下会有
cert.pem chain.pem fullchain.pem privkey.pem

Gogs配置文件直接设置

//复制证书 mv /etc/letsencrypt/live $GOPATH/src/github.com/gogits/gogs/custom/ //修正权限 sudo chown root:git custom/live/ sudo chmod 775 custom/live/ 修改$GOPATH/src/github.com/gogits/gogs/custom/conf/app.ini 在中[service] 修改或加入

PROTOCOL = https
CERT_FILE = $GOPATH/src/github.com/gogits/gogs/custom/live/git.h6.work/cert.pem
KEY_FILE = $GOPATH/src/github.com/gogits/gogs/custom/live/git.h6.work/privkey.pem

重启 服务

利用Nginx 跳转 https(推荐)

可以使用80 跳转 443 再跳转到 3000 安装 nginx 修改配置文件 /etc/nginx/conf.d

server {  
    listen 443 ssl;
    server_name git.xxx.com;
    ssl_certificate     /etc/letsencrypt/live/git.xxx.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/git.xxx.com/privkey.pem;

    location / {
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_pass http://localhost:3000$request_uri;
    }
}

# Redirect HTTP requests to HTTPS
server {  
    listen 80;
    server_name git.xxx.com;
    return 301 https://$host$request_uri;
}

因为非root 用户无法设置1024 以下的端口,所以需要开启内部端口映射,以达到访问443 可跳转映射3000端口效果

Azure 和 阿里云等安全组策略VPS的 需要添加 对应 端口 如 80 443 3000 //允许内部跳转 编辑/etc/sysctl.conf 修改或添加

net.ipv4.ip_forward = 1

有的可能需要利用iptables 转发 端口(我的不需要)

yum install -y iptables
yum install iptables-services
systemctl stop firewalld
systemctl mask firewalld

iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3000
service iptables save
#注册iptables服务
#相当于以前的chkconfig iptables on
systemctl enable iptables.service
#开启服务
systemctl start iptables.service
#查看状态
systemctl status iptables.service
#
sysctl -p /etc/sysctl.conf 
#or
sysctl -p 

自启动脚本

root账户 gogs/scripts/systemd/ 到 /etc/systemd/system 在修改运行路径
或者直接创建

vi /etc/systemd/system/gogs.service


[Unit]
Description=Gogs
After=syslog.target
After=network.target
After=mariadb.service mysqld.service postgresql.service memcached.service redis.service

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=simple
User=git
Group=git

WorkingDirectory=/home/git/go/src/github.com/gogits/gogs #路径看情况 /home/git/.gvm/pkgsets/go1.10/global/src/github.com/gogits/gogs 
ExecStart=/home/git/go/src/github.com/gogits/gogs/gogs web #路径看情况 /home/git/.gvm/pkgsets/go1.10/global/src/github.com/gogits/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target
systemctl start gogs
systemctl enable gogs

gitlab

硬件要求(最低)

  1. 1 核心CPU最多支持100个用户
  2. 安装使用GitLab需要至少4GB可用内存(RAM + Swap)
  3. 1GB 物理内存 + 3GB 交换分区 是最低的要求

详情 https://docs.gitlab.com.cn/ce/install/requirements.html#part-6542a9e251e

配置内存 swap

http://blog.csdn.net/ithomer/article/details/53942718

安装

如需要设置 域名 需要 配置 /etc/gitlab/gitlab.rb
具体操作: https://about.gitlab.com/downloads/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment