[Ubuntu] 기존 Apache 서버에 GitLab 설치하기

Software · Posted on January 22, 2017 at 02:15 AM

GitLab을 처음 설치하면 번들로 내장된 Nginx 웹서버에서 돌아가도록 기본 설정되어 있다. 웹 서버로 GitLab만 구동하지 않고, 다른 웹사이트와 함께 돌리는게 일반적인데, 이를 해결할 수 있는 가장 간단한 방법은 80번이 아닌 다른 포트로 할당하는 것이다. 하지만, 접속할 때 뒤에 포트번호를 매번 입력해야 하는 불편함이 있다. 이를 해결하기 위한 방법이 reverse proxy를 설정하는 것인데, 본 글에서는 이를 사용하여 기존에 Apache 서버를 사용하면서 GitLab을 같은 포트에 설치하는 방법에 대해 다룬다.

Step 1: GitLab 설치

GitLab을 설치하기 위해, 아래 명령어를 입력한다.

sudo apt-get install curl openssh-server ca-certificates postfix
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo apt-get install gitlab-ce
sudo gitlab-ctl reconfigure

만약 GitLab만 웹사이트로 운영할 생각이면, 더 이상 추가 설정을 할 필요가 없다. 해당 웹서버의 ip 주소를 웹 브라우저에 입력하면, GitLab 페이지가 뜨는 것을 확인할 수 있다. 하지만, 만일 Apache 등의 웹서버를 기존에 돌리고 있었던 상황이라면, 아래 명령어를 입력하여 Apache 서버를 재시작 후 다시 확인해 보자. 기존 Apache에서 운영하던 웹페이지가 없어지고, GitLab만이 뜨는 것을 확인할 수 있다.

sudo service apache2 restart

바로 다음 절에서 이 부분에 대한 해결책을 소개할 것이다.

Step 2: GitLab 설정 변경

Ubuntu에서 기본적으로 GitLab의 설정파일은 “/etc/gitlab/gitlab.rb”에 위치되어 있다. Type the following command to edit the configuration file:

sudo nano /etc/gitlab/gitlab.rb

설정파일에서 아래의 항목을 변경.

# Change GitLab port (any unused port is ok.)
external_url 'http://127.0.0.1:14500'

설정 저장 후, 아래 명령어를 입력하여 GitLab 설정 변경 적용

sudo gitlab-ctl reconfigure

Step 3: Apache에서 GitLab reverse proxy 설정

본론으로 들어가기 전에, GitLab 주소로 사용할 도메인을 하나 갖고 있어야 한다. 만일 없다면, 최소한 hosts 레코드 혹은 공유기 설정에서 DNS 설정을 사용하여 임시 도메인을 하나 만들어서 사용하면 된다. 이 부분에 대한 설명은 생략하고, 본 절에서는 “http://gitlabtest.yoursite.com/” 을 GitLab 주소 예시로 설명한다.

Apache에서 proxy_http 모듈을 사용하도록 설정한다. 설정 방법은 아래와 같다.

sudo a2enmod proxy_http

Apache의 site 설정에 GitLab을 다음과 같이 추가한다.

cd /etc/apache2/sites-available/
sudo touch gitlab.conf
sudo nano gitlab.conf

nano 에디터에 다음과 같이 입력한다.

<VirtualHost *:80>
  ServerName gitlab.yoursite.com

  ProxyRequests off
  ProxyPass / http://127.0.0.1:14500/
  ProxyPassReverse / http://127.0.0.1:14500/
<VirtualHost>

저장 후, site 활성화를 위해 아래와 같이 입력한다.

sudo a2ensite gitlab
sudo service apache2 restart

Step 4: GitLab 페이지에서 잘못된 repository 표출 문제 수정

