우분투(Ubuntu) 16.04(32bit) Gitlab install with apache2

집에서 개인 서버를 운영하는데 낡은 노트북으로 서버를 사용하다 보니 Gitlab에서 제공하는 64 bit용 설치파일을 사용할 수가 없었습니다.. 결국 Step-by-Step으로 Gitlab을 설치해보기로 합니다.
원본 가이드는 gitlab 공식 페이지에서 제공하는 다음 사이트를 참고하시기 바랍니다.

링크 : Gitlab 설치 가이드

제가 설치할 환경은 apache2를 웹서버로 활용하여 메인 페이지는 wordpress로 운영하고 subdirectory(예를 들어 vlee.kr/git)를 Gitlab에 연결하는 것 입니다.

Overview

Gitlab 설치를 위해서는 아래 의존 컴포넌트들이 필요합니다.

  1. Packages / Dependencies
  2. Ruby
  3. Go
  4. System Users
  5. Database
  6. Redis
  7. GitLab
  8. Nginx

 

Packages / Dependencies

패키지를 설치 하기 전에 apt-get update를 수행합니다.

# run as root!
sudo apt-get update -y 
sudo apt-get upgrade -y 
sudo apt-get install sudo -y

 

Note: 인스톨을 진행하면서 에디터를 쓸 일이 있으므로 vim이 익숙한 사람은 아래 명령을 수행합니다.

# Install vim and set as default editor 
sudo apt-get install -y vim sudo update-alternatives --set editor /usr/bin/vim.basic

 

다음의 필요한 패치키지들을 설치합니다. (아래 의존 패키지들은 Ruby를 빌드하는데 사용됨)

sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake nodejs

Kerberos를 이용한 사용자 인증을 할 경우 아래 패키지를 설치한다.

sudo apt-get install libkrb5-dev

Note: If you don’t know what Kerberos is, you can assume you don’t need it.

 

Git 설치

Gitlab 8.16 stable 버전과 호환되는 git 버전은 2.8.4 보다 나중 버전이라고 합니다. 아래 명령으로 현재 설치된 git을 지우고 보다 최근 버전으로 설치합니다.

# Install Git
sudo apt-get install -y git-core

# Make sure Git is version 2.8.4 or higher
git --version

만일 위와 같이 apt-get으로 인스톨하는 패키지가 2.8.4 보다 낮다면, 직접 소스를 다운로드 하여 빌드하면 됩니다.

# Remove packaged Git
sudo apt-get remove git-core

# Install dependencies
sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev build-essential

# Download and compile from source
cd /tmp
curl --remote-name --progress https://www.kernel.org/pub/software/scm/git/git-2.8.4.tar.gz
echo '626e319f8a24fc0866167ea5f6bf3e2f38f69d6cb2e59e150f13709ca3ebf301  git-2.8.4.tar.gz' | shasum -a256 -c - && tar -xzf git-2.8.4.tar.gz
cd git-2.8.4/
./configure
make prefix=/usr/local all

# Install into /usr/local/bin
sudo make prefix=/usr/local install

# When editing config/gitlab.yml (Step 5), change the git -> bin_path to /usr/local/bin/git

 

Ruby 설치

글을 작성하는 시점에서의 ruby 호환성은 2.1.x, 2.3.x 버전인데 2.3.x를 더 권장한다고 합니다. 2.1.x는 조만간 호환성 유지에서 제외 한다고 하네요.
먼저 이미 설치된 ruby가 있다면 다음 명령을 사용하여 제거하여 줍니다.

sudo apt-get remove ruby1.8

그리고 ruby를 다운로드 하여 컴파일 합니다.

mkdir /tmp/ruby && cd /tmp/ruby
curl --remote-name --progress https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.gz
echo '1014ee699071aa2ddd501907d18cbe15399c997d ruby-2.3.3.tar.gz' | shasum -c - && tar xzf ruby-2.3.3.tar.gz
cd ruby-2.3.3
./configure --disable-install-rdoc
make
sudo make install

다음으로 bundler를 설치합니다.

sudo gem install bundler --no-ri --no-rdoc

 

Go 설치

