웹사이트 검색

Ubuntu 16.04에서 Nginx로 HTTP Git 서버를 설치하는 방법


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • 우분투 18.04(Bionic Beaver)
  • 우분투 16.04(Xenial Xerus)

이 페이지에서

  1. 1 시작하기\n
  2. 2 필수 패키지 설치\n
  3. 3 Nginx 구성
  4. 4 Git 저장소 만들기
  5. 5 클라이언트 시스템에서 Git 테스트
  6. 결론

Git은 코드 변경 사항을 추적하는 데 사용할 수 있는 무료 오픈 소스 버전 제어 시스템입니다. Git을 사용하면 동일한 애플리케이션에 대한 많은 리포지토리를 만들고 여러 사람이 해당 파일에 대한 작업을 조정할 수 있습니다. 주로 소프트웨어 개발에서 소스 코드 관리에 사용됩니다.

이 기사에서는 Ubuntu 16.04에서 Nginx로 HTTP Git 서버를 설치하는 방법을 배웁니다.

요구 사항

  • 시스템에 최신 Ubuntu 16.04 서버가 설치되어 있습니다.
  • 루트 권한이 있는 Sudo 사용자.\n
  • 정적 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 웹 서버 구성을 진행할 수 있습니다.

3 Nginx 구성

먼저 Git 트래픽을 Git으로 전달하도록 Nginx를 구성해야 합니다. 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 .

다음으로 UFW 방화벽을 통해 http 서비스를 허용해야 합니다. 기본적으로 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 서버를 사용하여 소스 코드를 쉽게 푸시, 풀, 복제 및 커밋할 수 있기를 바랍니다. 의심의 여지가 있으면 언제든지 저에게 의견을 말하십시오.