惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
B
Blog
U
Unit 42
有赞技术团队
有赞技术团队
博客园 - 聂微东
GbyAI
GbyAI
宝玉的分享
宝玉的分享
F
Full Disclosure
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
Jina AI
Jina AI
Martin Fowler
Martin Fowler
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
P
Proofpoint News Feed
A
About on SuperTechFans
I
InfoQ
博客园 - 【当耐特】
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Project Zero
Project Zero
WordPress大学
WordPress大学
小众软件
小众软件
AWS News Blog
AWS News Blog
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
I
Intezer
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes

weirane’s blog

开始读《异见时刻》 中美文化差异 关于基于表达式的编程语言的一些思考 使用 bwrap 隔离 WPS Office 从零开始的 λ 演算 从 Manjaro 迁移到 Arch Linux 使用 pass 管理密码 更换域名 Fcitx5 安装记
搭建一个 Git 服务器
weirane · 2020-08-03 · via weirane’s blog

本篇将记录我使用 gitwebnginx 搭建带有网页界面和 HTTP clone 的 Git 服务器的过程。

Git 服务器

首先设置能使用 SSH 访问的 Git 服务器,参考 Pro Git 中的说明。先在服务器上添加 git 用户

sudo useradd -m /srv/git git
sudo passwd git

然后导入本地的 SSH key(需要在 /etc/ssh/sshd_config 中设置 PasswordAuthentication yes

ssh-copy-id -i ~/.ssh/id_rsa git@<IP address>

为了安全禁用 shell 登录

sudo chsh git -s $(which git-shell)

GitWeb

Ubuntu 中的 git 包已经包含了 gitweb 需要的一些静态文件,在 /usr/share/gitweb 里面。还需安装的是 highlightfcgiwrap

sudo apt install highlight fcgiwrap

/etc/gitweb.conf 中设置 gitweb,其它配置选项可在 man gitweb.conf 中查看。

# path to git projects (<project>.git)
$projectroot = "/srv/git";

# directory to use for temp files
$git_temp = "/tmp";

# html text to include at home page
$home_text = "/srv/git/indextext.html";

# stylesheet to use
#@stylesheets = ("static/gitweb.css");

# javascript code for gitweb
#$javascript = "static/gitweb.js";

# logo to use
#$logo = "static/git-logo.png";

# the 'favicon'
#$favicon = "static/git-favicon.png";

# git-diff-tree(1) options to use for generated patches
#@diff_opts = ("-M");
@diff_opts = ();

# 不显示 owner
$omit_owner = 1;

# 只显示 bare repo 中有 "git-daemon-export-ok" 这个文件的仓库
$export_ok = "git-daemon-export-ok";

# 语法高亮(需要 highlight 包)
$feature{'highlight'}{'default'} = [1];

/srv/git/indextext.html 中写入需要在主页中额外添加的文字

echo 'My personal git repos' | sudo tee /srv/git/indextext.html

添加一个新的 nginx server block

# /etc/nginx/sites-available/git

server {
    server_name git.example.com;
    location /gitweb.cgi {
        root /usr/share/gitweb;
        gzip off;
        include fastcgi_params;
        fastcgi_param SCRIPT_NAME $uri;
        fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }
    location / {
        root /usr/share/gitweb;
        index gitweb.cgi;
    }
}

最后 enable 并获取一个 SSL 证书

sudo ln -sf /etc/nginx/sites-{available,enabled}/git
sudo certbot --nginx
sudo systemctl restart nginx

HTTP clone

目的是允许任何人使用 git clone https://git.example.com/clone/NAME 对 gitweb 中显示的仓库进行 clone,为此使用 git-http-backend。在 /etc/nginx/sites-available/gitserver 块中加入

location ~ /clone(/.*) {
    client_max_body_size 0;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
    fastcgi_param GIT_PROJECT_ROOT /srv/git;
    fastcgi_param PATH_INFO $1;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
}

git-http-backend 会检查 bare repo 中是否有 git-daemon-export-ok 这个文件,如果没有则不允许 clone,这正好是 gitweb 配置中表示「公有仓库」使用的文件。如果想对所有的仓库都允许 clone,则可以添加一个 fastcgi param:

    fastcgi_param GIT_HTTP_EXPORT_ALL "";

这个 HTTP clone 只是一个只读的 clone,如果想要 push 还需使用 SSH。

结果

创建一个测试仓库并标记为公开,再添加一个说明

cd
git init --bare test.git
touch test.git/git-daemon-export-ok
echo 'Test repo' > test.git/description

网页效果如下

test.git

Clone:

$ git clone https://git.example.com/clone/test
Cloning into 'test'...
warning: You appear to have cloned an empty repository.

$ ls -a test
.git