Git HTTP request를 처리하는데 gitlab-workhorse라는 것이 필요한데 이걸 빌드 하기 위해서는 Go Compiler가 필요하다고 합니다. Go를 설치해줍시다. 참고로 말씀드리면, 저는 처음에 Go를 글 작성할 시점의 최신 버전을 설치하려고 했거든요. 근데 호환성 문제가 있는 것 같아. 1.5.3을 설치하였습니다.
64bit 시스템인경우 go1.5.3.linux-amd64를 설치하면 되지만 저는 32bit이기 때문에 386으로 설치하였습니다.

# Remove former Go installation folder
sudo rm -rf /usr/local/go

curl --remote-name --progress https://storage.googleapis.com/golang/go1.5.3.linux-386.tar.gz
echo '43afe0c5017e502630b1aea4d44b8a7f059bf60d7f29dfd58db454d4e4e0ae53  go1.5.3.linux-386.tar.gz' | shasum -a256 -c - && \
  sudo tar -C /usr/local -xzf go1.5.3.linux-386.tar.gz
sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
rm go1.5.3.linux-386.tar.gz

 

System User 등록

사용자 등록을 해줍니다.

sudo adduser --disabled-login --gecos 'GitLab' git

 

Database 설치

Gitlab의 기본 database로는 PostgreSQL(9.1 version 이상)을 권장합니다. 저는 이미 MySQL을 사용중이지만 혹시 문제가 생길까봐 그냥 PostgreSQL을 사용하였습니다.
(MySQL을 사용해도 충분히 문제 없을 겁니다. 예전에 설치할 땐 MySQL로 하였었습니다.)

# Install the database packages:
sudo apt-get install -y postgresql postgresql-client libpq-dev postgresql-contrib

# Create a database user for GitLab:
sudo -u postgres psql -d template1 -c "CREATE USER git CREATEDB;"

# Create the pg_trgm extension (required for GitLab 8.6+):
sudo -u postgres psql -d template1 -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;"

# Create the GitLab production database and grant all privileges on database:
sudo -u postgres psql -d template1 -c "CREATE DATABASE gitlabhq_production OWNER git;"

# Try connecting to the new database with the new user:
sudo -u git -H psql -d gitlabhq_production

위와 같이 실행하면 DB에 접속 될 것 입니다.

pg_trgm이 활성화 되었는지 확인하기 위해 아래 SQL을 수행합니다.

SELECT true AS enabled
FROM pg_available_extensions
WHERE name = 'pg_trgm'
AND installed_version IS NOT NULL;

위와 같이 수행했을 때 아래와 같은 결과를 확인하면 정상적으로 설치가 완료된 것 입니다.

enabled
---------
 t
(1 row)

DB에서 나오려면 아래와 같이 ‘\q’ 명령을 수행합니다.

gitlabhq_production> \q

 

Redis 설치

Gitlab 8.16은 2.8이상의 redis를 요구합니다.

sudo apt-get install redis-server

Redis 설치 후 몇가지 설정을 합니다.

# Configure redis to use sockets
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig

# Disable Redis listening on TCP by setting 'port' to 0
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf

# Enable Redis socket for default Debian / Ubuntu path
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf

# Grant permission to the socket to all members of the redis group
echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf

# Create the directory which contains the socket
mkdir /var/run/redis
chown redis:redis /var/run/redis
chmod 755 /var/run/redis

# Persist the directory which contains the socket, if applicable
if [ -d /etc/tmpfiles.d ]; then
  echo 'd  /var/run/redis  0755  redis  redis  10d  -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi

# Activate the changes to redis.conf
sudo service redis-server restart

# Add git to the redis group
sudo usermod -aG redis git

저는 위와 같은 설정을 안 했더니 Gitlab 페이지를 열었을 때 css style이 안 보이고 페이지도 매우 느리게 떴었습니다. 따라서, 꼭 올바른 설정을 해주시기 바랍니다.

 

Gitlab 설치

Gitlab을 설치하기 위해 git 디렉토리로 이동합니다.

# We'll install GitLab into home directory of the user "git"
cd /home/git

Gitlab 소스코드를 clone 합니다.

# Clone GitLab repository
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-16-stable gitlab

