使用 Gogs 搭建自己的 Git 服务器

前言

因为有些仓库上传到Github并不是非常合适,所以就搭建了一个自己的私人仓库。

在安装Gogs前,我也尝试了Gitlab,效果很不错

环境

  • Centos7.1

安装

配置Gogs所需的环境

安装nginx

sudo apt-get install nginx

安装git

sudo apt-get install git

安装MySQL

sudo apt-get install mysql-server

进入数据库

mysql -u root -p

创建gogs数据库

SET GLOBAL storage_engine = 'InnoDB';
CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin;
GRANT ALL PRIVILEGES ON gogs.* TO ‘root’@‘localhost’ IDENTIFIED BY 'YourPassword';
FLUSH PRIVILEGES;
QUIT;

为Gogs创建单独的用户

sudo adduser git
....

cd到根目录,下载Gogs

Gogs 官方下载页

su git
cd ~
wget https://dl.gogs.io/0.11.4/linux_amd64.zip
unzip linux_amd64.zip

配置与运行Gogs

修改Gogs service配置文件

vim /home/git/gogs/scripts/init/centos/gogs

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Go Git Service"
NAME=gogs
SERVICEVERBOSE=yes
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
WORKINGDIR=/home/git/gogs #这个根据自己的目录修改
DAEMON=$WORKINGDIR/$NAME
DAEMON_ARGS="web"
USER=git  #如果运行gogs不是用的这个用户,修改对应用户

切会root账户然后复制到/etc/init.d/

sudo cp /home/git/gogs/scripts/init/centos/gogs /etc/init.d/

增加执行权限

sudo chmod +x /etc/init.d/gogs

复制service

cp /home/git/gogs/scripts/systemd/gogs.service /etc/systemd/system/

启动Gogs

sudo service gogs start

在自己浏览器上配置Gogs, localhost替换成自己的ip地址

http://localhost:3000/install

有关Gogs的配置文件在/home/git/gogs/custom/conf/app.ini里面,相关配置在Gogs文档中有。

nginx 反代理

现在访问Gogs都需要在域名后面加入3000的端口号,可以设置nginx反代理,通过二级域名跳转到指定端口

创建相应的配置文件

sudo vim /etc/nginx/sites-enabled/gogs.conf

添加

server {
        listen 80;
        server_name  code.limchihi.cn;
        location / {
                proxy_pass http://127.0.0.1:3000/;
        }
}

Done