CentOS替代之路:Rocky Linux 9.1 搭建基于cgit的Git网页版

简介cgit是一款用C语言开发的轻量级、速度超快的Git网页前端,很多开源项目的Git发布平台都是基于cgit。cgit也非常适合中小团队内部使

简介

cgit是一款用C语言开发的轻量级、速度超快的Git网页前端,很多开源项目的Git发布平台都是基于cgit。cgit也非常适合中小团队内部使用,部署也非常简单。

cgit官网网站:https://git.zx2c4.com/cgit/about/

部分使用cgit的项目:

https://git.kernel.org/

https://git.busybox.net/buildroot/

https://git.yoctoproject.org/

CentOS替代之路:Rocky Linux 9.1 搭建基于cgit的Git网页版

Linux内核源码Git网页版

安装Nginx

cgit是一个cgi程序,需要通过Web服务器配合实现web访问功能,因此需要首先安装Web服务器,一般推荐使用Nginx搭配fcgiwrap来实现cgi的支持。因为Rocky Linux 9自带的Nginx版本挺新的,所以我们直接使用dnf进行安装(默认版本为:1.20.1):

sudo dnf -y update
sudo dnf -y install nginx fcgiwrap httpd-tools

sudo systemctl enable nginx
sudo systemctl enable fcgiwrap@nginx.socket
sudo systemctl enable fcgiwrap@nginx.service
sudo systemctl start nginx
sudo systemctl startfcgiwrap@nginx.socket
sudo systemctl startfcgiwrap@nginx.service

sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=https --permanent
sudo firewall-cmd --reload 

安装cgit

cgit目前还没有RHEL9的安装包,只能从源码进行编译和安装,从官网网站下载最新的1.2.3版本源码包:

cd ~/Downloads/
wget https://git.zx2c4.com/cgit/snapshot/cgit-1.2.3.tar.xz
tar xf cgit-1.2.3.tar.xz
cd cgit-1.2.3

make get-git
make
sudo make install

配置Nginx

sudo vim /etc/nginx/nginx.conf

配置如下:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    client_max_body_size 1024M;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_redirect off;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /var/www/htdocs/cgit;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
           # error_page 418 = @auth;
           # if ( $query_string = "service=git-receive-pack" ) {  return 418; }
           # if ( $uri ~ "git-receive-pack#34; ) { return 418; }

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/htdocs/cgit/cgit.cgi;
            fastcgi_param PATH_INFO       $uri;
            fastcgi_param QUERY_STRING    $args;
            fastcgi_pass  unix:/run/fcgiwrap/fcgiwrap-nginx.sock;
        }

        location @auth {
            auth_basic "Pushing To Git Repositories Is Restricted";
            auth_basic_user_file /srv/git/htpasswd;

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
            fastcgi_param GIT_HTTP_EXPORT_ALL "";
            fastcgi_param GIT_PROJECT_ROOT /srv/git;
            fastcgi_param PATH_INFO $uri;
            fastcgi_param REMOTE_USER $remote_user;
            fastcgi_pass  unix:/run/fcgiwrap/fcgiwrap-nginx.sock;
        }
        
        error_log /var/log/nginx/error.log warn;

    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

其中增加了对git push上传的身份验证,使用的是Nginx的auth_basic,示例中用户名为eurekait,密码为123456。配置完成后重启Nginx服务:

sudo htpasswd -cbm /srv/git/htpasswd eurekait 123456
sudo systemctl restart nginx.service 

cgit配置

sudo mkdir /srv/git
sudo vim /etc/cgitrc

配置如下,参考的官方示例配置:

#
# See cgitrc(5) or /usr/share/doc/cgit/cgitrc.5.html for details
#

# Enable caching of up to 1000 output entries
cache-size=100

# Specify some default clone prefixes
#clone-prefix=git://example.com ssh://example.com/pub/git https://example.com/git

# Specify the css url
css=/cgit.css

# Show owner on index page
enable-index-owner=0

# Allow http transport git clone
#enable-http-clone=1

# Show extra links for each repository on the index page
#enable-index-links=0

