git服务器的搭建- smart http协议(Apache网页版)和ssh协议两种方式进行搭建

闽承望
2023-12-01
  • git服务器搭建过程

  A.首先要选择搭建git时的协议,在文档里有三种协议

      1.本地协议:就是多人使用一台电脑,就是使用不同的账号登录同一台电脑,使用共享文件来同时进行操作,很危险。使用命令为:

git clone /opt/git/project.git

      2.HTTP 协议: http协议又分为两种 一种为智能(Smart) HTTP 协议 和 哑(Dumb) HTTP 协议 命令类似于

git clone https://example.com/gitproject.git

     3.ssh协议: 架设 Git 服务器时常用 SSH 协议作为传输协议,优点大多数环境下已经支持通过 SSH 访问,并且其普遍性,架设和使用都很容易。命令类似于:

git clone ssh://user@server/project.git

本文选则两种方式搭建git服务器  一种为ssh服务 一种为智能http服务, 在下篇文章中将为大家介绍gitlab的搭建以及使用(和Apache同时使用是有坑的)

本文意指想让搭建会自己手动搭建git服务器

 B.服务器环境配置:

 Ubuntu 14.04    Windows10  

C.ssh搭建过程

第一步,首先安装git应用:

 sudo apt-get install git

第二步,添加git用户  输入git密码,然后一路回车,在/home/下面会多一个文件夹,git

sudo adduser git 

第三步,创建证书登录:

收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个.  id_rsa.pub文件在 例如我的在/home/lijingle/.ssh/目录下,如果没有可以通过下面命令进行创建

ssh-keygen -t rsa -C "xiaoming@qq.com"

如果/home/git下面没有.ssh/authorized_keys文件 就用root用户新建一个。

此时还需要修改配置:

 $ sudo vim /etc/ssh/sshd_config
  RSAAuthentication yes 
  PubkeyAuthentication yes
  AuthorizedKeysFile      /home/git/.ssh/authorized_keys

//ssh服务重启
sudo /etc/init.d/ssh restart

第四步,初始化git仓库

cd /srv
git init --bare test.git
chown -R www-data:www-data test.git

第五步,禁用shell登录:

出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:

git:x:1001:1001:,,,:/home/git:/bin/bash

改为:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

第六步:

基本完成可以进行克隆仓库:

git clone git@192.168.156.32:/srv/test.git
Cloning into 'test'...
warning: You appear to have cloned an empty repository.

到这里ssh协议基本搭建完成。

D.http网页版搭建过程

第一步和第二步与上述一致 ,另外需要安装Apache和gitweb

apt-get install gitweb
apt-get install apache2

第三步就是配置Apache 打开/etc/apache2/apache2.conf配置文件

# 默认apache2只对两个目录(/usr/share和/var/www)有访问权限,如下指令赋予apache2对/srv的访问权限。
<Directory /srv/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# 如果没有这句,在其他机器上执行git clone等命令会返回403错误,参照最后一条“参考”
<Location />
    Options +ExecCGI
    Require all granted
</Location>

# 设置git的工程目录
SetEnv GIT_PROJECT_ROOT /srv/
# 默认情况下,含有git-daemon-export-ok文件的目录才可以被导出(用作git库目录)。设置这个环境变量以便所有目录都可以被导出
SetEnv GIT_HTTP_EXPORT_ALL

# 虚拟主机,匹配80端口的任何ip地址的请求,访问gitweb
<virtualhost *:80>
    # 顺便在/etc/hosts里添加上一句:127.0.0.1 git.example.com。这样,在服务器上可以通过该名字访问这个页面
    ServerName git.example.com
    DocumentRoot /usr/share/gitweb
    ErrorLog ${APACHE_LOG_DIR}/git_error.log
    CustomLog ${APACHE_LOG_DIR}/git_access.log combined
</virtualhost>

# gitweb目录添加ExecCGI的功能
<Directory /usr/share/gitweb>
    Options FollowSymLinks ExecCGI
    AddHandler cgi-script .cgi
    DirectoryIndex gitweb.cgi
</Directory>

# 对git库的各种请求,执行git-http-backend.cgi
ScriptAliasMatch \
    "(?x)^/(.*/(HEAD | \
    info/refs | \
    objects/(info/[^/]+ | \
     [0-9a-f]{2}/[0-9a-f]{38} | \
     pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
    git-(upload|receive)-pack))$" \
    /usr/lib/git-core/git-http-backend/$1
# 其余的请求,执行gitweb.cgi
ScriptAlias / /usr/share/gitweb/gitweb.cgi

# 设置git push等操作的认证方式为文件认证,/var/www/git-auth后面会创建。
<LocationMatch "^/.*/git-receive-pack$">
    AuthType Basic
    AuthName "Git Access"
    Require valid-user
    AuthBasicProvider file
    AuthUserfile /var/www/git-auth
</LocationMatch>

然后/srv/test.git/config,添加如下内容:

[http]
    receivepack = true

如果不加上面这句,git clone下来的版本库,git push时会提示403错误,即没有授权

第四步,配置gitweb服务   修改/etc/gitweb.conf中的一句

$projectroot = "/srv"

第五步重启Apache2

service apache2 restart

第六步 使用http协议下载代码

git clone http://server-ip/test.git test-repo

第七步  在浏览器上输入http://server-ip,查看刚才的操作是否记录到gitweb上了。

 类似资料: