使用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源码

注意libfcgi安装时如果出现fcgio.cpp:50:14: error: 'EOF' was not declared in this scope overflow(EOF) 错误,需要在fcgio.cpp的前面添加#include <stdio.h>,fcgio.cpp在libfcgi目录下

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/

猜你喜欢

转载自blog.csdn.net/name_is_wl/article/details/86705032