1. 程式人生 > >騰訊雲Linux伺服器搭建(四) Git伺服器搭建(通過https驗證)

騰訊雲Linux伺服器搭建(四) Git伺服器搭建(通過https驗證)

先安裝git:

下載
#wget https://github.com/git/git/archive/v2.9.2.tar.gz
解壓
# tar zxvf v2.9.2.tar.gz
# cd git-2.9.2
編譯安裝
# make configure
# ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
# make all doc
# sudo make install install-doc install-html
修改環境變數
# sudo vim /etc/profile
在最後一行新增
export PATH=/usr/local/git/bin:$PATH
儲存後使其立即生效
# source /etc/profile
檢視是否安裝成功
#git --version
顯示 git version 2.9.2 ,安裝完成。

安裝apache:

安裝所需軟體包 ,gcc通常預設帶,估計雲主機為了省空間給省略了。openssl和openssl-devel也是必須的
#yum -y install gcc openssl openssl-devel
下載Apache原始碼,版本2.2.29
#wget  http://archive.apache.org/dist/httpd/httpd-2.2.29.tar.gz
編譯帶ssl,安裝到/usr/local/apache2目錄
#./configure --enable-ssl --enable-so -–prefix=/usr/local/apache2
#make & make install
正常安裝完成。

設定apache的自啟動:

先檢視啟動級別,不是3的話改為3
#vi /etc/inittab
id:3:initdefault:
直接將上述的apachectl檔案拷貝到 /etc/rc.d/init.d 目錄中,命名為習慣用的httpd
#cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
在指令碼上加上註釋,以便讓chkconfig認識
#vi /etc/rc.d/init.d/httpd
在指令碼第二行加上如下注釋,2345是預設執行級別,61啟動順序,39停止順序,可以自己指定
# chkconfig: 2345 61 39  #在指令碼第二行開始新增
# description: Apache #centos6以後版本可加可不加,最好加上
追加自啟動
# chkconfig --add httpd 
預設開啟2345執行級別,當然可以通過選項--level num指定執行級別
#chkconfig httpd on 
#chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

配置apache的https訪問:

安裝在阿里雲申請的SSL證書安裝
檔案說明:
1. 證書檔案1530652701965.pem,包含兩段內容,請不要刪除任何一段內容。
2. 如果是證書系統建立的CSR,還包含:證書私鑰檔案1530652701965.key、證書公鑰檔案public.pem、證書鏈檔案chain.pem。
( 1 ) 在Apache的安裝目錄下建立cert目錄,並且將下載的全部檔案拷貝到cert目錄中。如果申請證書時是自己建立的CSR檔案,請將對應的私鑰檔案放到cert目錄下並且命名為1530652701965.key;
( 2 ) 開啟 apache 安裝目錄下 conf 目錄中的 httpd.conf 檔案,找到以下內容並去掉“#”:
#LoadModule ssl_module modules/mod_ssl.so (如果找不到請確認是否編譯過 openssl 外掛)
#Include conf/extra/httpd-ssl.conf
( 3 ) 開啟 apache 安裝目錄下 conf/extra/httpd-ssl.conf 檔案 (也可能是conf.d/ssl.conf,與作業系統及安裝方式有關), 在配置檔案中查詢以下配置語句:
# 新增 SSL 協議支援協議,去掉不安全的協議
SSLProtocol all -SSLv2 -SSLv3
# 修改加密套件如下
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
# 證書公鑰配置
SSLCertificateFile cert/public.pem
# 證書私鑰配置
SSLCertificateKeyFile cert/1530652701965.key
# 證書鏈配置,如果該屬性開頭有 '#'字元,請刪除掉
SSLCertificateChainFile cert/chain.pem
( 4 ) 重啟 Apache。
( 5 ) 通過 https 方式訪問站點,正常顯示 It works!

設定訪問git伺服器的使用者名稱密碼

#建立git訪問的使用者名稱密碼檔案 
htpasswd -m -c /usr/local/apache2/conf/git-team.htpasswd zhang
chown apache:apache /usr/local/apache2/conf/git-team.htpasswd
chmod 640 /usr/local/apache2/conf/git-team.htpasswd
#建立了zhang的使用者名稱密碼,再追加使用者的話,不要加-c  -m是選擇MD5加密方式

建立空的git倉庫

mkdir /git-data/git/sample.git -p
cd /git-data/git/sample.git
git init --bare
ll 可以看到裡面的branches,hooks,hooks,info,objects,refs,config,description,HEAD

配置git的https訪問:

#開啟httpd.conf
vi /usr/local/apache2/conf/httpd.conf
找到下面內容,開啟註釋,沒有的話可以自己追加上。
#Include conf/extra/httpd-ssl.conf
#開啟/usr/local/apache2/conf/extra/httpd-ssl.conf
找到下面內容 <VirtualHost _default_:443>
在裡面追加下面內容
DocumentRoot "/git-data/git"
ServerName www.域名:443
ServerAdmin 郵件地址
SetEnv GIT_HTTP_EXPORT_ALL
SetEnv GIT_PROJECT_ROOT /git-data/git
ScriptAlias /git/ /usr/local/git/libexec/git-core/git-http-backend/
<Location /git>
    AuthType Basic
    AuthName "Git"
    AuthUserFile /usr/local/apache2/conf/git-team.htpasswd
    Require valid-user
</Location>

重啟apache: 

service httpd restart

確認可以通過Https訪問git。

git checkout https://域名/git/sample.git
#提示輸入使用者名稱密碼,輸入剛才建立的zhang和密碼,提示checkout了一個空資料夾

從個人電腦裡把原來git裡面的程式碼直接匯入到搭建的git伺服器上。

git clone --mirror 老repo的地址
cd 上面clone完的目錄
git remote set-url origin https://域名/git/sample.git
git push -f origin

到此為止,git伺服器(https驗證)搭建完畢。

整理一下遇到的坑:

1,編譯apache的時候必須指定ssl,否則只能重新編譯安裝。

2,把程式碼轉移到自己搭建的伺服器上時,可能會遇到  fatal: Unable to find remote helper for 'https' ,原因時curl-devel沒裝

#伺服器上安裝curl-devel
yum install curl-devel
#重新編譯安裝git後解決