在Debian 8上安装 Nginx HTTP Git服务器
Anne655
・4 分钟阅读
Git是一个版本控制系统(VCS ),它支持跟踪代码的变化,在本教程中,我们将介绍安装HTTP(S)Git服务器,并添加用户名/密码验证。
前提条件
- Debian 8杰西
- sudo
- (nano,vim )的文本编辑器,
安装所需软件
我们需要nginx
,git
,fcgiwrap
和apache httpd工具,
sudo apt-get install nginx git fcgiwrap apache2-utils
如果另一个进程(如Apache )已绑定到端口80,就
dpkg
将无法安装Nginx。
创建Git目录
假设你希望在/var/www/git
处创建git目录,则需要运行以下命令:
mkdir /var/www/git
chown www-data:www-data /var/www/git # Make sure www-data (the user fastcgi runs) from has permissions.
配置Nginx
现在我们需要配置Nginx来传递Git流量到Git ,可以将它添加到默认配置中,即/etc/nginx/conf.d
或/etc/nginx/sites-enabled
的自定义server {}
。
必须按照下面列出的顺序添加文件和指令的配置参数。
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"/var/www/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 /var/www/git; # /var/www/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
}
如果你想让你的git仓库在一个子目录中,http://your-domain.com/repos
制作第一行 location ~ /repos(/.*) {
这是个正规表达式。
确保
server {}
中的server_name
指令不会与其他任何内容冲突,否则Nginx不会传递。
添加密码身份验证
Nginx接受Apache htpasswd
文件,创建它们时,我们需要执行以下命令:
htpasswd -c /var/www/git/htpasswd <your username>
你将被提示输入密码,要添加更多用户,请执行以下操作:
htpasswd /var/www/git/htpasswd <another username>
最后一步
确保通过运行下面的命令来重新加载Nginx,以便应用所有的更改:
sudo service nginx reload
你现在拥有了私有的Git服务器!好好享受吧。
(可选)创建初始化Git存储库的脚本
请注意,www-data (FastCGI在下面运行的用户帐户)必须具有对Git存储库的读写访问权限,创建脚本后,会方便很多。
打开你的脚本文件,/var/www/git/gitinit.sh
,并且粘贴以下内容:
#!/bin/sh
sudo -u www-data mkdir $1
cd $1
sudo -u www-data git init --bare
你可以像这样运行脚本:
cd /var/www/git
./gitinit.sh repo-name
记得用下面命令允许可执行:
chmod +x /var/www/git/gitinit.sh