搭建nginx+git+cgit环境

发布: 2013-07-27 15:58

1、环境目标
*) 通过http协议操作git库,
*) 支持基本的git库用户认证
*) 通过http协议访问git库展示,类似gitview功能

2、基础环境与包安装
centos-6, 64位
nginx-1.4.1

3、安装带完整dav支持的nginx
由于nginx包自带的dav模块不是完整的dav协议实现,
还需要用于一个额外的nginx模块,nginx-dav-ext-module。
下载nginx-1.4.1.tar.gz,解压,
./configure --add-module=/path/to/nginx-dav-ext-module
make && make install

配置编译好的nginx,并启动,

listen 8080;
server_name code.qtchina.net;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_ext_methods PROPFIND OPTIONS;

# turn on auth_basic
auth_basic "Private area";
auth_basic_user_file "/data1/SVN/passwd.conf";
create_full_put_path on;
dav_access group:rw all:r;

location /git/ {
root /data1;
index index.html index.htm;
autoindex on;
}

现在可以通过http://code.qtchina.net/git/xxx访问git库了。
git clone http://code.qtchina.net/git/xxx
git add xxx
git commit -a -m "xxx"
git push origin master

4、安装cgit fastcgi处理进程
这一步为了实现页面的gitview功能,像浏览代码树,查看提供记录与提交日志,查看提交代码改动。
下载cgit-0.9.2.tar.xz,git-1.8.3.4.tar.gz
解压cgit后,到cgit源代码目录解压git源代码,并把目录名改为git。
make V=1
编译完成,当前文件生成了可执行文件cgit,可尝试执行./cgit输出HTML代码。
把它拷贝到/path/to/nginx/install/path/cgi-bin/目录。
开始启动cgit了,这要需要用到spawn-fcgi,yum install spawn-fcgi。
配置/etc/sysconfig/spawn-fcgi文件,
SOCKET=/var/run/cgit-fcgi.sock
OPTIONS="-u nobody -g nobody -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/fcgiwrap"
启动后的socket文件信息,启动参数信息。
/etc/init.d/spawn-fcgi start

修改nginx配置,添加这一个location:
location /cgit/ {
root /usr/local/nginx/fcgi-bin;
fastcgi_pass unix:/var/run/cgit-fcgi.sock;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# Tell nginx to consider everything after /git as PATH_INFO. This way
# we get nice, clean URL’s
fastcgi_split_path_info ^(/cgit)(/?.+)$;

# Unfortunately the version of fcgiwrap currently available in Debian # squeeze removes the PATH_INFO variable from the CGI environment and # sets a new one based on DOCUMENT_ROOT and SCRIPT_NAME, so the line # below won’t work #fastcgi_param PATH_INFO $fastcgi_path_info; # Tell fcgiwrap about the binary we’d like to execute and cgit about # the path we’d like to access.
fastcgi_param SCRIPT_NAME /cgit$fastcgi_path_info;

include fastcgi_params;
}​

重启nginx后,通过浏览器访问,http://code.qtchina.net/cgit/已经能显示了,
但是显示没有项目,列表为空。

5、接下来配置cgit的项目,
cgit默认的配置文件放在/etc/cgitrc文件
# prepend this string to every url
virtual-root=/cgit

# title, heading, desc, about...
root-title=201 develop server git repositories
root-desc=testing purpose, try it just now. git clone http://code.qtchina.com/git/pubtest.git; cd pubtest; echo "123" >> abc.t\
xt;
#root-readme=/files/web/example.com/code/about.html
root-readme=/usr/local/nginx/fcgi-bin/cgit_about.html

# styling
#css=/htdocs/cgit/cgit.css
#logo=/htdocs/cgit/cgit.png

#
# "STATIC" REPOSITORIES
#
# Add them one by one, or include a file...
# section=Hosted repos
​# First repository
#repo.url=
#repo.owner=
#repo.path=/path/to/git_repo/.git
#repo.desc=A test project.

# admtools
repo.url=admtools
repo.owner=git201
repo.path=/data1/git/admtools.git
repo.desc=Develop server admin tools.

repo.url=couchbase-devel
repo.owner=git201
repo.path=/data1/git/couchbase-devel.git
repo.desc=
​ 这时再访问上一步的cgit页面时,已经会出现两个项目了。
注意,在配置完成后,会依旧显示不出来项目详细信息,需要到每个git库目录执行:
cd putest.git/
git update-server-info
这个命令生成refs/info文件,给cgit或者其他的git代码浏览工具使用。

6、现在已经能通过http协议操作git库,并能通过cgit浏览代码改动和提交相关信息了。
现在的认证方式只使用了http basic auth模式,比较简单。
其他的认证方式有数据库模式,或者证书模式,再以后有需要的时候研究补充。


nginx-dav-ext-module
cd putest.git/
git update-server-info

/etc/sysconfig/spawn-fcgi:
SOCKET=/var/run/cgit-fcgi.sock
OPTIONS="-u nobody -g nobody -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/fcgiwrap"

参考:
https://github.com/pmirshad/cgit-on-nginx/wiki/Installing-cgit-with-nginx-on-Ubuntu-11.10
cgitrc.5.txt


原文: http://qtchina.tk/?q=node/737

Powered by zexport