在debian 10下面搭建nginx + git server

  |   0 评论   |   0 浏览

背景

自己搞个代码仓库。

初体验

环境准备

apt-get install nginx git fcgiwrap apache2-utils

创建git仓库

mkdir -p /home/git
chown www-data:www-data /home/git

配置nginx

location ~ (/.*) {
    client_max_body_size 0; # Git pushes can be massive, just to make sure nginx doesn't suddenly cut the connection add this.
    auth_basic "Git Login"; # Whatever text will do.
    auth_basic_user_file "/home/git/htpasswd";
    include /etc/nginx/fastcgi_params; # Include the default fastcgi configs
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; # Tells fastcgi to pass the request to the git http backend executable
    fastcgi_param GIT_HTTP_EXPORT_ALL "";
    fastcgi_param GIT_PROJECT_ROOT /home/git; # /home/git is the location of all of your git repositories.
    fastcgi_param REMOTE_USER $remote_user;
    fastcgi_param PATH_INFO $1; # Takes the capture group from our location directive and gives git that.
    fastcgi_pass  unix:/var/run/fcgiwrap.socket; # Pass the request to fastcgi
}

增加密码认证

htpasswd -c /home/git/htpasswd <your username>

重启nginx

service nginx reload

(可选)初始化仓库脚本

文件 /home/git/gitinit.sh

#!/bin/sh
sudo -u www-data mkdir $1
cd $1
sudo -u www-data git init --bare

加上执行权限

chmod a+x gitinit.sh

初始化一个仓库

./gitinit.sh repo-name

客户端使用

git clone http://xxx.xxx.com/repo-name

首次提交代码

1.git init

2.git remote add origin http://*****************************

3.git add .

4.git commit -m 'First'

5.git push -u origin --all

注:如果提示本地已存在存储库,可尝试将.git文件夹删除,重新从第一步开始执行

参考