다음으로 설정 작업입니다.

# Go to GitLab installation folder
cd /home/git/gitlab

# Copy the example GitLab config
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# Update GitLab config file, follow the directions at top of file
sudo -u git -H editor config/gitlab.yml

gitlab.yml 파일을 열면 아래와 같은 내용들을 확인할 수 있다.

## GitLab settings
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
host: vlee.kr
relative_url_root: /git

subdirectory로 git을 사용할 것이므로 relative_url_root도 설정하여 준다.

# Copy the example secrets file
sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml

# Make sure GitLab can write to the log/ and tmp/ directories
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/

# Make sure GitLab can write to the tmp/pids/ and tmp/sockets/ directories
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

# Create the public/uploads/ directory
sudo -u git -H mkdir public/uploads/

# Make sure only the GitLab user has access to the public/uploads/ directory
# now that files in public/uploads are served by gitlab-workhorse
sudo chmod 0700 public/uploads

# Change the permissions of the directory where CI build traces are stored
sudo chmod -R u+rwX builds/

# Change the permissions of the directory where CI artifacts are stored
sudo chmod -R u+rwX shared/artifacts/

# Copy the example Unicorn config
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

# Find number of cores
nproc

# Enable cluster mode if you expect to have a high load instance
# Set the number of workers to at least the number of cores
# Ex. change amount of workers to 3 for 2GB RAM server
sudo -u git -H editor config/unicorn.rb

nproc 명령을 실행하면 시스템 코어 갯수를 표시해준다. unicorn.rb에 worker_processes를 코어 갯수에 맞춰 준다. 그리고 마찬가지로 relative url을 설정한다.

# Relative URL support
# WARNING: We recommend using an FQDN to host GitLab in a root path instead
# of using a relative URL.
# Documentation: http://doc.gitlab.com/ce/install/relative_url.html
# Uncomment and customize the following line to run in a non-root path
#
ENV['RAILS_RELATIVE_URL_ROOT'] = "/git"

# Read about unicorn workers here:
# http://doc.gitlab.com/ee/install/requirements.html#unicorn-workers
#
worker_processes 2

나머지 설정 파일들을 복사하여준다.

# Copy the example Rack attack config
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

# Configure Git global settings for git user
# 'autocrlf' is needed for the web editor
sudo -u git -H git config --global core.autocrlf input

# Disable 'git gc --auto' because GitLab already runs 'git gc' when needed
sudo -u git -H git config --global gc.auto 0

# Enable packfile bitmaps
sudo -u git -H git config --global repack.writeBitmaps true

# Configure Redis connection settings
sudo -u git -H cp config/resque.yml.example config/resque.yml

# Change the Redis socket path if you are not using the default Debian / Ubuntu configuration
sudo -u git -H editor config/resque.yml

resque.yml 파일은 redis path가 안 바뀌었다면 수정할 내용은 없다.

 

Gitlab DB 설정

# PostgreSQL only:
sudo -u git cp config/database.yml.postgresql config/database.yml

# MySQL and remote PostgreSQL only:
# Update username/password in config/database.yml.
# You only need to adapt the production settings (first part).
# If you followed the database guide then please do as follows:
# Change 'secure password' with the value you have given to $password
# You can keep the double quotes around the password
sudo -u git -H editor config/database.yml

# PostgreSQL and MySQL:
# Make config/database.yml readable to git only
sudo -u git -H chmod o-rwx config/database.yml

 

Gems 설치

# For PostgreSQL (note, the option says "without ... mysql")
sudo -u git -H bundle install --deployment --without development test mysql aws kerberos

만약 kerberos를 사용하면 without에서 kerberos를 제외한다.

 

Gitlab shell 설치

# Run the installation task for gitlab-shell (replace `REDIS_URL` if needed):
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production SKIP_STORAGE_VALIDATION=true

# By default, the gitlab-shell config is generated from your main GitLab config.
# You can review (and modify) the gitlab-shell config as follows:
sudo -u git -H editor /home/git/gitlab-shell/config.yml

 

gitlab-workhorse 설치

sudo -u git -H bundle exec rake "gitlab:workhorse:install[/home/git/gitlab-workhorse]" RAILS_ENV=production

 

