WeAir

源码安装GitLab

GitLab是由GitLab Inc.开发,使用MIT许可证的基于网络的Git仓库管理工具,且具有wiki和issue跟踪功能。

安装要求

系统为Ubuntu 20,内存至少4G,建议增加Swap交换分区解决内存不足等问题。

1.安装软件包及依赖

安装vim编辑器并设置为默认编辑器

1
2
sudo apt-get install -y vim
sudo update-alternatives --set editor /usr/bin/vim.basic

安装依赖

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

从GitLab13.6起,需要安装Gitaly提供的Git。

1
2
3
4
sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev libpcre2-dev build-essential git-core
git clone https://gitlab.com/gitlab-org/gitaly.git -b <X-Y-stable> /tmp/gitaly
cd /tmp/gitaly
sudo make git GIT_PREFIX=/usr/local

为了使自定义图标能够正常工作,需要安装GraphicsMagick。

1
sudo apt-get install -y graphicsmagick

为了接收邮件通知,需要安装postfix。

1
sudo apt-get install -y postfix

2.Ruby

运行GitLab需要使用Ruby,需要安装Ruby。

1
2
3
4
5
6
7
8
9

# 下载Ruby源码并编译安装
mkdir /tmp/ruby && cd /tmp/ruby
curl --remote-name --progress-bar "https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.4.tar.gz"
echo '3043099089608859fc8cce7f9fdccaa1f53a462457e3838ec3b25a7d609fbc5b ruby-2.7.4.tar.gz' | sha256sum -c - && tar xzf ruby-2.7.4.tar.gz
cd ruby-2.7.4
./configure --disable-install-rdoc --enable-shared
make
sudo make install

3.Go

GitLab有几个用Go编写的守护程序。要安装GitLab,需要一个Go编译器。

1
2
3
4
5
6
7
# 如果有旧的Go编译器的话,需要先移除掉旧版本
sudo rm -rf /usr/local/go

curl --remote-name --progress-bar "https://golang.org/dl/go1.16.9.linux-amd64.tar.gz"
echo 'd2c095c95f63c2a3ef961000e0ecb9d81d5c68b6ece176e2a8a2db82dc02931c' go1.16.9.linux-amd64.tar.gz' | shasum -a256 -c - && sudo tar -C /usr/local -xzf go1.15.12.linux-amd64.tar.gz
sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
rm go1.16.9.linux-amd64.tar.gz

4.Node

GitLab需要使用Node来编译JavaScript,并使用Yarn来管理JavaScript依赖。

1
2
3
4
5
# 安装node v14.x
curl --location "https://deb.nodesource.com/setup_14.x" | sudo bash -
sudo apt-get install -y nodejs

npm install --global yarn

5.添加GitLab用户

为GitLab创建一个git用户。

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

6.数据库

创建PostgreSQL数据库

1
sudo apt-get install -y postgresql postgresql-client libpq-dev postgresql-contrib

启动PostgreSQL数据库并查看状态

1
2
sudo service postgresql start
sudo service postgresql status

为GitLab创建数据库用户

1
sudo -u postgres psql -d template1 -c "CREATE USER git CREATEDB;"

创建pg_trgm扩展

1
sudo -u postgres psql -d template1 -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;"

创建btree_gist扩展

1
sudo -u postgres psql -d template1 -c "CREATE EXTENSION IF NOT EXISTS btree_gist;"

创建数据库并赋予用户权限

1
sudo -u postgres psql -d template1 -c "CREATE DATABASE gitlabhq_production OWNER git;"

尝试使用git用户连接数据库

1
sudo -u git -H psql -d gitlabhq_production

检查扩展pg_trgm是否生效

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

如果扩展生效,则显示以下内容

1
2
3
4
enabled
---------
t
(1 row)

检查扩展btree_gist是否生效

1
2
3
4
SELECT true AS enabled
FROM pg_available_extensions
WHERE name = 'btress_gist'
AND installed_version IS NOT NULL;

如果扩展生效,则显示以下内容

1
2
3
4
enabled
---------
t
(1 row)

退出数据库

1
gitlabhq_production> \q

修改Postgresql的配置文件允许机器访问

1
2
3
4
5
6
7
8
9
sudo vim /etc/postgresql/12/main/pg_hba.conf

# 修改文件末尾处
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust

重启PostgreSQL服务。

7.Redis

GitLab需要安装Redis。

1
sudo apt-get install redis-server

配置Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig

sudo sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf

echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf

echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf

sudo mkdir -p /var/run/redis
sudo chown redis:redis /var/run/redis
sudo chmod 755 /var/run/redis

if [ -d /etc/tmpfiles.d ]; then
echo 'd /var/run/redis 0755 redis redis 10d -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi

