利用Nginx搭建HTTP访问的Git服务器过程记录。搭建 Git 仓库,实现 SSH 协议、配合 Nginx 实现 HTTP 协议拉取、推送代码。利用 Nginx 实现 Gitweb 在线浏览代码,使用 Gitweb-theme 更新默认 Gitweb 样式。
一. 准备工作:
1. 下载nginx并安装
推荐到nginx官方网站下载并安装,有很详细的教程. 参考资料:
(1). 编辑repo文件,这里以64位的CentOS 7为示例:
> vi /etc/yum.repos.d/nginx.repo
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/x86_64/ gpgcheck=0 enabled=1
(2). 保存退出后,使用yum安装.可以选择把动态模块也安装上,具体参见上述站点
> yum install nginx -y
2. 下载git并安装
(1).不推荐使用yum安装的git版本,过低了.到github下载最新的git源码. 下载地址: https://github.com/git/git/releases
> yum -y remove git > yum -y install perl cpio autoconf tk zlib-devel libcurl-devel openssl-devel expat-devel gettext-devel perl-ExtUtils-MakeMaker automake gcc > cd /usr/local/src; wget https://github.com/git/git/archive/v2.11.1.tar.gz > tar zxf v2.11.1.tar.gz && cd git-2.11.1 > autoconf && ./configure && make && make install > git --version
(2). 这个时候,git安装好了,可以选择较高的稳定版本.我用的时候2.11.1
3. 下载spawn-fcgi, fcgi-devel, fcgiwrap并安装
(1). 安装spawn-fcgi.github地址: https://github.com/lighttpd/spawn-fcgi
这里需要注意的是,如果你没有安装前面的automake和gcc,请这里一定要把这些依赖安装好.
> cd /usr/local/src; > git clone https://github.com/lighttpd/spawn-fcgi.git > cd spawn-fcgi && ./autogen.sh && ./configure && make && make install
(2). 安装fcgi-devel.
安装前,需要先安装epel源,不然安装不了fcgi-devel
> yum -y install epel-release > yum -y install fcgi-devel
(3). 安装fcgiwrap. GitHub地址: https://github.com/gnosek/fcgiwrap
> cd /usr/local/src > git clone https://github.com/gnosek/fcgiwrap.git > cd fcgiwrap && autoreconf -i && ./configure && make && make install
二. 配置
1. 添加Git的运行用户, Git仓库初始化
> useradd -r -s /sbin/nologin git > mkdir -p /data/git && cd /data/git > git init --bare repo.git && chown -R git.git /data/git > cd repo.git && mv hooks/post-update.sample hooks/post-update > git update-server-info
2. 编写fcgiwrap启动脚本
> vi /etc/init.d/fcgiwrap
脚本内容:
#! /bin/bash ### BEGIN INIT INFO # Provides: fcgiwrap # Required-Start: $remote_fs # Required-Stop: $remote_fs # Should-Start: # Should-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: FastCGI wrapper # Description: Simple server for running CGI applications over FastCGI ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin SPAWN_FCGI="/usr/local/bin/spawn-fcgi" DAEMON="/usr/local/sbin/fcgiwrap" PIDFILE="/var/run/$NAME.pid" FCGI_SOCKET="/var/run/$NAME.socket" FCGI_USER="git" FCGI_GROUP="git" FORK_NUM=15 SCRIPTNAME=/etc/init.d/$NAME case "$1" in start) echo -n "Starting $NAME... " PID=`pidof $NAME` if [ ! -z "$PID" ]; then echo " $NAME already running" exit 1 fi $SPAWN_FCGI -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -P $PIDFILE -F $FORK_NUM -f $DAEMON if [ "$?" != 0 ]; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Stoping $NAME... " PID=`pidof $NAME` if [ ! -z "$PID" ]; then kill `pidof $NAME` if [ "$?" != 0 ]; then echo " failed. re-quit" exit 1 else rm -f $pid echo " done" fi else echo "$NAME is not running." exit 1 fi ;; status) PID=`pidof $NAME` if [ ! -z "$PID" ]; then echo "$NAME (pid $PID) is running..." else echo "$NAME is stopped" exit 0 fi ;; restart) $SCRIPTNAME stop sleep 1 $SCRIPTNAME start ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|status}" exit 1 ;; esac
注意其中"FCGI_USER"和"FCGI_GROUP"以及"FORK_NUM",分别为fastcgi运行的用户,组以及进程数(进程数按需调整).需要与之后配置的nginx的worker用户一样.
记得修改一下读写权限以及设置脚本为开机启动.然后我们启动fastcgi
> chmod a+x /etc/init.d/fcgiwrap > chkconfig --level 35 fcgiwrap on > /etc/init.d/fcgiwrap start