Database 초기화

sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail

초기화 명령 수행시 root의 password를 미리 설정 할 수 있다.

 

Init Script 설치 및 설정 파일 복사

sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
sudo update-rc.d gitlab defaults 21

Setup Logrotate

sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

 

Application Status 검사

sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production

Compile Assets

sudo -u git -H bundle exec rake gitlab:assets:compile RAILS_ENV=production

 

Gitlab 시작

sudo service gitlab start
# or
sudo /etc/init.d/gitlab restart

 

Relative URL 설정

subdirectory 사용을 위해 Relative URL을 좀 더 설정해줍니다. 앞서 일부 설정하였으니 나머지 부분만 설정하면 됩니다.

sudo service gitlab stop

실행했던 gitlab을 중지합니다. 그리고 아래 명령으로 relative_url.rb 파일을 복사합니다.

cp /home/git/gitlab/config/initializers/relative_url.rb.sample \
   /home/git/gitlab/config/initializers/relative_url.rb

복사 후 위 파일을 수정합니다.

Rails.application.configure do
  config.relative_url_root = "/git"
end

다음으로 gitlab-shell의  config.yml을 수정합니다.

vi /home/git/gitlab-shell/config.yml

아래와 같이 subdirectory를 설정합니다.

gitlab_url: http://localhost/git/

마지막으로 /etc/default/gitlab을 수정합니다. gitlab_workhorse_options을 다음과 같이 설정합니다.

gitlab_workhorse_options="-listenUmask 0 -listenNetwork unix -listenAddr $socket_path/gitlab-   
workhorse.socket -authBackend http://127.0.0.1:8080/git -authSocket $socket_path/gitlab.socket -documentRoot $app_root/public"

gitlab 재시작

sudo /etc/init.d/gitlab restart

 

위 Relative URL설정은 아래 링크에서 원본을 확인할 수 있다.

링크 : Gitlab Relative URL 설정

 

Apache2 설정

apache2에 config 파일을 생성하여준다.

vi /etc/apache2/sites-available/git.conf

위 파일에 아래와 같이 설정을 입력하여준다.

<Location /git>
    Require all granted

    ProxyPass http://127.0.0.1:8080/git/
    ProxyPassReverse http://127.0.0.1:8080/git/
</Location>

RewriteEngine on
  #Forward all requests to gitlab-workhorse except existing files like error documents
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads/.*
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
  # needed for downloading attachments
  DocumentRoot /home/git/gitlab/public
  #Set up apache error documents, if back end goes down (i.e. 503 error) then a maintenance/deploy
page is thrown up.

 ErrorDocument 404 /404.html
  ErrorDocument 422 /422.html
  ErrorDocument 500 /500.html
  ErrorDocument 502 /502.html
  ErrorDocument 503 /503.html

그리고 apache2 restart

sudo /etc/init.d/apache2 restart

 

사이트에 접속하면 아래와 같은 화면을 볼 수 있다. 초기 페이지에는 Sign-up 메뉴도 있는데 제가 사용하는 페이지에는 Sign-up 메뉴는 비활성화 했습니다. admin 계정 로그인은 ID : root와 DB 초기화에서 설정하였던 password를 입력하면 됩니다.

 

마치며

개인 프로젝트를 하려는 생각으로 Gitlab을 며칠째 세팅하였으나. 정작 Gitlab이 잘 돌아가니 아무 프로젝트고 안 하고 있는 현실입니다. ^^;
Gitlab 공식 홈페이지에서 제공하는 기본 설치 과정들을 거쳐서 설치했었는데 왜이리 오래 걸렸는지 모르겠네요. Gitlab이 기본으로 nginx를 사용하고 있다보니 연동하는데 문제도 있었던거 같고 Redis 설정을 잘 못 해서 발생한 문제도 있었습니다. 혹시 저와 같은 문제를 겪는 분들은 처음부터 차근차근 빠진 것이 없는지 확인해보시길 바랍니다. 무엇보다 64bit 시스템을 사용하면 패키지 인스톨로 할 수 있으니.. 이런 상세 설정은 필요 없을지도 모르겠네요.

Leave a Reply