本教程适用于以下操作系统版本

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)
  • Ubuntu 18.04 (Bionic Beaver)
  • Ubuntu 16.04 (Xenial Xerus)

本页内容

  • 先决条件
  • 安装 Nginx Web Server
  • 创建 Git 仓库
  • 配置 Nginx 以提供 Git 仓库服务
  • 使用 Let's Encrypt 保护 Git HTTP 服务器
  • 从客户端连接 Git 仓库
  • 结论

Git 是一个开源的版本控制系统,用于在源代码层面跟踪软件的变更。全球数以千计的开发人员使用它来跟踪和管理代码变更、回退到之前的阶段,以及创建文件和目录的替代版本。

HTTP Git Server 是一个开源项目,允许用户通过局域网(LAN)共享 Git 仓库。它非常简单、易于搭建,并帮助开发人员从命令行界面进行管理。

本教程将讲解如何在 Ubuntu 22.04 上使用 Nginx 搭建 HTTP Git 仓库服务器。

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 一个指向你服务器 IP 的有效域名。
  • 你的服务器上已配置 root 密码。

安装 Nginx Web Server

在本文中,我们将使用 Nginx 服务器来提供 Git 仓库服务,因此你需要在服务器上安装 Nginx Web 服务器及其他所需软件包。你可以使用以下命令安装所有这些软件包:

apt-get install nginx git fcgiwrap apache2-utils unzip -y

安装完所有软件包后,你可以继续创建 Git 仓库。

创建 Git 仓库

首先,在 Nginx 网站根目录下创建一个用于存储 Git 仓库的目录:

mkdir /var/www/html/gitrepo

接下来,进入 gitrepo 并为该用户创建另一个目录:

cd /var/www/html/gitrepo
mkdir hitesh.git

接下来,进入用户目录并使用以下命令初始化 Git 仓库:

cd hitesh.git
git --bare init

你将看到以下输出:

Initialized empty Git repository in /var/www/html/gitrepo/hitesh.git/

接下来,使用以下命令更新 Git 服务器信息:

git update-server-info

接下来,使用以下命令更改 gitrepo 的所有权并设置正确的权限:

chown -R www-data:www-data /var/www/html/gitrepo
chmod -R 755 /var/www/html/gitrepo

接下来,创建一个名为 hitesh 的用户并设置密码:

htpasswd -c /var/www/html/gitrepo/htpasswd hitesh

你可以按如下所示设置密码:

New password: 
Re-type new password: 
Adding password for user hitesh

你可以使用以下命令检查你的密码:

cat /var/www/html/gitrepo/htpasswd

示例输出:

hitesh:$vcr2$AdyCEkaA$Fsq5nDbLhBDdafCQGBUsr2

配置 Nginx 以提供 Git 仓库服务

接下来,创建一个 Nginx 虚拟主机配置文件,以便通过互联网提供 Git 仓库服务。

nano /etc/nginx/conf.d/gitrepo.conf

添加以下配置:

server {
        listen 80;

        root /var/www/html/gitrepo;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name git.example.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

location ~ (/.*) {
    client_max_body_size 0; 
    auth_basic "Git Login"; 
    auth_basic_user_file "/var/www/html/gitrepo/htpasswd";
    include /etc/nginx/fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; 
    fastcgi_param GIT_HTTP_EXPORT_ALL "";
    fastcgi_param GIT_PROJECT_ROOT /var/www/html/gitrepo;
    fastcgi_param REMOTE_USER $remote_user;
    fastcgi_param PATH_INFO $1; 
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
}

}

完成后保存并关闭文件,然后使用以下命令检查 Nginx 是否存在语法错误:

nginx -t

你将看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

最后,重启 Nginx 服务以应用更改:

systemctl restart nginx

你也可以使用以下命令检查 Nginx 状态:

systemctl status nginx

你将看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-11-05 08:00:04 UTC; 2s ago
       Docs: man:nginx(8)
    Process: 144985 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 144986 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 144987 (nginx)
      Tasks: 2 (limit: 2341)
     Memory: 2.5M
        CPU: 42ms
     CGroup: /system.slice/nginx.service
             ??144987 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??144988 nginx: worker process

Nove 5 08:00:04 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nove 5 08:00:04 ubuntu2204 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Nove 5 08:00:04 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

使用 Let's Encrypt 保护 Git HTTP 服务器

在开始之前,你需要在系统中安装 Certbot 客户端,以便安装和管理 Let's Encrypt SSL。你可以使用以下命令安装它:

apt-get install certbot python3-certbot-nginx -y

Certbot 客户端安装完成后,运行以下命令为你的网站下载并安装 Let's Encrypt SSL:

certbot --nginx -d git.example.com

按如下所示提供你的电子邮件地址并接受服务条款:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for git.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/git.conf

选择是否将 HTTP 流量重定向到 HTTPS:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入 2 并按回车键开始该过程。安装完成后,你应该会看到以下输出:

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/git.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://git.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=git.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/git.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/git.example.com/privkey.pem
   Your cert will expire on 2023-02-06. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

至此,你的 Git HTTP 服务器已通过 Let's Encrypt SSL 获得保护。

从客户端连接 Git 仓库

至此,Git HTTP 服务器已通过 Nginx 和 Let's Encrypt SSL 搭建完成。现在,是时候从客户端机器连接并进行测试了。

首先,在客户端机器上使用以下命令安装 Git 软件包:

apt-get install git -y

接下来,使用以下命令为你的项目创建一个目录:

mkdir project

接下来,进入你的项目目录并使用以下命令初始化 Git:

cd project
git init

接下来,使用你的电子邮件和用户名配置 Git:

git config --global user.email "
[email protected]
"
git config --global user.name "hitesh"

接下来,使用以下命令添加你的 Git HTTP 服务器:

git remote add origin https://
[email protected]
/hitesh.git

接下来,创建一个名为 data 的目录并在其中添加一个文件:

mkdir data
echo "This is my first application" > data/app

接下来,将你创建的目录和文件添加到 Git 仓库:

git add .

接下来,使用以下命令提交更改:

git commit -a -m "Add files and directories"

你将看到以下输出:

[master (root-commit) 0299d83] Add files and directories
 1 file changed, 1 insertion(+)
 create mode 100644 data/app

接下来,使用以下命令将你的文件和目录上传到 HTTP Git 服务器:

git push origin master

系统将要求你提供密码以访问 Git 服务器:

Password for 'https://
[email protected]
': 

连接成功后,你将看到以下输出:

Counting objects: 4, done.
Writing objects: 100% (4/4), 281 bytes | 281.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://git.example.com/hitesh.git
 * [new branch]      master -> master

你也可以使用以下命令直接从 Git 服务器下载你的仓库:

git clone https://
[email protected]
/hitesh.git

你将看到以下输出:

Cloning into 'hitesh'...
Password for 'https://
[email protected]
': 
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.

结论

恭喜!你已成功在 Ubuntu 22.04 上使用 Nginx 搭建了 HTTP Git 服务器。现在你可以使用命令行,在本地开发环境中使用 Git HTTP 服务器来管理和跟踪你的项目。