ubuntu14 apache/nginx 配置http git服务器

用 Apache 的 Basic 认证 + git-http-backend 实现,使用 git-http-backend 搭建 git 服务的原理都是类似的, 主要是利用 web 服务器 (apache/nginx) 进行用户认证, 并将用户信息传递给 CGI 程序 git-http-backend , 从而实现通过 http 完成 git 操作。

安装 git-core、 nginx 和 fcgiwrap

输入下面的命令安装需要的这三个软件包:

apt-get install git-core nginx fcgiwrap

我的目的是在 nginx 的默认网站下添加一个虚拟目录 /git/ , 通过访问 /git/xxx.git 的形式来访问服务器上的 xxx.git 代码库, 这就需要修改一下 nginx 默认网站的配置文件 /etc/nginx/sites-available/default , 添加下面的信息:

配置以 /git 开始的虚拟目录

location ~ /git(/.*) {
    # 使用 Basic 认证
    auth_basic "Restricted";
    # 认证的用户文件
    auth_basic_user_file /etc/nginx/passwd;
    # FastCGI 参数
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
    fastcgi_param GIT_HTTP_EXPORT_ALL "";
    # git 库在服务器上的跟目录
    fastcgi_param GIT_PROJECT_ROOT    /var/git-repos;
    fastcgi_param PATH_INFO           $1;
    # 将认证用户信息传递给 fastcgi 程序
    fastcgi_param REMOTE_USER $remote_user;
    # 包涵默认的 fastcgi 参数;
    include       fastcgi_params;
    # 将允许客户端 post 的最大值调整为 100 兆
    max_client_body_size 100M;
}

创建 nginx 认证用户文件

参考 nginx ngx http auth basic module , 用户认证文件格式如下: name1:password1 name2:password2:comment name3:password3

可以使用 htpasswd 命令创建用户, 如果服务器上没有这个命令的话, 可以输入命令

apt-get install apache2-utils

来安装这个命令, 安装了这个命令之后, 就可以使用它来创建认证用户了, 比如要创建用户 user1, 输入命令如下:

htpasswd /etc/nginx/passwd user1

然后根据提示输入密码就可以了。

创建 git 代码库

上面配置的 git 跟目录是 /var/git-repos , 我们在这个目录下初始化一个空的代码库, 命令如下:

cd /var/git-repos && git init --bare test.git

注意检查一下 test.git 的权限, 如果权限不足的话, 使用这个命令设置一下权限:

chmod a+rw -R test.git

重启 nginx 并测试输入命令

重启 nginx 并测试 git 服务:

nginx -s reload

git clone https://server-name/git/test.git

 

# You may add here your
# server {
#    ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
    listen 81 default_server;
    listen [::]:81 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #    proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #    root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #    # With php5-cgi alone:
    #    fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
    #    fastcgi_index index.php;
    #    include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}
location ~ /git(/.*) {
    # 使用 Basic 认证
    auth_basic "Restricted";
    # 认证的用户文件
    auth_basic_user_file /etc/nginx/passwd;
    # FastCGI 参数
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
    fastcgi_param GIT_HTTP_EXPORT_ALL "";
    # git 库在服务器上的跟目录
    fastcgi_param GIT_PROJECT_ROOT    /home/git/jgit;
    fastcgi_param PATH_INFO           $1;
    # 将认证用户信息传递给 fastcgi 程序
    fastcgi_param REMOTE_USER $remote_user;
    # 包涵默认的 fastcgi 参数;
    include       fastcgi_params;
    # 将允许客户端 post 的最大值调整为 100 兆
    # max_client_body_size 100M;
}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen 8000;
#    listen somename:8080;
#    server_name somename alias another.alias;
#    root html;
#    index index.html index.htm;
#
#    location / {
#        try_files $uri $uri/ =404;
#    }
#}


# HTTPS server
#
#server {
#    listen 443;
#    server_name localhost;
#
#    root html;
#    index index.html index.htm;
#
#    ssl on;
#    ssl_certificate cert.pem;
#    ssl_certificate_key cert.key;
#
#    ssl_session_timeout 5m;
#
#    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#    ssl_prefer_server_ciphers on;
#
#    location / {
#        try_files $uri $uri/ =404;
#    }
#}

hett:{SHA}0sKDLVkfU/1hB8kzTtIA3q28ys8=
git:{SHA}0sKDLVkfU/1hB8kzTtIA3q28ys8=

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##
    
    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
server {
    listen 443;
    index index.html index.php;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    location = /404.html {
          root /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    #访问形式 https://stroller.vip/git/test.git
    location  ~ /git(/.*) {
                # 使用 Basic 认证
                auth_basic "Restricted";
                # 认证的用户文件
                auth_basic_user_file /etc/nginx/passwd;
                # FastCGI 参数
                fastcgi_pass  unix:/var/run/fcgiwrap.socket;
                fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
                fastcgi_param GIT_HTTP_EXPORT_ALL "";
                # git 库在服务器上的跟目录
                fastcgi_param GIT_PROJECT_ROOT    /home/git/jgit/;
                fastcgi_param PATH_INFO          $1;
                # 将认证用户信息传递给 fastcgi 程序
                fastcgi_param REMOTE_USER $remote_user;
                # 包涵默认的 fastcgi 参数;
                include       fastcgi_params;
                # 将允许客户端 post 的最大值调整为 100 兆
                # client_max_body_size 500m;
        }
}
}


#mail {
#    # See sample authentication script at:
#    # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#    # auth_http localhost/auth.php;
#    # pop3_capabilities "TOP" "USER";
#    # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#    server {
#        listen     localhost:110;
#        protocol   pop3;
#        proxy      on;
#    }
#
#    server {
#        listen     localhost:143;
#        protocol   imap;
#        proxy      on;
#    }
#}

posted @ 2019-08-12 14:17  李悠然  阅读(649)  评论(0编辑  收藏  举报