CentOS 7 安裝 Gitlab

pantao發表於2015-05-05

安裝基本系統與依賴包

安裝 Gitlab 依賴的工具

bashyum -y update
yum -y groupinstall `Development Tools`
yum -y install readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel openssl-devel curl-devel expat-devel db4-devel byacc sqlite-devel libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel libicu libicu-devel system-config-firewall-tui git redis ruby sudo wget crontabs logwatch logrotate perl-Time-HiRes

安裝 Redis

訪問 http://www.redis.io/download,下載 Redis 原始碼。

bashwget http://download.redis.io/releases/redis-3.0.0.tar.gz
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make

若在編譯過程中出錯,則可以執行下面的命令:

bashsudo make test

安裝:

bashsudo make install
sudo ./utils/install_server.sh

配置

建立 /etc/init.d/redis 並使用下面的程式碼作為啟動指令碼。

新增如下內容:

bash###########################
PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/etc/redis/6379.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac
##############################

儲存後,新增可執行許可權:

sudo chmod +x /etc/init.d/redis

確保 redis 能隨系統啟動:

vi /etc/rc.d/rc.local

在檔案末尾新增下面這行:

service redis start

然後使用上面同樣的命令啟動 redis 服務:

service redis start

安裝郵件伺服器

yum -y install postfix

安裝Git

先刪除系統中原有的老版本 git

bashyum -y remove git
yum install zlib-devel perl-CPAN gettext curl-devel expat-devel gettext-devel openssl-devel

從官方網站下載原始碼進行:

bashcurl --progress https://www.kernel.org/pub/software/scm/git/git-2.4.0.tar.gz | tar xz
cd git-2.4.0/
./configure
make
make prefix=/usr/local install

然後使用下面這個命令檢測安裝是否有效:

which git

安裝 ruby

如果 ruby 的版本低於 2.0 的話,則需要重新安裝 ruby

bashcd ~
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz | tar xz
cd ruby-2.2.2
./configure --disable-install-rdoc
make
make prefix=/usr/local install

為 Gitlab 新增系統使用者

adduser --system --shell /bin/bash --comment `GitLab` --create-home --home-dir /home/git/ git

為了包含/usr/local/bin到git使用者的$PATH,一個方法是編輯超級使用者檔案。以管理員身份執行:

visudo

然後搜尋:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

將其改成:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

安裝資料庫

MySQL 已經不再包含在 CentOS 7 的源中,而改用了 MariaDB,先搜尋 MariaDB 現有的包:

rpm -qa | grep mariadb

然後全部刪除:

rpm -e --nodeps mariadb-*

然後建立 /etc/yum.repos.d/MariaDB.repo

vi /etc/yum.repos.d/MariaDB.repo

將以下內容新增至該檔案中:

# MariaDB 10.0 CentOS repository list - created 2015-05-04 19:16 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

然後執行下面命令安裝 MariaDB 10.0

sudo yum install MariaDB-server MariaDB-client

然後啟動 MariaDB 服務:

service mysql start

接著執行 mysql_secure_installation

mysql_secure_installation

登入 MariaDB 並建立相應的資料庫使用者與資料庫:

mysql -uroot -p
CREATE USER `git`@`localhost` IDENTIFIED BY `$password`;
SET storage_engine=INNODB;
CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO `git`@`localhost`;
q

嘗試使用新使用者連線資料庫:

sudo -u git -H mysql -u git -p -D gitlabhq_production
q

安裝 Gitlab

克隆源

sudo -u -git cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-10-stable gitlab

配置

cd /home/git/gitlab

# Copy the example GitLab config
# 複製GitLab的示例配置檔案
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# Make sure to change "localhost" to the fully-qualified domain name of your host serving GitLab where necessary
# 確保修改“localhost”為你的GitLab主機的FQDN
#
# If you want to use https make sure that you set `https` to `true`. See #using-https for all necessary details.
# 如果你想要使用https確保你設定了`https`為`true`。具體必要的細節參見#using-https
#
# If you installed Git from source, change the git bin_path to /usr/local/bin/git
# 如果你從原始碼安裝了Git,修改git的bin_path為/usr/local/bin/git
sudo -u git -H editor config/gitlab.yml

# Make sure GitLab can write to the log/ and tmp/ directories
# 確保GitLab可以寫入log/和temp/目錄
chown -R git {log,tmp}
chmod -R u+rwX  {log,tmp}

# Create directory for satellites
# 為衛星(?)建立目錄
sudo -u git -H mkdir /home/git/gitlab-satellites
chmod u+rwx,g+rx,o-rwx /home/git/gitlab-satellites

