Almalinux9(CentOS9)搭建git服务器 Nginx+fcgi+spawn-fcgi+fcgiwrap+Git

Almalinux9(CentOS9)搭建git服务器 nginx http访问

Nginx+fcgi+spawn-fcgi+fcgiwrap+Git 

先安装依赖

dnf install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel wget -y
dnf -y install fcgi-devel

如果已经安装了git 先卸载 编译安装 才有gitweb

dnf remove git -y

先下载git源码  当前版本v2.38 可以选择最新稳定版

dnf install libcurl-devel curl-devel

cd /usr/local/src/
wget https://github.com/git/git/archive/refs/tags/v2.38.0.tar.gz
tar zxvf v2.38.0.tar.gz
cd git-2.38.0/
make configure
./configure --prefix=/usr/local/git
make
make install

添加git环境变量

echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
source /etc/profile

查看git是否安装成功

git --version

新版本会报错:git: 'remote-https' is not a git command

echo "export PATH=$PATH:/usr/local/git/libexec/git-core" >> /etc/profile
source /etc/profile

查看用户是否存在

id git
id nginx
useradd git -g git -s /sbin/nologin -M
groupadd git
useradd git -g git -s /sbin/nologin -M

下载spawn-fcgi 并且编译安装spawn-fcgi

cd /usr/local/src/
git clone https://github.com/lighttpd/spawn-fcgi.git
cd spawn-fcgi/
./autogen.sh
./configure
make && make install

下载fcgiwrap 并且编译安装 生成Makefile后编译Makefile文件 删除CFLAGS选项否则报错

-Werror

修改前

CFLAGS = -std=gnu99 -Wall -Wextra -Werror -pedantic -O2 -g3

修改后

CFLAGS = -std=gnu99 -Wall -Wextra  -pedantic -O2 -g3
cd /usr/local/src/
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap/
autoreconf -i
./configure
make && make install

添加服务 用来启动fcgiwrap.service

cd /etc/systemd/system/
vim fcgiwrap.service
[Unit]
Description=Simple CGI Server
After=nss-user-lookup.target
[Service]

ExecStart = /usr/local/sbin/fcgiwrap -f -s unix:/run/fcgiwrap.socket & 
[Install]
Also=fcgiwrap.socket


vim fcgiwrap.socket
[Unit]
Description=fcgiwrap Socket

[Socket]
ListenStream=/run/fcgiwrap.sock

[Install]
WantedBy=sockets.target

systemctl daemon-reload
systemctl start fcgiwrap.service
systemctl enable fcgiwrap.service

配置gtiweb.conf 添加内容如下

cd /usr/local/git/share/gitweb/
touch gitweb.conf
vim gitweb.conf

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

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

# target of the home link on top of all pages
$home_link = $my_uri || "/";

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

# file with project list; by default, simply scan the projectroot dir.
$projects_list = $projectroot;

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

# stylesheet to use
$stylesheet = "static/gitweb.css";

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

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

安装httpd-tools

dnf -y install httpd-tools

添加用户及密码验证文件 请在要存密码验证文件的目录执行此命令

htpasswd -bc pass.db test 123456

添加nginx的webgit.conf

vim /etc/nginx/conf/nginx.conf

server {
    listen      80;
    server_name localhost;
    root /usr/local/git/share/gitweb;

    client_max_body_size 100m;

    auth_basic "Git User Authentication";
    auth_basic_user_file /etc/nginx/conf/conf.d/pass.db;

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root /home/git;
    }

    location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root          /home/git;
        fastcgi_pass  unix:/run/fcgiwrap.socket;
        fastcgi_connect_timeout 24h;
        fastcgi_read_timeout 24h;
        fastcgi_send_timeout 24h;
        fastcgi_param SCRIPT_FILENAME   /usr/local/git/libexec/git-core/git-http-backend;
        fastcgi_param PATH_INFO         $uri;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT  /home/git;
        fastcgi_param REMOTE_USER $remote_user;
        include fastcgi_params;
    }

    try_files $uri @gitweb;

    location @gitweb {
        fastcgi_pass  unix:/run/fcgiwrap.socket;
        fastcgi_param GITWEB_CONFIG    /usr/local/git/share/gitweb/gitweb.conf;
        fastcgi_param SCRIPT_FILENAME  /usr/local/git/share/gitweb/gitweb.cgi;
        fastcgi_param PATH_INFO        $uri;
        include fastcgi_params;
    }
}

创建仓库repo.git

git init --bare repo.git
cd repo.git
mv hooks/post-update.sample hooks/post-update
git update-server-info

systemctl restart nginx

浏览器访问此仓库

http://192.168.1.3/repo.git

如果有如下报错:

Can't locate CGI.pm in @INC (you may need to install the CGI module

安装perl cgi

perl -MCPAN -e shell
install CGI::Session

更换好看点的皮肤

cd /usr/local/src
git clone https://github.com/kogakure/gitweb-theme.git
cd gitweb-theme
./setup -vi -t /usr/local/git/share/gitweb/ --install

安装好后就可以开心的撸代码了~。~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值