centos6安裝git服務器

一、卸載centos自帶的git:yum remove git -y

提前安裝 必要的環境:

yum install libcurl-dev libcurl-devel

yum install expat-devel

yum install perl-ExtUtils-MakeMaker package

yum install tcl build-essential tk gettext

如果在編譯過程中,遇到缺少lib的可以繼續安裝需要的lib環境。

二、下載git-2.11.0.tar.gz 上傳至服務器,下載鏈接:http://distfiles.macports.org/git/

三、解壓安裝git 並添加git到環境變量

cd /usr/local/src/
tar zxvf git-2.11.0.tar.gz
make prefix=/usr/local/git all
make prefix=/usr/local/git install
添加git到環境變量
    echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
    或者
    echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
source /etc/bashrc 或者 source /etc/profile

四、查看git版本號 

[root@localhost git-2.11.0]# git --version
git version 2.11.0

五、生成htpasswd的驗證文件。

先了解什麼是htpasswd?

htpasswd是httpd功能apache服務的一個工具,是目錄訪問權限認證的一個方式。可配合.htaccess文件,編寫成獨立的.htpasswd文件。在.htpasswd文件中賬號以 用戶名/加密密碼 形式存在,密碼支持MD5、crypt、sha、plain明文方式保存。該權限控制並不能保證絕對的密碼安全,網上會有破解教程,但仍然可以作爲基本的權限認證來使用,或者搭配網站功能的權限檢測作爲重要加密內容的雙保險。

#建立git訪問的用戶名密碼文件 
htpasswd -m -c /etc/httpd/conf/git-team.htpasswd zhang
chown apache:apache /etc/httpd/conf/git-team.htpasswd
chmod 640 /etc/httpd/conf/git-team.htpasswd
#創建了zhang的用戶名密碼,再追加用戶的話,不要加-c  -m是選擇MD5加密方式

六、啓用http和https訪問git

vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
   DocumentRoot "/var/www/html/git代碼路徑"
   ServerName gamebox.域名.com

   SetEnv GIT_PROJECT_ROOT /var/www/html/git代碼路徑
   SetEnv GIT_HTTP_EXPORT_ALL

   #安裝git-core後,用find命令查一下git-http-backend在哪裏
   ScriptAlias /git /usr/libexec/git-core/git-http-backend/
   Alias /gitweb /var/www/git   //這個是git自己的路徑

   #在文件/etc/gitweb.conf末尾另起一行加上 $projectroot = "/var/www/html/git代碼路徑";
   SetEnv GITWEB_CONFIG /etc/gitweb.conf   //這個是git自己的路徑

   <Directory "/var/www/git">  //這個是git自己的路徑
      Options ExecCGI Indexes FollowSymLinks SymLinksIfOwnerMatch
      Order allow,deny
      Allow from all
      AllowOverride All
      Require all granted
      AddHandler cgi-script cgi
      DirectoryIndex gitweb.cgi
   </Directory>

   <Location />
      DAV on
      AuthType Basic
      AuthName "Git Access"
      AuthUserFile /etc/httpd/conf/git-team.htpasswd  //http的htpasswd驗證文件路徑
      Require valid-user
   </Location>

</VirtualHost>

七、以上配置完畢,在 “/var/www/html/git代碼路徑”裏去生成git倉庫

mkdir /var/www/html/git代碼路徑/test.git -p
cd /var/www/html/git代碼路徑/test.git
git init --bare
ll命令可以看到裏面的branches,hooks,hooks,info,objects,refs,config,description,HEAD

八、配置完成後,一定要給git代碼目錄Apache所有者權限!!

本地就可以通過:

git clone https://gamebox.域名.com/git/test.git

來拉取代碼了,這裏是需要驗證的htpasswd賬號即可!訪問下面的地址可以查看網頁版的git

http://gamebox.域名.com/gitweb/

九、https的配置:

<VirtualHost *:443> 
ServerName gamebox.域名.com

DocumentRoot "/var/www/html/git代碼路徑"

 SSLEngine on 
 SSLProtocol all -SSLv2 -SSLv3
 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM 
 SSLCertificateFile cert/3004746_gamebox.域名.com_public.crt
 SSLCertificateKeyFile cert/3004746_gamebox.域名.com.key
 SSLCertificateChainFile cert/3004746_gamebox.域名.com_chain.crt

SetEnv GIT_HTTP_EXPORT_ALL
SetEnv GIT_PROJECT_ROOT /var/www/html/MiniGameGitHub
ScriptAlias /git /usr/libexec/git-core/git-http-backend/
Alias /gitweb /var/www/git
SetEnv GITWEB_CONFIG /etc/gitweb.conf

   <Directory "/var/www/git">
      Options ExecCGI Indexes FollowSymLinks SymLinksIfOwnerMatch
      Order allow,deny
      Allow from all
      AllowOverride All
      Require all granted
      AddHandler cgi-script cgi
      DirectoryIndex gitweb.cgi
   </Directory>

   <Location /git> //這裏的權限是任何人都可以下載,git默認是push代碼是需要驗證的,所以去除驗證
      #DAV on    //也就是保證不能push 
      #AuthType Basic
      #AuthName "Git"
      #AuthUserFile /etc/httpd/conf/git-team.htpasswd
      #Require valid-user
   </Location>

</VirtualHost> 

十、git的pull 和push權限分開也是可以做到的。請參考後面的文章!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章