在EC2上创建Git仓库

1. 创建主仓库

在EC2上建立一个中央仓库。SSH到EC2实例后

mkdir repo-name.git
cd repo-name.git
git init --bare

一个bare仓库是不能进行commit的,只是当作一个中央仓库存在。所有工作都应该先在本地的仓库中完成后push进来。


2. 在本地push/pull

本地git仓库是通过ssh从服务器push/pull的,又因为EC2必须有私钥,所以先执行一步添加私钥的操作。

eval "$(ssh-agent -s)"
ssh-add /path/to/the/private/key

然后在本地建立另一个工作仓库,

cd repo-name
git init
git add .
git commit -m "Initial commit"
git remote add origin ubuntu@xxxxxxx:/path/to/your/repo-name.git
git config --global remote.origin.receivepack "git receive-pack" # needed for EC2 stuff
git push origin master

以后工作仓库的改动都可以push到中央仓库了。


3. 自动更新这个网站

在本地更新了网站内容后,源代码会push到服务器的仓库里。我新建了一个脚本,可以一键完成 (服务器端同步更新源码 — 在服务器端编译 — 部署到nginx根目录下)这一系列动作。

git pull origin master
source .venv/bin/activate
sphinx-build source notes
sudo rm -rf /var/www/html/notes
sudo cp -r notes /var/www/html/
deactivate

4. 基于cgit的静态网页仓库

如果我有一部分代码想分享出来,cgit是个很小巧的工具可以基于git repo生成网页以便于访问。在Ubuntu24.04下,可以直接通过sudo apt-get install cgit来安装。

cgit路径配置

cgit的配置文件位于/etc/cgitrc,要修改如下。其中csslogo是https访问时的静态资源的url后缀,scan-path是服务器上git repo的绝对路径,clone-prefix是别的用户想通过https clone的时候的url前缀。因为我想通过 https://dizong.top/git 来访问我的仓库,所以virtual-root写的是/git/

#
# cgit config
# see cgitrc(5) for details

css=/git/cgit.css
logo=/git/dogking+cgit.png
source-filter=/usr/lib/cgit/filters/syntax-highlighting.py
scan-path=/var/www/repo
clone-prefix=https://dizong.top/git
virtual-root=/git/
enable-html-serving=1

mimetype.css=text/css
mimetype.html=text/html
mimetype.txt=text/plain
mimetype.xml=text/xml              

cgit会去/var/www/repo寻找git repo,需要确保用户组www-data要有这个目录的读的权限,否则cgit的网页上一直会是空的。同时“我”又要有这个目录的写的权限(因为我需要往里push东西),所以需要把这个路径的所有者改成我,把所在组改成www-data

sudo chown ubuntu:www-data /var/www/repo

然后再检查权限设置就是正确的了。

ubuntu@ip-172-31-45-8:/var/www$ ls -l
total 8
drwxr-xr-x 3 root   root     4096 Sep 13 13:13 html
drwxr-xr-x 4 ubuntu www-data 4096 Sep 12 16:46 repo

cgit语法高亮设置

语法高亮是通过/etc/cgitrc的这一行来设置的。这是个官方写好的高亮脚本。根据文档,高亮脚本也有.sh版本可以选择,但是我实测.sh脚本版本太旧不兼容,所以还是.py好。

此外,如这里所说,这一行一定要放在include行之前,否则不会生效。我当时就折腾了好久怎么都没有效果,找到原因的时候啼笑皆非,真是奇怪的bug。

source-filter=/usr/lib/cgit/filters/syntax-highlighting.py

nginx配置

nginx需要这样配置。静态的css和png会从/usr/share/cgit读取。注意rewrite ^/git(/.*)?$ $1的作用是把url里的/git/去掉,防止cgit陷入/git/git/git的无穷嵌套找不到正确的路径。

location /git/ {
        # cgit的静态文件
        alias /usr/share/cgit/;
        try_files $uri @cgit;
}

location @cgit {
        rewrite ^/git(/.*)?$ $1 break;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi;
        fastcgi_param PATH_INFO $1;
        fastcgi_param QUERY_STRING $args;
        fastcgi_param CGIT_CONFIG /etc/cgitrc;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
}

参考资料

  1. https://www.geeksforgeeks.org/bare-repositories-in-git/

  2. https://gist.github.com/matthewoden/b29353e266c554e04be8ea2058bcc2a0

  3. https://www.geeksforgeeks.org/how-to-git-clone-a-remote-repository/

  4. Arch Linux Documentation - Cgit

  5. 搭建 Git 伺服器并利用 cgit 作为前端

  6. 使用 cgit 托管小小的也很可爱的 Git 服务器

  7. Index: Cgit

  8. 如何运行syntax-highlighting.sh命令来检查cgit是否正常工作?