在自己的服务器上部署 GitLab 社区版

2024-06-19 12:18
文章标签 部署 服务器 社区 gitlab

本文主要是介绍在自己的服务器上部署 GitLab 社区版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://www.cnblogs.com/restran/p/4063880.html

http://www.cnblogs.com/restran/p/4063880.html

http://www.cnblogs.com/restran/p/4063880.html

http://www.cnblogs.com/restran/p/4063880.html


在自己的服务器上部署 GitLab 社区版

GitLab 简介

提供 Git 项目仓库托管服务的有业界闻名的 GitHub,但你要将代码上传到 GitHub上面,而且将项目设为私有还要收费。而 GitLab 则是开源免费的(社区版免费,企业版需要订阅),同样是采用了 Ruby on Rails 开发,可以让你在自己的内网搭建一个“山寨版”的 GitHub。GitHub 的使用体验是诱人的,因此部署自己的 GitLab 就十分吸引人。

Gitlab的Github地址
https://github.com/gitlabhq/gitlabhq

安装部署

以下内容为自己阅读官方文档并试验后的学习笔记加部分翻译

GitLab 提供了两种方式来安装,一种是使用官方打包好的文件,这种方法比较简单,也不容易出错;另一种是手动安装,就是把代码下载下来,然后安装各种环境,数据库,编译环境等等,一步一步安装起来,这种就比较麻烦,但是可以安装各种分支版本,比如中文翻译版。

先介绍下自己的环境,是在虚拟机中安装的:
Ununtu 14.04,GitLab 7.4.2 Community Edition

一、使用官方安装包安装

官方安装文档
https://about.gitlab.com/downloads/

1. 下载gitlab安装包

https://downloads-packages.s3.amazonaws.com/ubuntu-14.04/gitlab_7.4.2-omnibus-1_amd64.deb
官方文档使用wget,但是这个文件有280MB,还是用迅雷先下载好,速度比较快。

2. 安装 openssh-server
sudo apt-get install openssh-server
3. 安装邮件服务器
sudo apt-get install postfix


这里选择Internet Site


然后让设置FQDN,但是使用默认的机器名即可,这里不是很确定。

4. 安装gitlab安装包
sudo dpkg -i gitlab_7.4.2-omnibus-1_amd64.deb

安装完之后,会建立一个git用户和一个gitlab-www用户,在GitLab上建立的代码仓库都是放在/home/git/下面。但是奇怪的是,该目录竟然对当前登录的管理员用户不可见,需要用git用户登录才可以,而且git用户还不能登录到图形窗口,只能登录到命令行。由于git用户建立时使用了--disabled-login,需要先设置密码才能登录,可以用passwd git,修改git用户的密码。

5. 配置 gitlab

这一步在官方的文档里面没有,但是如果没有配置的话,直接启动GitLab,会出现不正确的FQDN错误,导致无法正常启动。因此必须做配置。
这边的配置使用这里分享的经验。

sudo mkdir -p /etc/gitlab
sudo touch /etc/gitlab/gitlab.rb
sudo chmod 600 /etc/gitlab/gitlab.rb
sudo gedit /etc/gitlab/gitlab.rb


把'${external_url}'改成部署机器的域名或者IP地址

这个地址很重要,上传的图片什么的,url会以这个为基准,如果地址写错,将无法访问到图片等这些资源。

6. 然后对GitLab进行重配置即可

这一步也是启动 GitLab

sudo gitlab-ctl reconfigure
7. 打开浏览器登陆
Username: root
Password: 5iveL!fe

第一次启动的时候,需要初始化大量的东西,经常会出现502错误,通常是由于内存不足的原因导致,所以需要准备好足够的内存。官方推荐生产环境中使用2G内存,2核CPU。虚拟机中测试1GB内存基本就可以了。

二、手动安装

官方安装文档
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md

安装前最好先确认下能否访问amazon aws上面的资源,因为有很多安装包需要从这里获取,如果不行就先配置下代理服务器或者VPN,解决这个问题。否则后面的安装会很痛苦。

安装过程包含以下几个内容

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

需要先更新下系统,不然有些依赖的包会找不到

sudo apt-get update -y 
sudo apt-get upgrade -y
sudo apt-get install sudo -y

下面这一步为可选,如果熟悉用vim就安装vim为默认的编辑器,或者选择其它

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 redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake

安装 Git

sudo apt-get install -y git-core

需要确保 Git 的版本在 1.7.10 以上,如 1.7.12 或 2.0.0

git --version

如果当前系统中的 Git 版本太低,可以先删除后安装,如果版本没问题就跳过这一步。

删除 Git

sudo apt-get remove git-core 

安装依赖

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

下载源码并编译安装 Git

cd /tmp
curl -L --progress https://www.kernel.org/pub/software/scm/git/git-2.1.2.tar.gz | tar xz
cd git-2.1.2/
make prefix=/usr/local all

