2024/7/16大约 2 分钟
在Ubuntu上部署Git Server
一、部署服务器
1.安装必需的软件包
首先,安装Git、Apache和GitWeb:
sudo apt install git gitweb apache2 -y2.配置GitWeb
sudo nano /etc/gitweb.confGitWeb通常在Apache的配置文件中定义。首先,编辑GitWeb配置文件:
$projectroot = "/srv/git";
$git_temp = "/tmp";
$home_link = $my_uri || "/";
$site_name = "My Git Server";3.配置Apache
编辑Apache的GitWeb配置文件:
sudo nano /etc/apache2/conf-available/gitweb.conf一般不需要修改,文件内容如下:
<IfModule mod_alias.c>
  <IfModule mod_mime.c>
    <IfModule mod_cgi.c>
      Define ENABLE_GITWEB
    </IfModule>
    <IfModule mod_cgid.c>
      Define ENABLE_GITWEB
    </IfModule>
  </IfModule>
</IfModule>
<IfDefine ENABLE_GITWEB>
  Alias /gitweb /usr/share/gitweb
  <Directory /usr/share/gitweb>
    Options +FollowSymLinks +ExecCGI
    AddHandler cgi-script .cgi
  </Directory>
</IfDefine>启用GitWeb配置和CGI模块,然后重启Apache:
sudo a2enconf gitweb
sudo a2enmod cgi
sudo systemctl restart apache24.测试配置
在浏览器中打开以下URL来访问GitWeb界面: http://your_server_ip/gitweb

二、GitServer管理
在Ubuntu上创建一个单独的git用户,用于专门维护Git服务器。这有助于提高安全性和简化权限管理。
1.创建git用户
使用以下命令创建一个名为git的新用户,并为其设置一个登录shell(通常为/usr/bin/git-shell,这将限制用户只能进行Git相关操作):
sudo adduser --system --shell /usr/bin/git-shell --group --disabled-password --home /home/git git2.初始化仓库
切换到git用户,初始化一个裸仓库:
sudo -u git -H sh -c 'cd /srv/git && git init --bare gittest.git'3.配置SSH访问(可选)
使用/usr/bin/git-shell作为git用户的shell将限制该用户只能执行Git操作,无法执行其他命令。这有助于增强安全性。如果需要进一步限制,考虑使用SSH的配置来限制允许的命令和操作。
如果希望通过SSH访问Git仓库,需要为git用户配置SSH公钥认证:
1.切换到git用户:
sudo -u git -H sh -c 'cd ~ && mkdir -p .ssh && chmod 700 .ssh'2.添加SSH公钥到authorized_keys文件中:
sudo nano /home/git/.ssh/authorized_keys4.测试
git clone git@your_server_ip:/srv/git/gittest.git