git 서버 구축

▩STRING THAT NEEDS TO BE CHANGED:
  /mnt/scm/git 10.10.10.111 scm sample_repo

■git 원리 및 매커니즘
  https://git-scm.com/book/ko/v2/

###########################################################
#
# SSH 프로토콜로 운영 방법 (추천하지 않음)
#
###########################################################
1.사용자 생성
  GIT_REPO_DIR="/mnt/scm/git"
  useradd -m -d ${GIT_REPO_DIR} -s /usr/bin/git-shell -g scm -c "Git User" scm

2.SSH 인증키 생성
  GIT_REPO_DIR="/mnt/scm/git"
  mkdir ${GIT_REPO_DIR}/.ssh
  ssh-keygen -b 2048 -t rsa -C "git1@email.com" -f ${GIT_REPO_DIR}/.ssh/id_rsa -q -P ""
  cat ${GIT_REPO_DIR}/.ssh/id_rsa.pub >> ${GIT_REPO_DIR}/.ssh/authorized_keys
  chown -R scm:scm ${GIT_REPO_DIR}

3.SSH 개인키 PC에 설치 (Windows)
  MKDIR "%USERPROFILE%\.ssh"
  COPY /Y id_rsa "%USERPROFILE%\.ssh\"
  ECHO Host 10.10.10.111 >"%USERPROFILE%\.ssh\config"
  ECHO HostName 10.10.10.111 >>"%USERPROFILE%\.ssh\config"
  ECHO User git  >>"%USERPROFILE%\.ssh\config"
  ECHO KexAlgorithms +diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1 >>"%USERPROFILE%\.ssh\config"
  ECHO PubkeyAcceptedAlgorithms +ssh-rsa >>"%USERPROFILE%\.ssh\config"
  ECHO HostkeyAlgorithms +ssh-rsa >>"%USERPROFILE%\.ssh\config"

4.SSH 접속 테스트 (Windows)
  ssh scm@10.10.10.111
  ※오류 시 서버의 디렉토리 퍼미션 점검

5.git 테스트 저장소 생성
  GIT_REPO_DIR="/mnt/scm/git"
  git init --bare --shared ${GIT_REPO_DIR}/sample_repo.git
  chown -R scm:scm ${GIT_REPO_DIR}/sample_repo.git

6.git 접속 테스트 (Windows)
  git config --global user.email "git1@email.com"
  git config --global user.name "홍길동"
  git config --global http.sslVerify false
  git config --global core.autocrlf false
  git config --global init.defaultBranch master
  git config --global core.protectNTFS false
  git init
  git config pull.rebase false
  git remote rm origin
  git remote add origin "ssh://scm@10.10.10.111/mnt/scm/git/sample_repo.git"

  git fetch origin
  git pull origin master --allow-unrelated-histories
  git add .
  git commit -m "commit:test"
  git push origin master

###########################################################
#
# HTTPS 프로토콜로 운영 방법
# git+gitweb 서버 구축
#
###########################################################
1.git 설치
  apt -y install git
  git --version

2.gitweb 설치
  GIT_REPO_DIR="/mnt/scm/git"
  apt -y install gitweb apache2 libapache2-mod-fcgid libcgi-session-perl
  vi /etc/gitweb.conf
    $projectroot 수정 = ${GIT_REPO_DIR}
  vi /usr/share/gitweb/gitweb.cgi
    $projectroot 수정 = ${GIT_REPO_DIR}
  chown -R scm:scm /usr/share/gitweb
  a2enmod cgid mime alias
  a2dismod mpm_event
  a2enmod mpm_prefork
  a2enmod cgi

3.apache2 사용자 생성
  htpasswd -b -c "/etc/apache2/git.passwd" git git
  chown -R scm:scm ${GIT_REPO_DIR}
  chown scm:scm "/etc/apache2/git.passwd"

4.git 레파지토리 생성
  GIT_REPO_DIR="/mnt/scm/git"
  mkdir -p ${GIT_REPO_DIR}
  git init --bare --shared ${GIT_REPO_DIR}/sample_repo.git
  설명: git init의 --bare 옵션은 이 저장소에 working 디렉토리를 생성하지 않겠다는 의미.
       (일반적인 git init은 .git 이라는 폴더가 생성되어 그 안에 repository가 형성되는데, 이 옵션을 이용하면 바로 그 폴더 자체에 repository를 형성한다)
       이는 곧, 이 저장소에서는 직접적인 작업을 하지 않겠다는 의미.
       --shared 옵션은 여러 사람들이 이 서버에 접속하여 작업할때 그 권한을 자동으로 부여해 주기 위한 옵션

5. apache2 설정 추가
  vi /etc/apache2/sites-available/git-https.conf
------------------------------------------------------------
<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    ServerAdmin admin@localhost
    DocumentRoot "/usr/share/gitweb"
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory "/usr/share/gitweb">
      Options Indexes FollowSymLinks MultiViews ExecCGI
      AddHandler cgi-script .cgi
      DirectoryIndex gitweb.cgi
      AllowOverride FileInfo AuthConfig Limit
      Order Deny,Allow
      Allow from all
      Require all granted
    </Directory>

    SetEnv GIT_PROJECT_ROOT "/mnt/scm/git"
    SetEnv GIT_HTTP_EXPORT_ALL
    SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER

    # git과 gitweb 모두 같은 포트로 사용하기 위한 셋팅
    ScriptAliasMatch "(?x)^/(.*/(HEAD | \
      info/refs | \
      objects/(info/[^/]+ | \
              [0-9a-f]{2}/[0-9a-f]{38} | \
              pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
      git-(upload|receive)-pack))$" /usr/lib/git-core/git-http-backend/$1

    AliasMatch ^/git/(.*/objects/[0-9a-f]{2}/[0-9a-f]{38})$ /usr/share/gitweb/$1
    AliasMatch ^/git/(.*/objects/pack/pack-[0-9a-f]{40}.(pack|idx))$ /usr/share/gitweb/$1
    ScriptAlias ​​/git/ /usr/lib/git-core/git-http-backend

    <Location />
      AllowOverride All
      Options All
      AuthType Basic
      AuthName "Restricted Content"
      AuthUserFile "/etc/apache2/git.passwd"
      Require valid-user
    </Location>

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/certs/ssl-cert-scm.crt
    SSLCertificateKeyFile /etc/apache2/ssl/private/ssl-cert-scm.key
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
      SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
      SSLOptions +StdEnvVars
    </Directory>
  </VirtualHost>
</IfModule>
------------------------------------------------------------

6. apache2 모듈 셋팅 및 재시작
  a2dissite scm-http.conf
  a2dissite scm-https.conf
  a2ensite scm-webdav.conf
  a2ensite git-https.conf
  systemctl enable apache2
  systemctl restart apache2
  >> https://10.10.10.111 으로 확인

7.git 접속 테스트 (Windows)
  git config --global core.autocrlf false
  git config --global core.protectNTFS false
  git config --global http.sslVerify false
  git config --global init.defaultBranch master
  git config --global user.email "git1@email.com"
  git config --global user.name "홍길동"
  git init
  git config pull.rebase false
  git remote rm origin
  git remote add origin "https://git:scm@10.10.10.111/sample_repo.git"

  git fetch origin
  git pull origin master --allow-unrelated-histories
  git add .
  git commit -m "commit:test"
  git push origin master
위로 스크롤