# Enable blame page and create links to it from tree page
#enable-blame=0

# Enable ASCII art commit history graph on the log pages
#enable-commit-graph=0

# Show number of affected files per commit on the log pages
#enable-log-filecount=0

# Show number of added/removed lines per commit on the log pages
#enable-log-linecount=0

# Sort branches by age or name
#branch-sort=name

# Add a cgit favicon
favicon=/favicon.ico

# Use a custom logo
logo=/cgit.png

# Enable statistics per week, month, quarter, or year
#max-stats=

# Set the title and heading of the repository index page
root-title=EurekaIT Git Repository Browser

# Set a subheading for the repository index page
root-desc=EurekaIT's Internal Git Repositorys

# Include some more info about this site on the index page
#root-readme=/var/www/html/about.html

# Allow download of tar.gz, tar.bz2 and zip-files
#snapshots=tar.gz tar.bz2 zip

##
## List of common mimetypes
##

mimetype.gif=image/gif
mimetype.html=text/html
mimetype.jpg=image/jpeg
mimetype.jpeg=image/jpeg
mimetype.pdf=application/pdf
mimetype.png=image/png
mimetype.svg=image/svg+xml

# Enable syntax highlighting (requires the highlight package)
#source-filter=/usr/libexec/cgit/filters/syntax-highlighting.sh

# Format markdown, restructuredtext, manpages, text files, and html files
# through the right converters
#about-filter=/usr/libexec/cgit/filters/about-formatting.sh

##
## Search for these files in the root of the default branch of repositories
## for coming up with the about page:
##
#readme=:README.md
#readme=:readme.md
#readme=:README.mkd
#readme=:readme.mkd
#readme=:README.rst
#readme=:readme.rst
#readme=:README.html
#readme=:readme.html
#readme=:README.htm
#readme=:readme.htm
#readme=:README.txt
#readme=:readme.txt
#readme=:README
#readme=:readme
#readme=:INSTALL.md
#readme=:install.md
#readme=:INSTALL.mkd
#readme=:install.mkd
#readme=:INSTALL.rst
#readme=:install.rst
#readme=:INSTALL.html
#readme=:install.html
#readme=:INSTALL.htm
#readme=:install.htm
#readme=:INSTALL.txt
#readme=:install.txt
#readme=:INSTALL
#readme=:install

##
## List of repositories.
## PS: Any repositories listed when section is unset will not be
##     displayed under a section heading
## PPS: This list could be kept in a different file (e.g. '/etc/cgitrepos')
##      and included like this:
##        include=/etc/cgitrepos
##

#repo.url=foo
#repo.path=/var/lib/git/foo.git
#repo.desc=the master foo repository
#repo.owner=fooman@example.com
#repo.readme=info/web/about.html

virtual-root=/
scan-path=/srv/git/

这里需要注意一下,selinux策略对fcgiwrap-nginx.sock访问有限制,需要设置一下/etc/selinux/config中的SELINUX=permissive,这里临时修改一下:

sudo setenforce 0

测试一下效果

克隆cgit的git仓库来验证一下各项配置,首先建立服务器上的git仓库:

cd /srv/git
sudo git init --bare cgit
cd cgit
sudo git update-server-info

克隆cgit并推送至服务器:

cd ~/Downloads/
git clone --bare git://git.zx2c4.com/cgit
cd cgit.git
git push --mirror /srv/git/cgit

最后登录网页看一下效果:

CentOS替代之路:Rocky Linux 9.1 搭建基于cgit的Git网页版

虚拟机安装,IP为192.168.112.141

总结

cgit速度很快,很多大型项目都在用它,但功能相对单一,仅能用于Web查看Git仓库,缺少对issue的管理,一般需要搭配其他项目管理工具。如果服务器性能允许的情况下,推荐使用gitlab自建一个git网页服务器,非常的好用,下期将介绍一下Gitlab的安装和使用,欢迎和我交流,共同进步。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/10271.html

(0)
上一篇 2023年 4月 22日 下午11:57
下一篇 2023年 4月 22日 下午11:57

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信