原先在公司内网为了安全考虑,安装了最简单的基于ssh访问的git,但是集团这几天把终端ssh给禁用了,只能通过堡垒机访问。这也导致了之前的git服务访问不到了,不能提交和拉取代码了。
于是就将git的访问方式改为http,具体步骤记录如下:
一、服务器安装Git(这在之前已经安装好了,在这里再写一遍)
1、在服务器上安装Git
yum -y install git
2、安装完后,查看 Git 版本
git --version
3、服务器端创建 Git 仓库(设置/data/git_fileshare/fileshare.git 为 Git 仓库)
mkdir -p /data/git_fileshare/fileshare.git
git init --bare fileshare.git
chown -R apache:apache /data/git_fileshare (这个在安装好httpd之后执行)
二、安装和配置httpd服务
1、安装httpd
yum -y install httpd
2、修改httpd端口
vim /etc/httpd/conf/httpd.conf
Listen 16999
3、创建test账号
创建第一个用户:htpasswd -m -c /etc/httpd/conf.d/git-team.htpasswd test1
创建第n个用户:htpasswd -m /etc/httpd/conf.d/git-team.htpasswd testn
这边稍作下解释:
-c:创建一个新文件。注意:在添加完第一个用户之后,以后添加的每一个用户都不要加这个参数,不然会覆盖密码验证文件,导致前面已经添加的都没有了。。
-m:强制对密码进行MD5加密(默认)
test1和testn:新加的用户,可以写成任意名字
4、修改git-team.htpasswd文件的所有者与所属群组
chown apache:apache /etc/httpd/conf.d/git-team.htpasswd
5、设置git-team.htpasswd文件的访问权限
chmod 640 /etc/httpd/conf.d/git-team.htpasswd
6、在apache中新建git.conf,将请求转发到git-cgi
vim /etc/httpd/conf.d/git.conf
<VirtualHost *:16999>
ServerName 10.86.219.162
SetEnv GIT_HTTP_EXPORT_ALL
SetEnv GIT_PROJECT_ROOT /data/git_fileshare
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
<Location />
AuthType Basic
AuthName "Git"
AuthUserFile /etc/httpd/conf.d/git-team.htpasswd
Require valid-user
</Location>
</VirtualHost>
# ServerName是git服务器的域名,这里写上IP即可
# /data/git_fileshare是代码库存放的文件夹
# ScriptAlias是将以/git/开头的访问路径映射至git的CGI程序git-http-backend
# AuthUserFile是验证用户帐户的文件
7、启动httpd服务
service httpd restart
三、客户端克隆
git clone http://10.86.219.162:16999/git/fileshare.git
在弹出的对话框中输入用户名密码即可。