利用Nginx搭建HTTP訪問(wèn)的Git服務(wù)器過(guò)程記錄。搭建 Git 倉(cāng)庫(kù),實(shí)現(xiàn) SSH 協(xié)議、配合 Nginx 實(shí)現(xiàn) HTTP 協(xié)議拉取、推送代碼。利用 Nginx 實(shí)現(xiàn) Gitweb 在線瀏覽代碼,使用 Gitweb-theme 更新默認(rèn) Gitweb 樣式。
一. 準(zhǔn)備工作:
1. 下載nginx并安裝
推薦到nginx官方網(wǎng)站下載并安裝,有很詳細(xì)的教程. 參考資料: http://nginx.org/en/linux_packages.html
(1). 編輯repo文件,這里以64位的CentOS 7為示例:
> vi /etc/yum.repos.d/nginx.repo
[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/7/x86_64/gpgcheck=0enabled=1
(2). 保存退出后,使用yum安裝.可以選擇把動(dòng)態(tài)模塊也安裝上,具體參見(jiàn)上述站點(diǎn)
> yum install nginx -y
2. 下載git并安裝
(1).不推薦使用yum安裝的git版本,過(guò)低了.到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). 這個(gè)時(shí)候,git安裝好了,可以選擇較高的穩(wěn)定版本.我用的時(shí)候2.11.1
3. 下載spawn-fcgi, fcgi-devel, fcgiwrap并安裝
(1). 安裝spawn-fcgi.github地址: https://github.com/lighttpd/spawn-fcgi
這里需要注意的是,如果你沒(méi)有安裝前面的automake和gcc,請(qǐng)這里一定要把這些依賴安裝好.
> 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的運(yùn)行用戶, Git倉(cāng)庫(kù)初始化
> 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. 編寫(xiě)fcgiwrap啟動(dòng)腳本
> vi /etc/init.d/fcgiwrap
腳本內(nèi)容:
#! /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 INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binSPAWN_FCGI="/usr/local/bin/spawn-fcgi"DAEMON="/usr/local/sbin/fcgiwrap"NAME="fcgiwrap"PIDFILE="/var/run/$NAME.pid"FCGI_SOCKET="/var/run/$NAME.socket"FCGI_USER="git"FCGI_GROUP="git"FORK_NUM=15SCRIPTNAME=/etc/init.d/$NAMEcase "$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運(yùn)行的用戶,組以及進(jìn)程數(shù)(進(jìn)程數(shù)按需調(diào)整).需要與之后配置的nginx的worker用戶一樣.
記得修改一下讀寫(xiě)權(quán)限以及設(shè)置腳本為開(kāi)機(jī)啟動(dòng).然后我們啟動(dòng)fastcgi
> chmod a+x /etc/init.d/fcgiwrap> chkconfig --level 35 fcgiwrap on> /etc/init.d/fcgiwrap start
3. nginx配置. yum安裝的nginx已經(jīng)默認(rèn)配置了WebDAV模塊,所以不用麻煩了.如果發(fā)現(xiàn)沒(méi)有WebDAV模塊的功能,可以參考nginx的官方文檔中Dynamic Modules的說(shuō)明: http://nginx.org/en/docs/ngx_core_module.html#load_module
(1). 創(chuàng)建授權(quán)文件夾以及git的nginx設(shè)置文件
> mkdir -p /usr/local/nginx/config> vi /etc/nginx/conf.d/git.conf
內(nèi)容如下:
server { listen 80; server_name gitServer; root /usr/local/share/gitweb; client_max_body_size 100m; auth_basic "Git User Authentication"; auth_basic_user_file /usr/local/nginx/config/pass.db; location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ { root /data/git; } location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ { root /data/git; fastcgi_pass unix:/var/run/fcgiwrap.socket; fastcgi_connect_timeout 24h; fastcgi_read_timeout 24h; fastcgi_send_timeout 24h; fastcgi_param SCRIPT_FILENAME /usr/local/libexec/git-core/git-http-backend; fastcgi_param PATH_INFO $uri; fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param GIT_PROJECT_ROOT /data/git; fastcgi_param REMOTE_USER $remote_user; include fastcgi_params; } try_files $uri @gitweb; location @gitweb { fastcgi_pass unix:/var/run/fcgiwrap.socket; fastcgi_param GITWEB_CONFIG /etc/git/gitweb.conf; fastcgi_param SCRIPT_FILENAME /usr/local/share/gitweb/gitweb.cgi; fastcgi_param PATH_INFO $uri; include fastcgi_params; }}
(2). 修改/etc/nginx/nginx.conf中的worker進(jìn)程所有者.
# 將此處的nginx用戶修改為git用戶,以保證能調(diào)用到fastcgi(需要和fcgiwrap腳本中的FCGI_USER保持一致)
user git;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;}
4. 安裝http-tools并添加認(rèn)證用戶
> yum -y install httpd-tools> cd /usr/local/nginx/config> htpasswd -c pass.db guestUser
> git config --global color.ui true
> git config --global user.name 'git'
> git config --global user.email
5. 配置gitweb,首先要確定默認(rèn)安裝的gitweb(采用源碼安裝git才會(huì)有)是否存在
> find /usr/local/share -name gitweb.cgi> cd /usr/local/share/gitweb && ll /usr/local/share/gitweb> vi /etc/git/gitweb.conf
gitweb.conf的配置內(nèi)容如下:
# path to git projects (<project>.git)$projectroot = "/data/git";# directory to use for temp files$git_temp = "/tmp";# target of the home link on top of all pages$home_link = $my_uri || "/";# html text to include at home page$home_text = "indextext.html";# file with project list; by default, simply scan the projectroot dir.$projects_list = $projectroot;# javascript code for gitweb$javascript = "static/gitweb.js";# stylesheet to use$stylesheet = "static/gitweb.css";# logo to use$logo = "static/git-logo.png";# the 'favicon'$favicon = "static/git-favicon.png";
三. 啟動(dòng)nginx,fastcgi
> nginx -t> systemctl start nginx> /etc/init.d/fcgiwrap start
四. 問(wèn)題收集:
1. 訪問(wèn)http://hostname/repo.git出現(xiàn)502錯(cuò)誤,nginx錯(cuò)誤日志中出現(xiàn):connect() to unix:/var/run/fcgiwrap.socket failed (13: Permission denied) while connecting to upstream
解決方法: 檢查selinux是否開(kāi)啟,如果開(kāi)啟,請(qǐng)關(guān)閉或者配置策略使其能被訪問(wèn).
2. Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendorperl /usr/share/perl5/vendorperl /usr/lib/perl5 /usr/share/perl5 .) BEGIN failed--compilation aborted.
解決方法: yum -y install perl-CPAN
3. Can't locate CGI.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendorperl /usr/share/perl5/vendorperl /usr/lib/perl5 /usr/share/perl5 .) BEGIN failed--compilation aborted.
解決方法: yum -y install perl-CGI
4. Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/gitweb/gitweb.cgi line 20.
解決方法: yum -y install perl-Time-HiRes
五. Gitweb-theme 樣式
如果覺(jué)得 gitweb 默認(rèn)樣式不好看,可以拿該樣式替換
> cd /usr/local/src> git clone https://github.com/kogakure/gitweb-theme.git> cd gitweb-theme# -t 指定 gitweb 根目錄,一路 y 即可> ./setup -vi -t /usr/local/share/gitweb --install
Git 教程系列文章:
GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm
Git使用圖文詳細(xì)教程 http://www.linuxidc.com/Linux/2016-11/136781.htm
Ubuntu Git安裝與使用 http://www.linuxidc.com/Linux/2016-11/136769.htm
Git 標(biāo)簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm
Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm
Git 遠(yuǎn)程倉(cāng)庫(kù)詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm
Git 本地倉(cāng)庫(kù)(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm
Git 服務(wù)器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm
Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm
分享實(shí)用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm
Git從入門(mén)到學(xué)會(huì) http://www.linuxidc.com/Linux/2016-10/135872.htm
Git基本操作詳解 http://www.linuxidc.com/Linux/2016-10/135691.htm
Git創(chuàng)建遠(yuǎn)程倉(cāng)庫(kù)實(shí)例 http://www.linuxidc.com/Linux/2017-02/140737.htm
Git 的詳細(xì)介紹:請(qǐng)點(diǎn)這里
Git 的下載地址:請(qǐng)點(diǎn)這里
本文永久更新鏈接地址:http://www.linuxidc.com/Linux/2017-02/140779.htm
聯(lián)系客服