在Ubuntu14.04上部署使用SMART HTTP的Git服务器
打开Ubuntu14.04的终端
执行以下命令
$ sudo apt-get install git-core
$ sudo apt-get install apache2 apache2-utils
分别安装Git与Apache
$ a2enmod cgi alias env
该操作将启用mod_cgi、mod_alias与mod_env等Apache模块
接着需要向Apache配置文件添加一些内容,使git-http-backend作为Web服务器对/git路径请求的处理器。
使用以下命令打开Apache的默认配置文件
$ sudo vim /etc/apache2/sites-available/000-default.conf
在元素
SetEnv GIT_PROJECT_ROOT /opt/git
SetEnv GIT_HTTP_EXPORT_ALL
SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
若SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER这环境变量没被配置,将导致git push总是返回403错误
为了让Apache接受通过改路径的请求,添加以下内容至配置文件
<Directory "/usr/lib/git-core*">
Options ExecCGI Indexes
Order allow,deny
Allow from all
Require all granted
</Directory>
希望实现写操作授权验证,添加以下未授权屏蔽配置
<LocationMatch "^/git/.*/git-receive-pack$">
AuthType Basic
AuthName "Git Access"
AuthUserFile /opt/git/.htpasswd
Require valid-user
</LocationMatch>
通过以上配置,指定了git的库的路径为/opt/git,合法用户的密码文件为/opt/git/.htpasswd
创建git文件夹与空库目录,并把空库目录的所有者设置为www-data用户,让Apache有权限读写
$ sudo mkdir /opt/git
$ sudo mkdir /opt/git/test.git
$ cd /opt/git/test.git
$ git init --bare
$ sudo chown -R www-data:www-data .
创建密码文件,并添加用户abc
$ htpasswd -c /opt/git/.htpasswd abc
接着系统会询问密码,输入此用户使用的密码即可,需要重输一次作为验证
添加新用户def,添加新用户一样需要输入密码
$ htpasswd /opt/git/.htpasswd def
接着重启Apache
$ sudo service apache2 restart
接着可以在客户端以下命令拷贝库
$ git clone http://abc:{password}@{address}/git/test.git
{password}与{address}自行替换为用户密码与git所在服务器的地址。
修改库内容并本地提交后,可以通过以下命令
$ git push
把修改推送至服务器的库中
以上内容通过参考以下链接并实践获得。
https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-Smart-HTTP
http://superuser.com/questions/933858/git-push-gives-403-error