在ubuntu上面用gogs搭建git服务器

假设我们以deploy用户部署,git用户存放git仓库。gogs程序放在deploy用户home目录下面。

创建用户deploy

sudo useradd -m -s /bin/bash deploy
sudo passwd deploy
sudo usermod -aG sudo deploy

创建用户git

sudo useradd -m -s /bin/bash git

注意git用户一定要有 shell ,也就是 -m /bin/bash,否则后期会出现报错:

Cloning into 'xxx'...
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

从官网下载gogs二进制安装包

下载页面:https://gogs.io/docs/installation/install_from_binary

解压到目录 /home/deploy/gogs/

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

安装superviosr来运行 gogs

sudo apt-get install supervisor 
cp -r /etc/supervisor /home/deploy/etc/supervisor
sudo rm -r /etc/supervisor
sudo ln -s /home/deploy/etc/supervisor /etc/supervisor
vim /home/deploy/etc/supervisor/supervisord.conf

unix_http_server节点添加:

chown=deploy:deploy

修改supervisord节点的日志目录,到 /home/deploy/log 下面。

添加配置文件 vim ~/etc/supervisor/conf.d/gogs.conf :

[program:gogs]
command=/home/deploy/gogs/gogs web
autorestart=true
user=git
redirect_stderr = true
directory = /home/deploy/gogs/
environment = HOME="/home/git", USER="git"
stdout_logfile = /home/deploy/log/supervisor/gogs.log

然后启动supervisor。

再配置 nginx 中转,vim ~/etc/nginx/upstreams.conf

upstream gogs{
    server 127.0.0.1:3000;
}

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

server {
        listen 80;

        server_name git.xxx.com;
        access_log  /var/log/nginx/git-access.log;
        error_log  /var/log/nginx/git-error.log;

        root /home/deploy/gogs/;
        client_max_body_size 10m;

        location ^~ / {
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_pass http://gogs;
                proxy_next_upstream error;
        }
}

gogs的部分配置, /home/deploy/gogs/custom/conf/app.ini

[repository]
ROOT = /home/git/gogs-repositories

[server]
DOMAIN       = git.xxx.com
HTTP_PORT    = 3000
ROOT_URL     = http://git.xxx.com/
DISABLE_SSH  = false
SSH_PORT     = 22
OFFLINE_MODE = false