安装到 /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

安装邮件服务器,用于发送 GitLab 的通知邮件

sudo apt-get install -y postfix
Then select 'Internet Site' and press enter to confirm the hostname.

安装邮件服务器,选择 Internet Site,并在 FQDN 处,设置为主机名(缺省就是主机名)

2. Ruby

如果已经安装了 Ruby 1.8 版本,先删除

sudo apt-get remove ruby1.8

下载并编译安装 Ruby

mkdir /tmp/ruby && cd /tmp/ruby
curl -L --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz
cd ruby-2.1.2
./configure --disable-install-rdoc
make
sudo make install

安装 Bundler Gem:

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

如果步骤1中的依赖部分,有些没有安装成功的话,这里就会出现错误,比如
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass

3. System Users

在系统中为 GitLab 创建一个 git 用户

Create a git user for GitLab:
sudo adduser --disabled-login --gecos 'GitLab' git

这里创建的系统用户,显示的用户名是GitLab,但是在home目录下已经有/home/git了

4. Database

官方推荐使用 PostgreSQL 数据库

安装数据库包

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

登录到 PostgreSQL

sudo -u postgres psql -d template1

为 GitLab创建一个用户

template1=# CREATE USER git CREATEDB;

创建 GitLab 生成环境数据库,并给数据库授予所有权限

template1=# CREATE DATABASE gitlabhq_production OWNER git;

退出数据库会话

template1=# \q

试一下用新建的用户连接到新建的数据库,然后退出会话

sudo -u git -H psql -d gitlabhq_production
template1=# \q
5. Redis

安装 resis 服务器

sudo apt-get install redis-server

配置 redis 以使用 sockets

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig

将 redis 的监听端口设置为 0 来关闭 TCP 监听

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

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

Persist the directory which contains the socket, if applicable

if [ -d /etc/tmpfiles.d ]; thenecho '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
6. GitLab

GitLab 将被安装到/homt/git目录下

cd /home/git

通过克隆 GitLab 代码仓库的方式下载 GitLab

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

这里可以替换为其他版本,例如中文翻译版

sudo -u git -H git clone https://gitlab.com/larryli/gitlab.git -b 7-4-zh gitlab



配置 GitLab

切换到 GitLab 安装目录

cd /home/git/gitlab

复制一份 GitLab 配置文件的例子

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

更新配置文件

sudo -u git -H editor config/gitlab.yml

需要配置两项

host: example.com # Gitlab settings
email_from: example@example.com # Email settings 

host 这个字段的值要改成服务器的ip或域名,不要包含 http,这个字段很重要,会影响到后面建立项目的地址,以及上传的图片的地址,如把 host 配置成192.168.137.135,那么项目地址就会使这样
http://192.168.137.135/username/test_project.git

email_from 配置的项就是 Gitlab 发邮件时的发件人,这里一定要写成一个合法的 email 地址,可以任意。
当新建用户,或者有评论时,就会收到这里设置的邮箱发来的邮件。如果这里填的邮箱格式有错,将导致收不到邮件。

为 GitLab 配置写 log/ 和 tmp/ 目录的权限

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

Create directory for satellites

sudo -u git -H mkdir /home/git/gitlab-satellites
sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites

为 GitLab 配置写 tmp/pids/ 和 tmp/sockets/ 目录的权限

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

为 GitLab 配置写 public/uploads/ 目录的权限

sudo chmod -R u+rwX  public/uploads

复制一份 Unicorn 配置文件的例子

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

查看当前机器有多少核CPU

nproc

Enable cluster mode if you expect to have a high load instance
Ex. change amount of workers to 3 for 2GB RAM server
Set the number of workers to at least the number of cores

sudo -u git -H editor config/unicorn.rb

复制一份 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

根据 gitlab.yml 中配置的信息来配置 user.email

sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "example@example.com"
sudo -u git -H git config --global core.autocrlf input

配置 redis 连接设置

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

Important Note: Make sure to edit both gitlab.yml and unicorn.rb to match your setup.

Note: If you want to use HTTPS, see Using HTTPS for the additional steps.

配置 GitLab 数据设置

PostgreSQL only:

sudo -u git cp config/database.yml.postgresql config/database.yml

MySQL only:

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

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

Install Gems

Note: As of bundler 1.5.2, you can invoke bundle install -jN (where N the number of your processor cores) and enjoy the parallel gems installation with measurable difference in completion time (~60% faster). Check the number of your cores with nproc. For more information check this post. First make sure you have bundler >= 1.5.2 (run bundle -v) as it addresses some issues that were fixed in 1.5.2.

For PostgreSQL (note, the option says "without ... mysql")
如果使用 PostgreSQL,请执行这条命令

sudo -u git -H bundle install --deployment --without development test mysql aws

这里如果出现安装错误,请检查前面的依赖是否安装正确

