# 在EC2上创建Git仓库 ## 1. 创建主仓库 在EC2上建立一个中央仓库。SSH到EC2实例后 ``` bash mkdir repo-name.git cd repo-name.git git init --bare ``` 一个bare仓库是不能进行commit的,只是当作一个中央仓库存在。所有工作都应该先在本地的仓库中完成后push进来。 --- ## 2. 在本地push/pull 本地git仓库是通过ssh从服务器push/pull的,又因为EC2必须有私钥,所以先执行一步添加私钥的操作。 ``` bash eval "$(ssh-agent -s)" ssh-add /path/to/the/private/key ``` 然后在本地建立另一个工作仓库, ``` bash 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根目录下)这一系列动作。 ``` bash 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`,要修改如下。其中`css`和`logo`是https访问时的静态资源的url后缀,`scan-path`是服务器上git repo的绝对路径,`clone-prefix`是别的用户想通过https clone的时候的url前缀。因为我想通过 https://dizong.top/git 来访问我的仓库,所以`virtual-root`写的是`/git/`。 ``` bash # # 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`。 ``` bash sudo chown ubuntu:www-data /var/www/repo ``` 然后再检查权限设置就是正确的了。 ``` bash 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`的这一行来设置的。这是个官方写好的高亮脚本。根据[文档](https://wiki.archlinux.org/title/Cgit),高亮脚本也有.sh版本可以选择,但是我实测.sh脚本版本太旧不兼容,所以还是.py好。 此外,如[这里](https://dev59.com/unix/DUbRa4cB1Zd3GeqPwgu1)所说,这一行一定要放在`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`的无穷嵌套找不到正确的路径。 ``` nginx 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. 2. 3. 4. [Arch Linux Documentation - Cgit](https://wiki.archlinux.org/title/Cgit) 5. [搭建 Git 伺服器并利用 cgit 作为前端](https://leanhe.dev/posts/2021.05.02.1/) 6. [使用 cgit 托管小小的也很可爱的 Git 服务器](https://blog.dejavu.moe/posts/hosting-minimal-git-server-with-cgit/) 7. [Index: Cgit](https://git.zx2c4.com/cgit/about/) 8. [如何运行syntax-highlighting.sh命令来检查cgit是否正常工作?](https://dev59.com/unix/DUbRa4cB1Zd3GeqPwgu1)