GNU/Linux >> Linux の 問題 >  >> Debian

Debian11にNginxを使用してHTTPGitサーバーをインストールする方法

Gitは、世界中の何千人もの開発者が使用しているオープンソースのバージョン管理システムです。これは、ソースレベルでソフトウェアの変更を追跡するために使用されます。変更を追跡し、前の段階に戻し、ファイルとディレクトリの代替バージョンを作成できます。

HTTP Gitサーバーは、Nginxウェブサーバーを使用してローカルエリアネットワーク(LAN)経由でGitリポジトリにサービスを提供するオープンソースプロジェクトです。非常にシンプルでセットアップも簡単です。誰でもコマンドラインインターフェイスから管理できます。

このチュートリアルでは、Debian11でNginxを使用してHTTPGitリポジトリサーバーをセットアップする方法を説明します。

前提条件
  • Debian11を実行しているサーバー。
  • サーバーIPで指定された有効なドメイン名。
  • ルートパスワードはサーバーで構成されています。

Nginxとその他の依存関係をインストールする

まず、HTTP Gitサーバーをセットアップするために、NginxWebサーバーとその他の必要なパッケージをインストールする必要があります。次のコマンドを使用して、それらすべてをインストールできます。

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

すべてのパッケージがインストールされたら、次のステップに進むことができます。

Gitリポジトリを作成する

次に、Gitリポジトリを保存するためのディレクトリを作成する必要があります。 myrepoというディレクトリを作成しましょう Nginx Webルートディレクトリ内:

mkdir /var/www/html/myrepo

次に、ディレクトリを myrepoに変更します ユーザー用に別のディレクトリを作成します:

cd /var/www/html/myrepo
mkdir user1.git

次に、ユーザーディレクトリに移動し、次のコマンドを使用してGitリポジトリを初期化します。

cd user1.git
git --bare init

次の出力が得られます:

Initialized empty Git repository in /var/www/html/myrepo/user1.git/

次に、次のコマンドを使用してGitサーバー情報を更新します。

git update-server-info

次に、myrepoの所有権を変更し、次のコマンドで適切な権限を設定します。

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

次に、user1というユーザーを作成し、パスワードを設定します。

htpasswd -c /var/www/html/myrepo/htpasswd user1

パスワードは以下のように設定できます:

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

次のコマンドを使用してパスワードを確認できます。

cat /var/www/html/myrepo/htpasswd

サンプル出力:

user1:$apr1$LoyCEkzA$Fjq5nBbLhBRdaxCQBBUQd1

Gitリポジトリを提供するようにNginxを構成する

次に、Gitリポジトリにサービスを提供するためにNginx仮想ホスト構成ファイルを作成する必要があります。

nano /etc/nginx/conf.d/git.conf

次の行を追加します:

server {
        listen 80;

        root /var/www/html/myrepo;

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

        server_name git.yourdomain.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/myrepo/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/myrepo;
    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 2021-12-11 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

Dec 11 08:00:04 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 11 08:00:04 debian11 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Dec 11 08:00:04 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.
クライアントからGitリポジトリに接続する

この時点で、GitHTTPサーバーはNginxでセットアップされています。次に、クライアントマシンから接続してテストします。

まず、次のコマンドを使用して、クライアントマシンに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 "user1"

次に、次のコマンドを使用してGitHTTPサーバーを追加します。

git remote add origin http://[email protected]/user1.git

次に、dev01というディレクトリを作成し、その中にファイルを追加します。

mkdir dev01
echo "This is my first application" > dev01/file1

次に、作成したディレクトリとファイルを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 dev01/file1

次に、次のコマンドを使用して、ファイルとディレクトリをHTTPGitサーバーにアップロードします。

git push origin master

Gitサーバーにアクセスするためのパスワードの入力を求められます:

Password for 'http://[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 http://git.yourdomain.com/user1.git
 * [new branch]      master -> master

次のコマンドを使用して、Gitサーバーからリポジトリを直接ダウンロードすることもできます。

git clone http://[email protected]domain.com/user1.git

次の出力が得られます:

Cloning into 'user1'...
Password for 'http://[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.
結論

上記のガイドでは、Debian11でNginxを使用してHTTPGitサーバーをセットアップする方法を学習しました。これで、このセットアップをローカル開発環境に実装し、コマンドラインを使用してプロジェクトを管理および追跡できます。


Debian
  1. Ubuntu16.04にNginxを使用してHTTPGitサーバーをインストールする方法

  2. Debian8VPSにNginxを使用してFuelPHPをインストールする方法

  3. Debian11にNginxを使用してWonderCMSをインストールする方法

  1. Ubuntu20.04にNginxを使用してHTTPGitサーバーをインストールする方法

  2. Nginxを使用してDebianにGhostをインストールする方法

  3. Nginxを使用してDebianWheezyにDokuWikiをインストールする方法

  1. Debian9にGitをインストールする方法

  2. Debian9にNginxを使用してWonderCMSをインストールする方法

  3. Ubuntu18.04LTSにNginxを使用してHTTPGitサーバーをインストールする方法