0%

在宝塔面板使用nginx搭建Gitweb服务

为了更加方便的访问服务器上的git仓库,搭建Gitweb服务。Gitweb是一个Git网络接口。它是用Perl编写的,可以用作CGI脚本。它允许使用Web浏览器浏览git仓库。本文介绍如何在宝塔面板下使用nginx搭建Gitweb服务。

软件安装

Gitweb

安装git时,默认会安装Gitweb。如果没有,可以使用包管理器安装。

Ubuntu:

1
apt-get install gitweb

CentOS:

1
yum -y install gitweb

gitweb安装完成之后,需要确定gitweb.cgi所在的路径。从locate命令的输出结果看,gitweb所在的目录是/usr/local/git/share/gitweb。记住这个路径,后面会用到。

1
2
3
[root@localhost ~]# updatedb
[root@localhost ~]# locate gitweb.cgi
/usr/local/git/share/gitweb/gitweb.cgi

设置projectroot

更改/ect/gitweb.conf,将$projectroot设置为git仓库所在的目录。

1
our $projectroot = "/path/to/gitrepo";

fastcgi

1
yum -y install fcgi-devel spawn-fcgi
1
apt-get install fcgiwrap spawn-fcgi

fastcgi-wrapper

Ubuntu可以跳过此步。

1
2
3
4
5
6
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoconf -i
./configure
make
make install

启动fastcgi-wrapper。可以任意选择一个空闲的端口,需要在宝塔面板放开端口。注意根据实际情况更改fcgiwrap的路径。

1
spawn-fcgi -f /usr/local/sbin/fcgiwrap -p 8050

设置Gitweb网站

在宝塔面板新建一个网站,更改网站设置。将index更改为gitweb.cgiroot更改为gitweb所在的路径。当访问cgi结尾的文件时,通过fastcgi-wrapper响应,注意这里的8050端口。保存设置,就可以访问网站了。

fastcgi-wrapper开机自启动

如果要访问gitweb网站,fcgiwrap一定要处于开启状态。为了保证服务器重启之后gitweb还能正常访问,需要添加开机自启动。这里选择使用systemd来完成。新增文件/lib/systemd/system/fcgi.service,并在文件写入以下内容,保存退出。

根据实际情况修改spawn-fcgifcgiwrap的路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=fastcgi-wrapper
After=network.target
Conflicts=shutdown.target

[Service]
Type=forking
User=root
Group=root
Restart=always
RestartSec=1
ExecStart=/usr/bin/spawn-fcgi -f /usr/local/sbin/fcgiwrap -p 8050

[Install]
WantedBy=multi-user.target

然后就可以使用systemctl来管理fcgi服务了。

1
2
3
4
5
6
7
8
# 开机自启动
systemctl enable fcgi
# 手动启动
systemctl start fcgi
# 停止服务
systemctl stop fcgi
# 查看服务状态
systemctl status fcgi

Gitweb美化

从网站gitweb-theme下载主题文件,将解压后的文件放入/usr/local/git/share/gitweb/static目录。注意备份原始文件。

修复中文乱码

中文路径乱码

打开一些中文目录或文件时,网页会乱码。修改gitweb.cgi文件,将所有的file_name=>"$basedir$t->{'name'}"替换为file_name=>"$basedir".to_utf8($t->{'name'})。可以使用下面的sed命令完成替换。

1
sed -i 's/'file_name\=\>\"\$basedir\$t-\>{\'name\'}\"/file_name\=\>\"\$basedir\".to_utf8\(\$t-\>{\'name\'}\)'/g' /usr/local/git/share/gitweb/gitweb.cgi

中文内容乱码

点击raw按钮查看文件的内容,中文显示为乱码。更改gitweb.cgi,将

1
our $default_text_plain_charset  = undef;

更改为

1
our $default_text_plain_charset  = 'utf8';

最终效果

参考