# Make sure GitLab can write to the tmp/pids/ and tmp/sockets/ directories
# 確保GitLab可以寫入tmp/pids/和temp/sockets/目錄
chmod -R u+rwX  tmp/{pids,sockets}

# Make sure GitLab can write to the public/uploads/ directory
# 確保GitLab可以寫入public/uploads/目錄
chmod -R u+rwX  public/uploads

# Copy the example Unicorn config
# 複製Unicorn的示例配置檔案
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

# Enable cluster mode if you expect to have a high load instance
# Ex. change amount of workers to 3 for 2GB RAM server
# 啟用叢集模式如果你期望擁有一個高負載例項
# 附:修改worker的數量到3用於2GB記憶體的伺服器
sudo -u git -H editor config/unicorn.rb

# Copy the example Rack attack config
# 複製Rack attack的示例配置檔案
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

# Configure Git global settings for git user, useful when editing via web
# Edit user.email according to what is set in config/gitlab.yml
# 為git使用者配置Git全域性設定,當通過web修改時有用
# 修改user.email根據config/gitlab.yml中的設定
sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "gitlab@localhost"
sudo -u git -H git config --global core.autocrlf input

資料庫配置

# MySQL only:
# 僅限MySQL:
sudo -u git cp config/database.yml.mysql 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
# 僅限MySQL和遠端PostgreSQL:
# 在config/database.yml中更新使用者名稱/密碼;
# 你只需要適配生產設定(第一部分);
# 如果你跟從資料庫嚮導,請按以下操作:
# 修改`secure password`使用你剛才設定的$password;
# 你可以保留密碼兩端的雙引號。
sudo -u git -H editor config/database.yml

# PostgreSQL and MySQL:
# Make config/database.yml readable to git only
# PostgreSQL和MySQL:
# 設定config/database.yml僅對git可讀。
sudo -u git -H chmod o-rwx config/database.yml

安裝 Gems

cd /home/git/gitlab

# For users from China mainland only
# 僅限中國大陸使用者
nano /home/git/gitlab/Gemfile
source "http://ruby.taobao.org" // 原始 source "https://rubygems.org/"

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

Install GitLab shell

安裝GitLab Shell

GitLab Shell是一個專門為GitLab開發的SSH訪問和源管理軟體。

# Go to the Gitlab installation folder:
# 轉到GitLab安裝目錄:
cd /home/git/gitlab

# For users from China mainland only
# 僅限中國大陸使用者
nano /home/git/gitlab/Gemfile
source "http://ruby.taobao.org" // 原始 source "https://rubygems.org/"

# Run the installation task for gitlab-shell (replace `REDIS_URL` if needed):
# 執行gitlab-shell的安裝任務(替換`REDIS_URL`如果有需要的話):
sudo -u git -H bundle exec rake gitlab:shell:install[v1.9.6] REDIS_URL=redis://localhost:6379 RAILS_ENV=production

# By default, the gitlab-shell config is generated from your main gitlab config.
# 預設的,gitlab-shell的配置檔案是由你的gitlab主配置檔案生成的。
#
# Note: When using GitLab with HTTPS please change the following:
# - Provide paths to the certificates under `ca_file` and `ca_path options.
# - The `gitlab_url` option must point to the https endpoint of GitLab.
# - In case you are using self signed certificate set `self_signed_cert` to `true`.
# See #using-https for all necessary details.
# 提示:當通過HTTPS使用GitLab時,請做出如下更改:
# - 提供證照的路徑在`ca_file`和`ca_path`選項;
# - `gitlab_url`選項必須指向GitLab的https端點;
# - 如果你使用自簽名的證照,設定`self-signed_cert`為`true`。
# 所有必需的具體細節參見#using-https
#
# You can review (and modify) it as follows:
# 你可以檢查(並修改該)通過以下方法:
sudo -u git -H editor /home/git/gitlab-shell/config.yml

# Ensure the correct SELinux contexts are set
# Read http://wiki.centos.org/HowTos/Network/SecuringSSH
# 確保正確的SELinux上下文被設定
# 閱讀http://wiki.centos.org/HowTos/Network/SecuringSSH
restorecon -Rv /home/git/.ssh

初始化資料庫和啟用高階功能

sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
# Type `yes` to create the database tables.
# When done you see `Administrator account created:`

提示:你可以設定管理員密碼通過在環境變數GITLAB_ROOT_PASSWORD中提供,例如:

sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=newpassword

安裝初始化指令碼

下載初始化指令碼(將放在/etc/init.d/gitlab):

sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
chmod +x /etc/init.d/gitlab
chkconfig --add gitlab

設定GitLab開機啟動:

chkconfig gitlab on

設定日誌翻轉

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

檢查應用狀態

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

編譯靜態檔案

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

啟動例項

/etc/init.d/gitlab start

相關文章