Or if you use MySQL (note, the option says "without ... postgres")
如果使用 MySQL,请执行这条命令

sudo -u git -H bundle install --deployment --without development test postgres aws



安装 GitLab Shell

GitLab Shell is an SSH access and repository management software developed specially for GitLab.

Run the installation task for gitlab-shell (replace REDIS_URL if needed):

sudo -u git -H bundle exec rake gitlab:shell:install[v2.0.1] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production

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

如果要使用HTTPS,则要去查看官方文档的HTTPS的配置步骤。



Initialize Database and Activate Advanced Features

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:'
Note: You can set the Administrator password by supplying it in environmental variable GITLAB_ROOT_PASSWORD, eg.:

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

Install Init Script

Download the init script (will be /etc/init.d/gitlab):

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

And if you are installing with a non-default folder or user copy and edit the defaults file:

sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab

If you installed GitLab in another directory or as a user other than the default you should change these settings in /etc/default/gitlab. Do not edit/etc/init.d/gitlab as it will be changed on upgrade.

Make GitLab start on boot:

sudo update-rc.d gitlab defaults 21

Setup Logrotate

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

检查 GitLab 的环境是否配置正确

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

如果内存不够,会导致无法分配内存,出错。给虚拟机增加内存,重启后,再次执行出现错误could not locate gemfile
原因是执行当前命令所在的目录没有gemfile,切换到目录Gitlab安装目录
cd /home/git/gitlab
再次执行就可以了

Compile Assets

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

Start Your GitLab Instance

sudo service gitlab start

或者

sudo /etc/init.d/gitlab restart
7. Nginx

Note: Nginx is the officially supported web server for GitLab. If you cannot or do not want to use Nginx as your web server, have a look at the GitLab recipes.

Installation

sudo apt-get install -y nginx

Site Configuration

Copy the example site config:

sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

Make sure to edit the config file to match your setup:

Change YOUR_SERVER_FQDN to the fully-qualified
domain name of your host serving GitLab.

配置 Nginx

sudo editor /etc/nginx/sites-available/gitlab

需要配置两项,servre中的listen和server_name,server_name跟gitlab.yml
中的host字段配置成一样。
关键一点是去掉listen *:80 后面的default_server
否则执行sudo nginx -t时会出现nginx emerg a duplicate default server错误

如果是用gedit编辑的话,会在/etc/nginx/sites-available/保存一个default
需要删除这个文件,否则会导致nginx出现
nginx error “conflicting server name” ignored
可以在var/log/niginx/error 里面看到

症状:
访问localhost,只出现nginx的欢迎页,没有出现gitlab的登录页面,
在后面执行
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
会发现gitlab-shell出错

Note: If you want to use HTTPS, replace the gitlab Nginx config with gitlab-ssl. See Using HTTPS for HTTPS configuration details.

Test Configuration

Validate your gitlab or gitlab-ssl Nginx config file with the following command:

sudo nginx -t

You should receive syntax is okay and test is successful messages. If you receive errors check your gitlab or gitlab-ssl Nginx config file for typos, etc. as indicated in the error message given.

Restart

sudo service nginx restart

结束,所有的都配置完了。执行下面的命令来验证是否配置成功。

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

如果所有的项目都是绿色的,表示 GitLab 已经成功安装了!

NOTE: Supply SANITIZE=true environment variable to gitlab:check to omit project names from the output of the check command.

打开浏览器登陆吧!
用户名:root
密码:5iveL!fe

其他问题:

如果安装的是 Larry Li 翻译的 7-2-zh,在新建项目的时候,如果名称填写的不符合要求,会提示
Namecan contain only letters, digits, '', '-' and '.' and space. It must start with letter, digit or ''.

第一个单词Name和can之间少了一个空格,安装官方推荐方式安装7.4.2官方版,没有这个问题。
找到文件
/home/git/gitlab/lib/gitlab/regex.rb

找到 project_regex_message,在前面加上空格,其他类似的也可以一并修改

def project_regex_message#"can contain only letters, digits, '_', '-' and '.' and space. " \" can contain only letters, digits, '_', '-' and '.' and space. " \"It must start with letter, digit or '_'."
end


这篇关于在自己的服务器上部署 GitLab 社区版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075008

相关文章

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Windows Server 2025 搭建NPS-Radius服务器的步骤

《WindowsServer2025搭建NPS-Radius服务器的步骤》本文主要介绍了通过微软的NPS角色实现一个Radius服务器,身份验证和证书使用微软ADCS、ADDS,具有一定的参考价... 目录简介示意图什么是 802.1X?核心作用802.1X的组成角色工作流程简述802.1X常见应用802.

GitLab文件的上传与下载方式

《GitLab文件的上传与下载方式》:本文主要介绍GitLab文件的上传与下载方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录GitLab 项目拉取到本地GitLab 项目上传方法方法 1:本地项目未初始化Git方法 2:本地项目已初始化GitGitLab 上