> For the complete documentation index, see [llms.txt](https://jun-wang.gitbook.io/learnjava/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jun-wang.gitbook.io/learnjava/kai-fa/huan-jing-da-jian/linux-da-jian-git-fu-wu-qi.md).

# Linux搭建git服务器

## 1. 安装git

```
$ sudo apt-get install git
# 查看git版本
$ git --version
git version 2.20.1
```

## 2. 新建git用户

为git仓库专门创建一个用户和用户目录，方便管理git仓库，可以省略

```
# 创建用户
sudo useradd git
# 设置密码
sudo passed git
# 创建用户目录
mkdir /home/git
```

## 3. 创建git仓库

```
$ git init --bare test.git
Initialized empty Git repository in /home/git/test.git/
```

至此git远程仓库就建好了。

## 4. 本地clone

```
 git clone git@10.235.111.242:/home/git/test.git
```

这样就clone了一个git仓库，之后就可以添加文件，进行后续的`commit` `push`等操作了。

## 5. 生成ssh公钥

由于每次pull，push等操作都需要输入密码，我们可以生成一个ssh公钥：

```
ssh-keygen -t rsa -C "XXXX@XXX.com"
```

创建好后会在user/.ssh/下有一个`id_rsa`和`id_rsa.pub`，`id_rsa.pub`就保存了我们的公钥，拷贝出来，粘贴在服务器的`/home/git/.ssh/authorized_keys`文件下保存即可，没有这个文件则新建。后面pull和push就不需要输入密码了。

## 6. 支持http协议

目前我们搭建的git服务器只支持ssh协议，但是我们很多情况下需要使用http协议，比如：

```
git clone http://ip/project/name.git
```

我们可以使用nginx来配合git搭建http协议的git服务器（经过使用Apache搭建失败的痛苦经历后，转战nginx）。

**安装nginx和fcgiwarp**

```
apt-get install nginx fcgiwrap
```

**配置nginx**

编辑`/etc/nginx/sites-enabled/default`

```
location ~ /git(/.*){
        # 使用 Basic 认证
        auth_basic "Restricted";
        # 认证的用户文件
        auth_basic_user_file /home/git/conf/gituser.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;
        fastcgi_param PATH_INFO           $1;
        # 将认证用户信息传递给fastcgi程序
        fastcgi_param REMOTE_USER $remote_user;
        # 包含默认的fastcgi参数
        include       fastcgi_params;
        # 将允许客户端post的最大值调整为100M
        client_max_body_size 100M;
    }
```

**创建nginx认证用户文件**

```
touch /home/git/conf/gituser.passwd
```

可以使用`htpasswd`命令来创建认证用户， 如果服务器上没有这个命令的话， 可以输入命令 `apt-get install apache2-utils` 来安装这个命令， 安装了这个命令之后， 就可以使用它来创建认证用户了， 比如要创建用户 user1， 输入命令如下：

```
htpasswd /home/git/conf/gituser.passwd user1
```

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

**修改git仓库权限**

通过http协议进行`clone`,`push`等操作等保证git仓库有对应的权限，使用`git init`创建的仓库默认权限是`drwxr-xr-x`需要给group组合other赋予读写权限：

```
chmod a+rw -R test.git
```

**重启nginx并测试**

```
nginx -s reload
git clone http://ip/git/test.git
```

## 7. 遇到的问题

## 7.1 配置pub\_key后clone还是需要输入密码

一开始搭建的时候出现这个问题，重新生成了一个公钥并拷贝到`authorized_keys`文件下解决。后来鼓捣了半天http协议，结果git协议clone又需要输入密码了，这次重新生成的方法失效了。找了好多资料，折腾了大半天，才发现是`/home/git`目录的权限问题，在linux中所有的用户目录权限默认是`drwxr-xr-x`如果更改了用户目录权限就会引起很多问题，之前搞http擅自更改了权限才导致的这次折腾。参考：<https://blog.csdn.net/jacky0922/article/details/17999271>

**注意：在authorized\_keys添加另一个pub\_key需要重起一行，否则会鉴权失败**

> 参考：
>
> <https://gitee.com/progit/4-%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git.html>
>
> <https://blog.csdn.net/LMXQH/article/details/80792135>
>
> <https://beginor.github.io/2016/03/12/http-git-server-on-nginx.html>
>
> <https://blog.csdn.net/m0_38092942/article/details/80699464>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jun-wang.gitbook.io/learnjava/kai-fa/huan-jing-da-jian/linux-da-jian-git-fu-wu-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
