Nginx 本身不是 Git 服務,但可搭配 fcgiwrap 與 git-http-backend,讓用戶透過 HTTP 存取 Git 倉庫。本文整理 CentOS 7 的實作流程。
可先設定 nginx repo,再以 yum 安裝。Git 建議使用較新版本,可從官方原始碼編譯。
yum install nginx -y 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
yum -y install epel-release yum -y install fcgi-devel git clone https://github.com/lighttpd/spawn-fcgi.git cd spawn-fcgi ./autogen.sh ./configure make make install git clone https://github.com/gnosek/fcgiwrap.git cd fcgiwrap autoreconf -i ./configure make make install
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
建立 /etc/init.d/fcgiwrap,設定 FCGI_USER 與 FCGI_GROUP 為 git,並啟動。
chmod a+x /etc/init.d/fcgiwrap chkconfig --level 35 fcgiwrap on /etc/init.d/fcgiwrap start
在 /etc/nginx/conf.d/git.conf 中設定 server、client_max_body_size、auth_basic,以及 git-http-backend 的 fastcgi_param。重點包括 GIT_PROJECT_ROOT、GIT_HTTP_EXPORT_ALL 與 SCRIPT_FILENAME。
nginx.conf 的 user 需與 fcgiwrap 執行用戶一致,才能避免 socket 權限錯誤。
yum -y install httpd-tools htpasswd -c /usr/local/nginx/config/pass.db guestUser
gitweb 設定檔通常放在 /etc/git/gitweb.conf,需指定 projectroot 為 /data/git。
nginx -t systemctl start nginx /etc/init.d/fcgiwrap start
若出現 502,多為 socket 權限或 SELinux 問題。若出現 Perl 模組缺失,可安裝 perl-CPAN、perl-CGI、perl-Time-HiRes 等套件。
若想更換 gitweb 主題,可 clone gitweb-theme 後執行 setup 安裝。
git clone https://github.com/kogakure/gitweb-theme.git cd gitweb-theme ./setup -vi -t /usr/local/share/gitweb --install