用 Apache 的 Basic 認證 + git-http-backend 實現,使用 git-http-backend 搭建 git 服務的原理都是相似的, 主要是利用 web 服務器 (apache/nginx) 進行用戶認證, 並將用戶信息傳遞給 CGI 程序 git-http-backend , 從而實現經過 http 完成 git 操做。javascript
安裝 git-core、 nginx 和 fcgiwrapphp
輸入下面的命令安裝須要的這三個軟件包:css
apt-get install git-core nginx fcgiwrap
html
個人目的是在 nginx 的默認網站下添加一個虛擬目錄 /git/ , 經過訪問 /git/xxx.git 的形式來訪問服務器上的 xxx.git 代碼庫, 這就須要修改一下 nginx 默認網站的配置文件 /etc/nginx/sites-available/default , 添加下面的信息:java
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 ngx http auth basic module , 用戶認證文件格式以下: name1:password1 name2:password2:comment name3:password3node
能夠使用 htpasswd 命令建立用戶, 若是服務器上沒有這個命令的話, 能夠輸入命令nginx
apt-get install apache2-utils
git
來安裝這個命令, 安裝了這個命令以後, 就能夠使用它來建立認證用戶了, 好比要建立用戶 user1, 輸入命令以下:web
htpasswd /etc/nginx/passwd user1
apache
而後根據提示輸入密碼就能夠了。
上面配置的 git 跟目錄是 /var/git-repos , 咱們在這個目錄下初始化一個空的代碼庫, 命令以下:
cd /var/git-repos && git init --bare test.git
注意檢查一下 test.git 的權限, 若是權限不足的話, 使用這個命令設置一下權限:
chmod a+rw -R test.git
重啓 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;# }#}