使用nginx搭建http 訪問的git服務器

一般git安裝完成之後都是使用ssh協議拉取和推送git服務器,如果需要使用http協議的方式,需要一個http容器和額外的配置。這裏使用nginx作爲http容器。

首先需要安裝gitnginx,接着需要安裝 libfcgi-dev、 autoconf 、libtool、 automake具體過程請參考https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/

如果想通過源碼安裝需要下載對應的源碼,這裏提供一下源碼路徑

libfcgi源碼autoconf源碼libtool源碼automake源碼

https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/   中安裝過程主要有以下幾點需要注意

1.安裝fcgiwrap之前需要先安裝pkg-config

 pkg-config源碼

安裝方式

./configure --with-internal-glib
make
make install

2.啓動腳本的bin路徑一定要正確,並且不要忘記給腳本執行的權限

我的fcgiwrap在/usr/local/sbin/目錄下,所以 $bin_path修改爲$bin_path='/usr/local/sbin/fcgiwrap'

3.將啓動腳本放在rc.local下,這樣每次開機就會自動啓動

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/local/sbin/sshd
/usr/local/nginx/sbin/nginx
/etc/init.d/fcgiwrap
exit 0

現在執行這個腳本

root@ubuntu:/# /etc/init.d/fcgiwrap 
root@ubuntu:/#
root@ubuntu:/# ps aux | grep fcg
root       2174  0.0  0.1   6444  1324 pts/1    S    15:59   0:00 /usr/local/sbin/fcgiwrap
root      14500  0.0  0.0   6444   672 pts/1    S    16:58   0:00 /usr/local/sbin/fcgiwrap
root      14502  0.0  0.2  15956  2228 pts/1    S+   16:59   0:00 grep --color=auto fcg
root@ubuntu:/#

啓動後會在/tmp目錄下生成cgi.sock文件,注意啓動用戶爲root。

安裝完成,現在需要配置nginx

在conf目錄中的nginx.conf文件加入如下配置

 server {
        listen      8000;
        server_name localhost;
        root /home/git/repo;

        client_max_body_size 100m;

        auth_basic "git";
        auth_basic_user_file /usr/local/nginx/conf/pass.db;

        location ~(/.*) {
           fastcgi_pass  unix:/tmp/cgi.sock;
           fastcgi_param SCRIPT_FILENAME   /usr/local/libexec/git-core/git-http-backend;
           fastcgi_param PATH_INFO         $1;
           fastcgi_param GIT_HTTP_EXPORT_ALL "";
           fastcgi_param GIT_PROJECT_ROOT  /home/git/repo;
           fastcgi_param REMOTE_USER $remote_user;
           include fastcgi_params;
        }
     }

listen 8000 表示監聽8000端口

server_name localhost表示域名localhost

root  /home/git/repo   表示請求路徑都會到/home/git/repo下進行匹配

例如http://localhost:8000/blog.git 匹配到的的目錄是/home/git/repo/blog.git

auth_basic  "git"  表示用戶名校驗爲git (如果不需要校驗填寫off)

auth_basic_user_file /usr/local/nginx/conf/pass.db  爲htpasswd 對git和其密碼生成的密鑰文件。沒有安裝htpasswd 可以使用在線生成然後將文件拷貝到此(不需要校驗請填寫off)

在線生成網址http://tool.oschina.net/htpasswd

 

 

root@ubuntu:/usr/local/nginx/conf# echo "git:AWoU4Acdd8XLM" >> pass.db
root@ubuntu:/usr/local/nginx/conf#

其餘需要注意的是fastcgi_param SCRIPT_FILENAME   /usr/local/libexec/git-core/git-http-backend這個路徑要是正確的如果不知到git-http-backend在哪,可以查找一下

fastcgi_pass  unix:/tmp/cgi.sock就是之前執行啓動腳本後生成的文件路徑

保存文件重啓nginx

root@ubuntu:/usr/local/nginx/conf# ps aux | grep nginx
root       2229  0.0  0.0  22444   456 ?        Ss   16:11   0:00 nginx: master process ../sbin/nginx
root       2230  0.0  0.3  22836  3052 ?        S    16:11   0:00 nginx: worker process
root      14546  0.0  0.2  15956  2172 pts/1    S+   17:23   0:00 grep --color=auto nginx
root@ubuntu:/usr/local/nginx/conf#

注意工作進程用戶爲root,nginx默認爲nobody,修改配置nginx.conf

第一行加上

#user  nobody;
user  root;
worker_processes  1;

重啓,現在就可以使用http協議進行git相關操作了

Administrator@WL /d/workspace/wl/study/service/email-service (master)
$ git clone http://192.168.245.128:8000/blog.git
Cloning into 'blog'...
Username for 'http://192.168.245.128:8000': git
Password for 'http://[email protected]:8000':
remote: Counting objects: 2312, done.
remote: Compressing objects: 100% (1573/1573), done.
remote: Total 2312 (delta 680), reused 2114 (delta 594)R
Receiving objects: 100% (2312/2312), 8.88 MiB | 0 bytes/s, done.
Resolving deltas: 100% (680/680), done.

參考https://blog.csdn.net/bb2210083/article/details/82455747 

       https://www.howtoforge.com/tutorial/ubuntu-git-server-installation/

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