Reverse proxy를 사용하여 설정하였기에, GitLab 페이지에서는 내부 Nginx에서의 URL (예: http://127.0.0.1:14500/repo_url) 을 표출하는데, 이는 실제 클라이언트에서는 접근이 불가능한 주소인 경우가 많다. 이는 아래와 같이 설정 파일을 수정하여 해결할 수 있다.

sudo nano /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

초기 설정 (변경 전):

## Web server settings (note: host is the FQDN, do not include http://)
host: 127.0.0.1
port: 14500
https: false

변경 후:

## Web server settings (note: host is the FQDN, do not include http://)
host: gitlabtest.yoursite.com
port: 80
https: false

설정 후, GitLab을 다음 명령어를 사용하여 재실행한다.

sudo gitlab-ctl restart

이후 GitLab 페이지에 접속하면, 실제 URL을 표출하는 것을 확인할 수 있다. 추후 수정시에, gitlab-ctl reconfigure 를 실행하여 GitLab을 재설정하면 위의 URL 설정값이 초기화된다. 그런 경우에는, 위와 같이 다시 수정하여 gitlab-ctl restart 명령어로 재실행하면 된다.

Note 1: https 설정 방법

GitLab을 https로 설정하는 방법은 위의 방법에서 추가적으로 수정할 부분이 있다. https 설정시에 필요한 인증서, 키, 루트 인증서등은 이미 준비되어 있다고 가정하고, 이후 추가 수정하는 방법을 기술한다.

GitLab 설정 변경 설정 파일 /etc/gitlab/gitlab.rb 에서 다음 부분 수정:

# GitLab 포트 변경 (원하는 port 로 변경하면 됨.)
external_url 'https://127.0.0.1:14500'

...
################
# GitLab Nginx #
################
...
nginx['ssl_certificate'] = "/path/to/certificate.crt"
nginx['ssl_certificate_key'] = "/path/to/certificate_key.key"

이후, 아래 명령어로 GitLab 설정을 재구성한다.

sudo gitlab-ctl reconfigure

추가적으로, 설정파일 /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml 에서 다음 부분을 수정한다.

## Web server settings (note: host is the FQDN, do not include http://)
host: gitlabtest.yoursite.com
port: 443
https: true

마지막으로, 아래 명령어로 GitLab을 재시작한다.

sudo gitlab-ctl restart

Apache 설정 변경

Apache에서 reverse proxy를 구성하기 위해, 설정 파일 /etc/apache2/sites-available/gitlab.conf 를 생성 후 아래 설정값을 입력한다.

<VirtualHost *:80>
  ServerName gitlabtest.yoursite.com
  Redirect permanent / https://gitlabtest.yoursite.com/
</VirtualHost> 

<VirtualHost *:443>
  ServerName gitlabtest.yoursite.com

  # Certificate
  SSLEngine on
  SSLCertificateFile /path/to/certificate.crt
  SSLCertificateKeyFile /path/to/certificate_key.key
  SSLCertificateChainFile /path/to/certificate_ca.crt

  # Proxy
  SSLProxyEngine on
  SSLProxyVerify none
  SSLProxyCheckPeerCN off
  SSLProxyCheckPeerName off
  SSLProxyCheckPeerExpire off

  ProxyPass / https://127.0.0.1:14500/
  ProxyPassReverse / https://127.0.0.1:14500/
</VirtualHost>

이제 Apache 서버를 아래 명령어로 재실행한다.

sudo service apache2 restart

Note 2: IP를 제한하고 싶다면…

접속 IP를 제한하고 싶다면, Apache 설정파일 gitlab.conf에 아래 항목을 추가 입력한다.

<Proxy *>
  Order Deny,Allow
  Deny from all
  Allow from your_ip_address
<Proxy>

결론

이 글은 Apache 서버에 reverse proxy를 사용하여 GitLab의 번들 서버인 Nginx를 Apache를 통해 접속하도록 설정하는 방법을 설명하였습니다. 위 방법은 많은 방법들 중 하나이고, 더 좋은 방법이 있다면 댓글로 말씀 부탁드립니다.

References

[1] forum.gitlab.com, “[SOLVED] Setting up Gitlab on Ubuntu 14.04 with Apache2 without owning a domain name,” GitLab Forum, 27-Apr-2015. [Online]. Available: https://forum.gitlab.com/t/solved-setting-up-gitlab-on-ubuntu-14-04-with-apache2-without-owning-a-domain-name/679/2
[2] superuser.com, “js, and css not appearing after installing gitlab,” Superuser, 19-Nov-2015. [Online]. Available: http://superuser.com/questions/1002827/js-and-css-not-appearing-after-installing-gitlab
[3] Goedecke, “Setup GitLab on Debian 7 with existing Apache WebServer,” GitLab Forum, 17-Sep-2015. [Online]. Available: https://kevingoedecke.me/2015/09/17/setup-gitlab-on-debian-7-with-existing-apache-webserver/
[4] stackoverflow.com, “Gitlab in a subdirectory with apache and passenger,” kevingoedecke.me, 29-Jul-2013. [Online]. Available: http://stackoverflow.com/questions/17924644/gitlab-in-a-subdirectory-with-apache-and-passenger
[5] Tully, “Running GitLab from a subdirectory on Apache,” shanetully.com, 23-Aug-2012. [Online]. Available: https://shanetully.com/2012/08/running-gitlab-from-a-subdirectory-on-apache/
[6] redmine.org, “Redmine in a subdirectory,” redmine.org, 03-Sep-2008. [Online]. Available: http://www.redmine.org/boards/2/topics/2244

Posted by chlee

0 Comments

Post a comment

Security Code