如何在 Ubuntu 16.04 上使用 Nginx 安装 HTTP Git 服务器如何在 Ubuntu 16.04 上使用 Nginx 安装 HTTP Git 服务器如何在 Ubuntu 16.04 上使用 Nginx 安装 HTTP Git 服务器如何在 Ubuntu 16.04 上使用 Nginx 安装 HTTP Git 服务器
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Ubuntu 16.04 上使用 Nginx 安装 HTTP Git 服务器

本教程适用于这些操作系统版本

  • Ubuntu 18.04(仿生海狸)
  • Ubuntu 16.04(Xenial Xerus)

在此页

  1. 1 入门
  2. 2 安装所需的包
  3. 3 配置 Nginx
  4. 4 创建 Git 存储库
  5. 5 在客户端机器上测试 Git
  6. 结论

Git 是一个免费的开源版本控制系统,可用于跟踪代码的更改。 Git 允许您为同一个应用程序创建多个存储库,并协调多人对这些文件的工作。它主要用于软件开发中的源代码管理。

在本文中,我们将学习如何在 Ubuntu 16.04 上安装带有 Nginx 的 HTTP Git 服务器。

要求

  • 在您的系统上安装了全新的 Ubuntu 16.04 服务器。
  • 具有 root 权限的 Sudo 用户。
  • 在您的服务器上配置静态 IP 地址 192.168.15.189

1 入门

在开始之前,您需要使用最新的稳定版本更新您的系统。

您可以通过运行以下命令来执行此操作:

sudo apt-get update -y
sudo apt-get upgrade -y

更新系统后,重新启动系统并使用 sudo 用户登录。

2 安装所需的包

首先,您需要在系统中安装一些必需的软件包,包括 nginx、git、nano 和 fcgiwrap。您可以通过运行以下命令来安装所有这些:

sudo apt-get install nginx git nano fcgiwrap apache2-utils -y

安装所有必需的包后,您将需要为 Git 存储库创建一个目录。您可以通过运行以下命令来执行此操作:

sudo mkdir /var/www/html/git

接下来,给 Git 目录适当的权限:

sudo chown -R www-data:www-data /var/www/html/git

完成后,您可以继续配置 Nginx Web 服务器。

3 配置 Nginx

首先,您需要配置 Nginx 以将 Git 流量传递给 Git。你可以通过编辑 Nginx 默认配置文件来做到这一点:

sudo nano /etc/nginx/sites-available/default

更改文件如下所示:

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;


        root /var/www/html/git;

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

        server_name _;

        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; # 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/html/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/html/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
}

}

完成后保存并关闭文件。然后使用以下命令测试 Nginx 是否存在任何配置错误:

sudo nginx -t

如果一切正常,您应该会看到以下输出:

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

接下来,您需要创建一个用户帐户,您需要使用该帐户浏览或提交到存储库。您可以使用 htpasswd 实用程序创建名为 hitesh 的用户:

sudo htpasswd -c /var/www/html/git/htpasswd hitesh

最后,使用以下命令重新启动 Nginx 以应用所有更改:

sudo systemctl restart nginx

您可以使用以下命令检查 Nginx 服务器的状态:

sudo 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 Tue 2017-06-20 23:00:11 IST; 51min ago
  Process: 12415 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 7616 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
  Process: 12423 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 12419 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 12427 (nginx)
   CGroup: /system.slice/nginx.service
           ??????12427 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ??????12431 nginx: worker process                           

Jun 20 23:00:11 localhost systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jun 20 23:00:11 localhost systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 20 23:00:11 localhost systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Jun 20 23:00:11 localhost systemd[1]: Started A high performance web server and a reverse proxy server.

4 创建 Git 仓库

一旦一切配置正确,就可以创建 Git 存储库了。

您可以使用以下命令创建名称为 repo.git 的存储库:

cd /var/www/html/git
sudo mkdir hitesh.git
sudo cd hitesh.git
sudo git --bare init
sudo git update-server-info
sudo chown -R www-data.www-data .
sudo chmod -R 777 .

接下来,您需要允许 http 服务通过 UFW 防火墙。默认情况下,UFW 在您的系统上是禁用的,因此您需要先启用它。您可以使用以下命令启用它:

sudo ufw enable

启用 UFW 防火墙后,您可以通过运行以下命令来允许 HTTP 服务:

sudo ufw allow http

您现在可以通过运行以下命令来检查 UFW 防火墙的状态:

sudo ufw status

好的,这就是服务器端配置。您现在可以转到客户端来测试 Git。

5 在客户端机器上测试 Git

在开始之前,您需要在客户端系统上安装 git。您可以使用以下命令安装它:

sudo apt-get install git -y

首先,使用以下命令创建本地存储库:

sudo mkdir ~/testproject

接下来,将目录更改为 testproject 并使用以下命令启动新的远程存储库:

cd ~/testproject
git init
git remote add origin http:///hitesh.git

接下来,使用以下命令创建一些文件和目录:

mkdir test1 test2 test3
echo "This is my first repository" > test1/repo1
echo "This is my second repository" > test2/repo2
echo "This is my third repository" > test3/repo3

接下来,运行以下命令将所有文件和目录添加到存储库:

git add .
git commit -a -m "Add files and directoires"

您应该看到以下输出:

[master 002fac9] Add files and directoires
 3 files changed, 3 insertions(+)
 create mode 100644 repo1
 create mode 100644 repo2
 create mode 100644 repo3

接下来,使用以下命令将所有文件和目录推送到 Git 服务器:

git push origin master

您应该看到以下输出:

Password for 'http://': 
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 422 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To http:///hitesh.git
   68f1270..002fac9  master -> master

现在,您的所有文件和目录都已提交到您的 Git 服务器。

您的 Git 存储库创建过程现已完成。您现在可以在将来轻松克隆您的存储库。您可以在远程系统上使用以下命令克隆您的存储库:

git clone :/var/www/html/git/hitesh.git

您应该看到以下输出:

Cloning into 'hitesh'...
's password: 
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (3/3), done.
Receiving objects: 100% (8/8), 598 bytes | 0 bytes/s, done.
remote: Total 8 (delta 0), reused 0 (delta 0)
Checking connectivity... done.

现在,使用以下命令将目录更改为克隆的存储库:

cd hitesh
tree

您应该看到以下输出:

.
|-- test1
|   `-- repo1
|-- test2
|   `-- repo2
`-- test3
    `-- repo3

3 directories, 3 files

结论

我希望您现在可以使用 Git 服务器轻松地推送、拉取、克隆和提交源代码。如果您有任何疑问,请随时给我评论。

©2015-2025 艾丽卡 support@alaica.com