出自 Arch Linux 中文维基

Gitweb 是 Git 自帶的默認網絡界面。

Gitweb 實際上原生支持 FCGI,因此無需將其包裝成 CGI 腳本。[1]

安裝

要安裝 Gitweb,首先要安裝 git網絡伺服器。如果想快速測試,請參閱 git instaweb 的幫助。否則,如果你想要一個全面的設置,請繼續閱讀。

以下所有示例都需要安裝 perl-cgi 軟體包。(FS#45431)。

配置網絡伺服器

Apache

將以下內容添加到 /etc/httpd/conf/httpd.conf 的末尾:

Alias /gitweb "/usr/share/gitweb"
<Directory "/usr/share/gitweb">
   DirectoryIndex gitweb.cgi
   Options ExecCGI
   Require all granted
   <Files gitweb.cgi>
   SetHandler cgi-script
   </Files>
   SetEnv  GITWEB_CONFIG  /etc/gitweb.conf
</Directory>

或者將其添加到另一個文件中,如 etc/httpd/conf/extra/gitweb.conf 然後在 httpd.conf 末尾添加以下內容:

# gitweb configuration
Include conf/extra/gitweb.conf

如果 Apache 拒絕顯示 Gitweb,而是列印 perl 腳本的純原始碼,則很可能是 Apache 的配置不允許執行 CGI。請確保 httpd.conf 中的以下內容未被注釋:

<IfModule !mpm_prefork_module>
   LoadModule cgid_module modules/mod_cgid.so
</IfModule>
<IfModule mpm_prefork_module>
   LoadModule cgi_module modules/mod_cgi.so
</IfModule>

然後重啟 httpd.service 來應用這些更改。有關使用 Apache 執行 CGI 的更多詳情,請參閱 https://httpd.apache.org/docs/2.4/howto/cgi.html。

Lighttpd

/etc/lighttpd/lighttpd.conf 中添加以下內容:

server.modules += ( "mod_alias", "mod_cgi", "mod_redirect", "mod_setenv" )
url.redirect += ( "^/gitweb$" => "/gitweb/" )
alias.url += ( "/gitweb/" => "/usr/share/gitweb/" )
$HTTP["url"] =~ "^/gitweb/" {
       setenv.add-environment = (
               "GITWEB_CONFIG" => "/etc/gitweb.conf",
               "PATH" => env.PATH
       )
       cgi.assign = ( ".cgi" => "" )
       server.indexfiles = ( "gitweb.cgi" )
}

然後重啟 lighttpd.service 來應用這些更改。 您可能還需要在 mimetype.assign 行添加 ".css" => "text/css" 以便 GitWeb 正常顯示。

Nginx

將此位置添加到 nginx 配置中(可能需要更改位置):

/etc/nginx/nginx.conf
location /gitweb.cgi {
    include fastcgi_params;
    gzip off;
    fastcgi_param   SCRIPT_FILENAME  /usr/share/gitweb/gitweb.cgi;
    fastcgi_param   GITWEB_CONFIG  /etc/gitweb.conf;
    fastcgi_pass    unix:/run/fcgiwrap.sock;
}

location / {
    root /usr/share/gitweb;
    index gitweb.cgi;
}

如果您遵循 Nginx#CGI 實現,請嘗試將include fastcgi_params;替換為include fastcgi.conf;

配置 Gitweb

所有配置選項的列表,請參見 gitweb.conf(5)

基本配置

打開(如果不存在,則創建)文件 /etc/gitweb.conf,並將此內容放入其中:

/etc/gitweb.conf
# The directories where your projects are. Must not end with a slash.
our $projectroot = "/path/to/your/repositories"; 

# Base URLs for links displayed in the web interface.
our @git_base_url_list = qw(git://<your_server> http://git@<your_server>);

要啟用 "blame" 視圖(顯示源文件中每一行的作者),請添加以下一行:

$feature{'blame'}{'default'} = [1];

配置完成後,重啟網絡伺服器。

添加倉庫

要添加版本庫,請進入版本庫文件夾,像這樣創建版本庫:

$ mkdir my_repository.git
$ git init --bare my_repository.git/
$ cd my_repository.git/
$ touch git-daemon-export-ok
$ echo "Short project's description" > description

接下來打開 gitweb.config 文件並添加以下內容:

gitweb.config
[gitweb]
owner = Your Name

這將填寫 Gitweb 中的 "Owner" 欄位。這不是必填項。

這裡假定您希望將此倉庫作為 "central" 倉庫,將您的提交推送至此,因此 git-daemon-export-ok 和 --bare 的作用是將開銷降至最低,並允許在此倉庫上使用 git 守護進程。

這就是創建版本庫的全部過程。現在你可以在 http://localhost/gitweb 上看到它了(假設一切順利)。由於 Gitweb CGI 腳本只需讀取版本庫文件夾,因此無需為新版本庫重啟 Apache。

語法高亮

要在 Gitweb 上啟用語法高亮,必須先安裝 highlight 軟體包:

安裝 highlight 後,只需在 gitweb.conf 中添加此行即可:

$feature{'highlight'}{'default'} = [1];

保存文件後,高亮顯示就會啟用。

參見