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

Ubuntu/Debian

Gitは、LinusTorvaldsによって開発された無料のオープンソースバージョン管理システムです。これは、世界中の何百万もの開発者によって使用されています。 GitHubは、無料のコードホスティングサービスも提供しています。ただし、無料サービスでは、コードのプライベートホスティングは許可されていません。この場合、GitHTTPサーバーを使用して独自のコードホスティングサーバーをホストできます。これにより、サーバーを完全に制御できるようになります。

このチュートリアルでは、Ubuntu20.04でNginxを使用してGitHTTPサーバーをインストールおよび構成する方法を示します。

前提条件

  • Ubuntu20.04を実行しているサーバー。
  • サーバーIPで指定された有効なドメイン名。
  • ルートパスワードがサーバーに設定されます。

入門

開始する前に、サーバーパッケージを最新バージョンに更新することをお勧めします。次のコマンドで更新できます。

apt-get update -y

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

NginxとGitをインストールします

次に、Nginx Webサーバー、Git、およびその他の必要なパッケージをシステムにインストールする必要があります。次のコマンドでインストールできます。

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

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

Gitリポジトリを作成する

次に、NginxWebルートディレクトリ内にGitリポジトリを作成する必要があります。まず、次のコマンドを使用してgitという名前のディレクトリを作成します。

mkdir /var/www/html/git

次に、ディレクトリをgitに変更し、Gitリポジトリ用の新しいディレクトリを作成します。

cd /var/www/html/git
mkdir gituser.git

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

git --bare init

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

git update-server-info

次に、次のコマンドを使用してgitディレクトリの所有権と権限を設定します。

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

次に、次のコマンドを使用して、認証用の新しいgituserを作成します。

htpasswd -c /var/www/html/git/htpasswd gituser

以下に示すように、パスワードを設定するように求められます。

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

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

cat /var/www/html/git/htpasswd

次の出力が得られるはずです。

gituser:$apr1$iPKZDbFB$ziRRbGXzVMMHaPYOtL05m/

Git用にNginxを構成する

次に、Gitリポジトリにサービスを提供するようにNginxを構成する必要があります。次のコマンドを使用して、Gitの新しい仮想ホスト構成ファイルを作成できます。

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

次の行を追加します。

server {
        listen 80;

        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 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/git/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/git;
    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 Tue 2020-11-17 07:43:46 UTC; 4s ago
       Docs: man:nginx(8)
    Process: 3240 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 3256 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 3257 (nginx)
      Tasks: 3 (limit: 4691)
     Memory: 3.5M
     CGroup: /system.slice/nginx.service
             ??3257 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??3258 nginx: worker process
             ??3259 nginx: worker process

Nov 17 07:43:46 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 17 07:43:46 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.

終了したら、次のステップに進むことができます。

HTTPGitサーバーをテストする

この時点で、Gitサーバーがインストールおよび構成されます。さて、それをテストする時が来ました。

クライアントマシンで、次のコマンドを使用してGitパッケージをインストールします。 広告

apt-get install git -y

インストールしたら、次のコマンドを使用してmyappという名前のディレクトリを作成します。

mkdir myapp

次に、新しいディレクトリに移動し、次のコマンドでGitを初期化します。

cd myapp
git init

次に、次のコマンドを使用してリモートGitリポジトリを追加します。

git remote add origin http://gituser@git.example.com/gituser.git

次に、app1およびapp2ディレクトリを作成し、それらのディレクトリ内にいくつかのコンテンツを含むapp1およびapp2ファイルも作成します。

mkdir app1 app2
echo "This is my first application" > app1/app1
echo "This is my first application" > app2/app2

次に、次のコマンドを使用して、すべてのディレクトリとファイルをリポジトリに追加します。

git add .

次に、次のコマンドを使用して変更をコミットします。

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

次の出力が得られるはずです。

[master (root-commit) 4e90372] Add files and directories
 2 files changed, 2 insertions(+)
 create mode 100644 app1/app1
 create mode 100644 app2/app2

次に、次のコマンドを使用して、これらの変更をリモートGitサーバーにプッシュします。

git push origin master

以下に示すように、リモートGitユーザーのパスワードを入力するように求められます。

Password for 'http://gituser@git.example.com':

パスワードを入力してEnterキーを押します。次の出力が得られるはずです。 広告

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 354 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To http://gituser@git.example.com/gituser.git
 * [new branch]      master -> master

上記の出力は、ファイルとディレクトリがリモートGitリポジトリに追加されていることを示しています。このリポジトリをローカルシステムに複製する場合は、次のコマンドを実行します。

git clone http://gituser@git.example.com/gituser.git

次の出力が得られるはずです。

Cloning into 'gituser'...
Password for 'http://gituser@git.example.com': 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.

結論

おめでとう!これで、Ubuntu20.04サーバーにGitHTTPサーバーが正常にインストールおよびセットアップされました。これで、LAN内からアクセスできる開発環境にGitサーバーを実装できます。

Source

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です