首页 > Personal > git > Centos下使用Nginx搭建git http服务,支持gitlist访问
2014
08-22

Centos下使用Nginx搭建git http服务,支持gitlist访问

要使Nginx支持git-core的git-http-backend需要fcgiwrap,centos的yum里没有,就需要下载代码编译安装了

首先安装fcgi-devel
yum install fcgi-devel

下载代码
cd /usr/local/src
git clone git://github.com/wy182000/fcgiwrap.git fcgiwrap.git

编译安装
cd fcgiwrap.git
yum install autoconf
yum install automake
autoreconf -i
./configure
make
make install

安装spawn-fcgi,可以以daemon来运行fcgiwrap
yum install spawn-fcgi

修改/etc/sysconfig/spawn-fcgi为
FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS=”-M 0700″
OPTIONS=”-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid — $FCGI_PROGRAM”

添加spawn-fcgi开机启动
chkconfig spawn-fcgi on

启动spawn-fcgi
service spawn-fcgi start

修改git工作目录所有权
chown nginx:nginx -R your_git_path

在nginx的conf.d下添加git.conf
server {
listen 80;
server_name your_hostname;

# 用户认证,passwd使用htpasswd生成,和apache一样,htpasswd可以从soft.vpser.net/lnmp/ext/htpasswd.sh获取
auth_basic “Git Authorize”;
auth_basic_user_file /home/data/passwd;

# 访问地址为http://your_hsotname/xxxx.git
location ~ (/.*) {
# disalbe gzip
gzip off;

# fastcgi sockt
fastcgi_pass unix:/var/run/fcgiwrap.socket;

# fastcgi parameters
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param DOCUMENT_ROOT /usr/libexec/git-core/;
fastcgi_param SCRIPT_NAME git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL “”;
# git的工作目录
fastcgi_param GIT_PROJECT_ROOT /home/data/work;
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
}
}
这样,使用git clone http://your_hsotname/xxxx.git就可以checkout代码了。

这里只是能够下载代码,但是并不能够在网页上像github一样查看,我一般都会使用GitList来做网页浏览。
到官网下载最新的安装包
解压到/var/www/gitlist
cd /var/www/gitlist
mkdir cache
chmod 777 cache
mv config.ini-example config.ini
修改config.ini下git工作目录为正确工作目录

nginx的git.conf配置需要相应修改
server {
listen 80;
server_name your_hostname;

# 设置为gitlist目录,index为index.php
root /var/www/gitlist;
index index.php;

# 用户认证,passwd使用htpasswd生成,和apache一样,htpasswd可以从soft.vpser.net/lnmp/ext/htpasswd.sh获取
auth_basic “Git Authorize”;
auth_basic_user_file /home/data/passwd;

# 为了git clone能够正常使用,还需要用到git-http-backend,但是请求路径不再是(/.*)了
location ~ ^(.*/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]/[0-9a-f]|pack/pack-[0-9a-f]\.(pack|idx))|git-(upload|receive)-pack))$ {
# disalbe gzip
gzip off;

# fastcgi sockt
fastcgi_pass unix:/var/run/fcgiwrap.socket;

# fastcgi parameters
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param DOCUMENT_ROOT /usr/libexec/git-core/;
fastcgi_param SCRIPT_NAME git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL “”;
fastcgi_param GIT_PROJECT_ROOT /home/data/work;
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
}

# php相关配置
location ~* ^/index.php.*$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

# if you’re using php5-fpm via tcp
fastcgi_pass 127.0.0.1:9000;

include /etc/nginx/fastcgi_params;
}

# 所有请求重定向到@gitlsit
location / {
try_files $uri @gitlist;
}

# 图片的处理
location ~* ^/.*\.(js|css|png|jpg|jpeg|gif|ico)$ {
add_header Vary “Accept-Encoding”;
expires max;
try_files $uri @gitlist;
tcp_nodelay off;
tcp_nopush on;
}

# @gitlist请求,rewrite到index.php
location @gitlist {
rewrite ^/.*$ /index.php;
}
}

如果客户端git push时,出现错误
git error: RPC failed; result=22, HTTP code = 411
修改git配置,增大http buffer
git config –global http.postBuffer 524288000

如果错误为HTTP code = 413
修改服务器nginx配置,
vim /etc/nginx/nginx.conf
在http{}中添加
client_max_body_size 500m;

最后,如果nginx error里出现 connect() failed (111: Connection refused) while connecting to upstream,
一般是没有安装php-fpm
yum install fpm
就可以了。

最后编辑:
作者:wy182000
这个作者貌似有点懒,什么都没有留下。

留下一个回复