sudo service redis-server restart

sudo usermod -aG redis git

8.GitLab

1
2
# 在git的home目录安装GitLab
cd /home/git

下载GitLab源码

1
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-foss.git -b 14-3-stable gitlab

配置GitLab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
cd /home/git/gitlab

sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# 编辑gitlab.yml修改域名、是否使用HTTPS以及git路径,修改以下配置
sudo -u git -H editor config/gitlab.yml

gitlab:
host: domain.name
port: 443 # Set to 443 if using HTTPS, see installation.md#using-https for additional HTTPS configuration details
https: true # Set to true if using HTTPS, see installation.md#using-https for additional HTTPS configuration details
...
git:
bin_path: /usr/local/bin/git

sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml

sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/

sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

sudo -u git -H mkdir -p public/uploads/

sudo chmod 0700 public/uploads

sudo chmod -R u+rwX builds/

sudo chmod -R u+rwX shared/artifacts/

sudo chmod -R ug+rwX shared/pages/

sudo -u git -H cp config/puma.rb.example config/puma.rb

# 设置Git全局配置
sudo -u git -H git config --global core.autocrlf input

sudo -u git -H git config --global gc.auto 0

sudo -u git -H git config --global repack.writeBitmaps true

sudo -u git -H git config --global receive.advertisePushOptions true

sudo -u git -H git config --global core.fsyncObjectFiles true

sudo -u git -H cp config/resque.yml.example config/resque.yml

配置GitLab数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo -u git cp config/database.yml.postgresql config/database.yml

# 修改数据库配置,把secure password改成你自己的密码。
sudo -u git -H editor config/database.yml

#
# PRODUCTION
#
production:
adapter: postgresql
encoding: unicode
database: gitlabhq_production
username: git
password: "yourpassword"
host: localhost

sudo -u git -H chmod o-rwx config/database.yml

安装Gems,确保bundle的版本大于等于1.5.2小于2.X。

1
2
3
sudo -u git -H bundle config set --local deployment 'true'
sudo -u git -H bundle config set --local without 'development test mysql aws kerberos'
sudo -u git -H bundle install

安装GitLab Shell

1
sudo -u git -H bundle exec rake gitlab:shell:install RAILS_ENV=production

安装GitLab Workhorse

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

安装GitLab Pages

1
2
3
4
5
cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-pages.git
cd gitlab-pages
sudo -u git -H git checkout v$(</home/git/gitlab/GITLAB_PAGES_VERSION)
sudo -u git -H make

安装Gitaly

1
2
3
4
5
cd /home/git/gitlab
sudo -u git -H bundle exec rake "gitlab:gitaly:install[/home/git/gitaly,/home/git/repositories]" RAILS_ENV=production

sudo chmod 0700 /home/git/gitlab/tmp/sockets/private
sudo chown git /home/git/gitlab/tmp/sockets/private

运行Gitaly

1
2
3
4
gitlab_path=/home/git/gitlab
gitaly_path=/home/git/gitaly

sudo -u git -H sh -c "$gitlab_path/bin/daemon_with_pidfile $gitlab_path/tmp/pids/gitaly.pid $gitaly_path/gitaly $gitaly_path/config.toml >> $gitlab_path/log/gitaly.log 2>&1 &"

初始化数据库并激活高级功能

1
2
3
4
cd /home/git/gitlab

# 设置GitLab密码为你自己的密码
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail

安装启动脚本

1
2
3
4
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab

# 设置开机自启
sudo sysv-rc-conf gitlab on

设置Logrotate

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

检查GitLab及其环境是否配置正确

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

编译GetText PO

1
sudo -u git -H bundle exec rake gettext:compile RAILS_ENV=production

编译静态文件

1
2
sudo -u git -H yarn install --production --pure-lockfile
sudo -u git -H bundle exec rake gitlab:assets:compile RAILS_ENV=production NODE_ENV=production NODE_OPTIONS="--max_old_space_size=4096"

启动GitLab

1
2
3
sudo service gitlab start
# 或
sudo /etc/init.d/gitlab restart

9.NGINX

NGINX是GitLab官方支持的Web服务器。安装NGINX并将GitLab的配置文件复制到NGINX的配置目录里,编辑相应文件。

1
2
3
4
5
# HTTP
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab

# HTTPS
sudo cp lib/support/nginx/gitlab-ssl /etc/nginx/sites-available/gitlab-ssl

测试并重启NGINX。

安装完成

检查GitLab状态

1
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

如果所有项目都是绿色,安装成功。

后记

打开ip域名访问GitLab服务,进行后续配置。

GitLab

其它问题可查询官方文档