本帖针对Centos6/REHL6系统
Gitlab的安装过程主要包括以下组件的配置:
关闭selinux
# 修改/etc/selinux/config 文件 将SELINUX=enforcing改为SELINUX=disabled ,然后重启电脑 # sestatus -v 查看selinux状态 Current mode: permissive #说明已关闭selinux
GitLab软件包
所有GitLab软件包都会发布到我们的软件包服务器上,并且可以下载。我们维持五个回购:
GitLab EE:适用于官方企业版版本
GitLab CE:用于官方Community Edition版本
Unstable:适用于发布候选版和其他不稳定版本
Nighty Builds:每晚制作
安装软件包及版本要求
Ubuntu/Debian/CentOS/RHEL**
ruby 2.0+
git 1.7.10+
redis 2.0+
MySQL or PostgreSQ
1.安装软件包及解决依赖项
添加EPEL源:
wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-6 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 # 安装`epel-release-latest-6.noarch.rpm`包,启用EPEL rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm yum groupinstall "Development tools" yum install autoconf automake bison build-essential byacc checkinstall cmake cpio crontabs curl curl-devel db4-devel expat-devel gcc-c++ gdbm-devel gettext gettext-devel glibc-devel libcurl4-openssl-dev libexpat1-dev libffi libffi-dev libffi-devel libgdbm-dev libicu libicu-dev libicu-devel libkrb5-dev libncurses5-dev libreadline-dev libssl-dev libtool libxml2 libxml2-dev libxml2-devel libxslt libxslt-dev libxslt-devel libyaml libyaml-dev libyaml-devel libz-dev logrotate logwatch make ncurses-devel openssh-server openssl-devel patch pcre-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-Time-HiRes pkg-config postfix python-devel python-docutils readline readline-devel sqlite-devel sudo system-config-firewall-tui tcl-devel vim wget zlib1g-dev zlib-devel
安装git
如果已经用yum安装过git,要先卸载掉旧的版本
yum remove git
使用源码编译安装git
mkdir /tmp/git && cd /tmp/git curl -O --progress https://www.kernel.org/pub/software/scm/git/git-2.8.5.tar.gz tar zxvf git-2.8.5.tar.gz cd git-2.8.5 ./configure make prefix=/usr/local all # 安装到/usr/local/bin sudo make prefix=/usr/local install # 验证git版本号 git --version #创建软连接 ln -s /usr/local/bin/git /usr/bin/git
2.添加系统用户
我们添加一个用来管理运行Gitlab的用户git
adduser --system --shell /bin/bash --comment 'GitLab' --create-home --home-dir /home/git/ git # 修改git用户的环境变量PATH,以root用户运行 visudo # 找到下面一行 Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin #修改为 Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
3.安装ruby环境
在Gitlab生产环境使用Ruby版本管理工具RVM,rbenv或者chruby常常会带来很多疑难杂症.比如Gitlab-shell版本管理器调用OpenSSH的功能以防止越过ssh对仓库进行pull和push操作.而前面提到的三个版本管理器不支持这样的功能,所以我们强烈建议大家按照下面的方式来安装Ruby.
Note: The current supported Ruby (MRI) version is 2.3.x. GitLab 9.0 dropped
support for Ruby 2.1.x.
如果系统上存在旧的Ruby1.8,先删除掉:
yum remove ruby
下载Ruby源码,编译安装:
mkdir /tmp/ruby && cd /tmp/ruby # 这里替换官方文档的下载地址为mirrors.ustc.edu.cn提供的镜像地址 curl -O --progress https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz tar zxvf ruby-2.1.5.tar.gz cd ruby-2.1.5 ./configure --disable-install-rdoc make sudo make install 安装完成后,重新登录终端确保$PATH生效,检测ruby的安装成功与否: ruby -v ln -s /usr/local/bin/ruby /usr/bin/ruby
国内使用Ruby的Gem和Bundler必须要做的事情:
# 修改git用户gem安装源为淘宝 gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/ # 确保git用户当前gems源为淘宝 gem sources -l *** CURRENT SOURCES *** 备:gems源中科大: https://gems.ruby-china.org/
安装bundle包(root用户)
sudo gem install bundler --no-ri --no-rdoc
4-1.安装数据库-mysql
安装mysql数据库,设置数据库管理员密码
#下载yum仓库文件: wget http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm #下载完成后将Yum库导入到你的本地: sudo yum localinstall mysql-community-release-el6-*.noarch.rpm #安装MySQLServer: yum install mysql-server mysql-devel mysql-client libmysqlclient-dev #启动mysql服务 /etc/init.d/mysqld start #MySQL安全配置向导 mysql_secure_installation --------------------------------------------------------------------------------- Enter current password for root (enter for none): <–初次运行直接回车 Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车 Remove anonymous users? [Y/n] <– 是否删除匿名用户,生产环境建议删除,所以直接回车 Disallow root login remotely? [Y/n] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止 Remove test database and access to it? [Y/n] <– 是否删除test数据库,直接回车 Reload privilege tables now? [Y/n] <– 是否重新加载权限表,直接回车
创建新用户和数据库给gitlab使用
# 登录数据库 $ mysql -u root -p # 输入root密码 # 为gitlab创建使用用户 mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab账号的密码'; # 创建gitlaba使用的数据库 mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`; # 给予gitlab用户权限 mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'git'@'localhost' IDENTIFIED BY 'gitpasswd'; # 刷新权限 mysql> flush privileges; # 查看创建的用户 mysql> select user,host,password from mysql.user;
#测试新建的用户能否登陆mysql mysql -u git -p -h localhost mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | gitlabhq_production | +---------------------+ 2 rows in set (0.00 sec)
4-2.安装数据库-PostgreSQL
配置postgresql安装源:
https://wiki.postgresql.org/wiki/YUM_Installation#Configure_your_YUM_repository
# 修改/etc/yum.repos.d/CentOS-Base.repo,在[base]和[update]段落添加下面的配置 exclude=postgresql* # 安装postgresql源 yum localinstall http://mirrors.ustc.edu.cn/postgresql/repos/yum/9.5/redhat/rhel-6-x86_64/pgdg-centos95-9.5-3.noarch.rpm #yum localinstall http://mirrors.ustc.edu.cn/postgresql/repos/yum/9.2/redhat/rhel-6-x86_64/pgdg-redhat92-9.2-8.noarch.rpm # 安装postgresql yum install postgresql95-server postgresql95-devel postgresql95-contrib # 默认情况下,postgresql的数据库文件存放在 /var/lib/pgsql/9.5/data # 初始化 mv /etc/init.d/{postgresql-9.5,postgresql} service postgresql initdb # 启动postgresql service postgresql start # 配置postgresql自启动 chkconfig postgresql on # 为Gitlab创建一个用户,用户名为git cd /home sudo -u postgres psql -d template1 -c "CREATE USER git CREATEDB;" #创建pg_trgm扩展 (required for GitLab 8.6+): sudo -u postgres psql -d template1 -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;" # 创建Gitlab生产环境数据库并赋予git用户属主权限 sudo -u postgres psql -d template1 -c "CREATE DATABASE gitlabhq_production OWNER git;" # 用git用户测试下是否能登录刚才创建的数据库 sudo -u git -H psql -d gitlabhq_production #检查是否启用 pg_trgm 扩展: SELECT true AS enabled FROM pg_available_extensions WHERE name = 'pg_trgm' AND installed_version IS NOT NULL; 如果启用了扩展,这将产生以下输出: enabled --------- t (1 row) # 退出数据库会话 gitlabhq_production> \q # 创建pg_config的软连接 ln -s /usr/pgsql-9.5/bin/pg_config /usr/bin/pg_config
5.配置Redis
#此处redis是yum安装,为了省事。 yum install -y redis # 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 #添加redis.sock文件信息 echo 'unixsocket /var/run/redis/redis.sock' |tee -a /etc/redis.conf echo -e 'unixsocketperm 0770' |tee -a /etc/redis.conf #创建文件并赋予权限 mkdir -p /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 ]; then echo 'd /var/run/redis 0755 redis redis 10d -' | sudo tee -a /etc/tmpfiles.d/redis.conf fi #将redis用户添加到git组 sudo usermod -aG redis git #配置redis使其在开机时启动: sudo chkconfig redis on sudo service redis start
6.安装GitLab
#将GitLab安装在git的家目录下: cd /home/git/ sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-6-stable gitlab cd /home/git/gitlab # 复制gitlab.yml(Gitlab的主配置文件) sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml # 修改gitlab.yml sudo -u git -H vim config/gitlab.yml host: gitlab.xxx.com ####修改第32行 为你的域名或者ip port: 80 https: false # 修改 log/ 和 tmp/ 文件夹权限 sudo chown -R git log/ sudo chown -R git tmp/ sudo chmod -R u+rwX,go-w log/ sudo chmod -R u+rwX tmp/ #创建文件夹gitlab-satellites sudo -u git -H mkdir /home/git/gitlab-satellites sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites # 修改 tmp/pids/ 和 tmp/sockets/ 文件夹权限 sudo chmod -R u+rwX tmp/pids/ sudo chmod -R u+rwX tmp/sockets/ # 创建 public/uploads/ 文件夹 sudo -u git -H mkdir public/uploads/ # 修改 public/uploads/ 文件夹权限,只有git用户有访问权限 sudo chmod -R u+rwX public/uploads # 复制 Unicorn 配置文件 sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb # 查询CPU核心数 nproc # 如果你想搭建一个高负载的Gitlab实例,可启用集群模式. # 修改'worker_processes'参数,至少要跟cpu核心数一样. # 修改监听地址和端口,要和下文 gitlab-shell/config.yml 中配置一致 sudo -u git -H vim config/unicorn.rb worker_processes 3 listen "your_IP:8080", :tcp_nopush => true # 复制Rack attack 配置文件 sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb # 为 git 用户配置用户和邮件 sudo -u git -H git config --global user.name "GitLab" sudo -u git -H git config --global user.email "example@example.com" # 'autocrlf' 需要Web编辑器 sudo -u git -H git config --global core.autocrlf input # 复制 Redis 连接配置文件 sudo -u git -H cp config/resque.yml.example config/resque.yml # 如修改过redis socket的路径,在这个配置文件里面修改,确认redis端口和ip. sudo -u git -H vim config/resque.yml ------------------------------------- development: redis://127.0.0.1:6379 test: redis://127.0.0.1:6379 production: unix:/var/run/redis/redis.sock
7.GitLab数据库配置
# 仅限于Mysql: sudo -u git cp config/database.yml.mysql config/database.yml # 仅限于PostgreSQl: sudo -u git cp config/database.yml.postgresql config/database.yml # 以下修改针对MySQL,修改username/password. sudo -u git -H vim config/database.yml production: adapter: mysql2 encoding: utf8 collation: utf8_general_ci reconnect: false database: gitlabhq_production pool: 10 username: git password: "gitpasswd" # host: localhost # socket: /tmp/mysql.sock socket: /var/lib/mysql/mysql.sock # 以下修改针对PostgreSQL,修改username/password. # 修改'secure password' 为你设置的密码,没单独设置则不改 production: adapter: postgresql encoding: unicode database: gitlabhq_production pool: 10 # username: git # password: #未设置不用修改 # host: localhost # port: 5432 # 修改database.yml的权限,确保git用户可以读取该文件. sudo -u git -H chmod o-rwx config/database.yml
8.Gems安装
# 进入gitlab目录 cd /home/git/gitlab -------------------------------------------------------------- gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/ -------------------------------------------------------------- # 修改 Gemfile 和 Gemfile.lock vim Gemfile(Gemfile.lock) 更改 source https://rubygems.org/ 为: source 'https://ruby.taobao.org/' -------------------------------------------------------------- # 确保只有 https://ruby.taobao.org/ gem sources -l https://ruby.taobao.org/ ln -s /usr/local/bin/bundle /usr/bin/ ####一定要注意选择自己用的数据库的命令 ###PostgreSQL (note, the option says "without ... mysql") sudo -u git -H bundle install --deployment --without development test mysql aws # 如果使用 MySQL,执行下面的命令 (note, the option says "without ... postgres") sudo -u git -H bundle install --deployment --without development test postgres aws 笔记: 如果你想去用 Kerberos 做用户认证, 然后在--without选项中省略Kerbero
9.Gitlab-shell安装
# Run the installation task for gitlab-shell (replace `REDIS_URL` if needed): sudo -u git -H bundle exec rake gitlab:shell:install[v2.4.0] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production # 默认情况下,gitlab-shell的配置是根据Gitlab的配置生产的. # 你可以运行下面的命令查看和修改gitlab-shell的配置, # 监听端口要和/home/git/gitlab/config/unicorn.rb中配置一致 sudo -u git -H vim /home/git/gitlab-shell/config.yml --- user: git gitlab_url: https://localhost:8080/ #使用https http_settings: self_signed_cert: false #如果gitlab_url为https,修改成true repos_path: "/home/git/repositories/" auth_file: "/home/git/.ssh/authorized_keys" redis: bin: "/usr/bin/redis-cli" namespace: resque:gitlab socket: "/var/run/redis/redis.sock" log_level: INFO audit_usernames: false
10.初始化数据库,激活高级特性
cd /home/git/gitlab sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production # 输入 'yes' 以创建数据库表 # 当看到以下内容,表示已经安装完成 Administrator account created: login: root password: your_passwd == Seed from /home/git/gitlab/db/fixtures/production/010_settings.rb
Note: 你能通过提供环境变量设置 Administrator/root 密码和邮箱, 分别为GITLAB_ROOT_PASSWORD 和 GITLAB_ROOT_EMAIL , 如下所示。如果你不能设置密码(它被设置为默认的) 请等待曝光gitlab到公共互联网直到安装完成和你已经登录到服务器的第一时间。 在第一次登录时,您将被迫更改默认密码。.
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail
11.下载服务脚本
wget -O /etc/init.d/gitlab https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/init/sysvinit/centos/gitlab-unicorn # 配置gitlab的启动级别 chmod +x /etc/init.d/gitlab chkconfig --add gitlab chkconfig gitlab on 设置Logrotate cd /home/git/gitlab sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab 检查GitLab环境配置 sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production ----- production System information System: CentOS 6.8 Current User: git Using RVM: no Ruby Version: 2.1.5p273 Gem Version: 2.2.2 Bundler Version:1.15.1 Rake Version: 10.3.2 Sidekiq Version:2.17.8 GitLab information Version: 7.6.2 Revision: fatal: Not a git repository (or any parent up to mount point /home) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set). Directory: /home/git/gitlab DB Adapter: mysql2 URL: http://your_ip HTTP Clone URL: http://your_ip/some-project.git SSH Clone URL: git@your_ip:some-project.git Using LDAP: no Using Omniauth: no GitLab Shell Version: 2.1.0 Repositories: /home/git/repositories/ Hooks: /home/git/gitlab-shell/hooks/ Git: /usr/bin/git # 生成GitLab前端资源 sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production # 启动gitlab服务 service gitlab start
12.安装nginx服务,并下载gitlab的站点配置文件
# 使用yum进行安装 yum install -y nginx chkconfig nginx on wget -O /etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab-ssl # 将nginx用户添加到git组 mkdir -p /etc/nginx/ssl cd /etc/nginx/ssl/ openssl req -new -x509 -nodes -days 3560 -out gitlab.crt -keyout gitlab.key # 启动nginx服务 service nginx restart
13.Gitlab配置SMTP邮件
# 使用SMTP邮件服务器,可以不安装postfix cd /home/git/gitlab sudo -u git -H cp config/initializers/smtp_settings.rb.sample config/initializers/smtp_settings.rb #修改如下 sudo -u git -H vim config/initializers/smtp_settings.rb 这里提供163和腾讯企业邮箱两种配置方式,注意将下面的123456替换成自己的密码 163邮箱: if Rails.env.production? Gitlab::Application.config.action_mailer.delivery_method = :smtp ActionMailer::Base.delivery_method = :smtp #使用smtp方法 ActionMailer::Base.smtp_settings = { address: "smtp.163.com", port: 25, user_name: "wpgitlab", password: "123456", domain: "163.com", authentication: :plain, enable_starttls_auto: true } end QQ邮箱: if Rails.env.production? Gitlab::Application.config.action_mailer.delivery_method = :smtp ActionMailer::Base.delivery_method = :smtp #使用smtp方法 ActionMailer::Base.smtp_settings = { address: "smtp.exmail.qq.com", port: 25, user_name: "wangpeng@scnee.com", password: "123456", domain: "smtp.qq.com", authentication: :plain, enable_starttls_auto: true, } end 3. 修改gitlab.yml cd /home/git/gitlab sudo -u git -H vim config/gitlab.yml 一键安装路径: cd /opt/gitlab-8.5.7-0 vim apps/gitlab/htdocs/config/gitlab.yml 将默认邮箱修改为自己的邮箱 163: ## Email settings # Email address used in the "From" field in mails sent by GitLab email_from: wpgit@163.com 腾讯企业邮箱: ## Email settings # Email address used in the "From" field in mails sent by GitLab email_from: wangpeng@scnee.com #编辑文件 [root@git gitlab]#sudo -u git -H vim config/environments/production.rb config.action_mailer.delivery_method = :smtp #将方法修改成smtp方式 4. 修改后。重启gitlab #监控日志文件 tail -f /home/git/gitlab/log/*.log tail -f /var/log/maillog
安装完成后gitlab运行异常使用如下命令检测,并解决
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
验证结果,访问地址如下:
用户名和密码如下
login.........root
password......5iveL!fe
备份恢复
Gitlab 创建备份
使用Gitlab一键安装包安装Gitlab非常单, 同样的备份恢复与迁移也非常简单,用一条命令即可创建完整的Gitlab备份:
cd /home/git/gitlab sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production STRATEGY=copy
注:跳过某些内容sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production SKIP=db,uploads
skip为:
db (database)
uploads (attachments)
repositories (Git repositories data)
builds (CI job output logs)
artifacts (CI job artifacts)
lfs (LFS objects)
registry (Container Registry images)
pages (Pages content)
Gitlab 修改备份文件默认目录
修改/home/git/gitlab/config/gitlab.yml
来修改默认存放备份文件的目录:
sudo -u git -H vim /home/git/gitlab/config/gitlab.yml ---------------------- backup: path: "/home/git/backups/" # Relative paths are relative to Rails.root (default: tmp/backups/)
自动备份
# Create a full backup of the GitLab repositories and SQL database every day at 4am 0 4 * * * cd /home/git/gitlab && PATH=/usr/local/bin:/usr/bin:/bin bundle exec rake gitlab:backup:creat # 0 4 * * * cd /home/git/gitlab && sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production
恢复
首先进入备份 gitlab 的目录,这个目录是配置文件中的path
,本文设置为 /home/git/backups/ 。
然后停止 unicorn 和 sidekiq ,保证数据库没有新的连接,不会有写数据情况。
# 停止相关数据连接服务 service gitlab stop cd /home/git/gitlab sudo -u git -H bundle exec rake gitlab:backup:restore RAILS_ENV=productione RAILS_ENV=production CRON=1 # 如果备份目录下面有多个备份文件,则需加参数并指明时间戳 BACKUP=timestamp_of_backup (required if more than one backup exists) force=yes (do not ask if the authorized_keys file should get regenerated) sudo -u git -H bundle exec rake gitlab:backup:restore RAILS_ENV=productione RAILS_ENV=production CRON=1 force=yes BACKUP=timestamp_of_backup #恢复时候BACKUP=timestamp_of_backup timestamp_of_backup表示是时间前缀 默认恢复最新一次的备份 # 启动Gitlab sudo gitlab-ctl start
# 恢复过程中没有权限 mkdir /home/git/backups/ chown git /home/git/backups/ chmod 700 /home/git/backups/
迁移
把备份文件拷贝到gitlab的备份目录下,根据上面gitlab恢复步骤即可。
备份注意事项:
还需要备份文件:/home/git/gitlab/config/secrets.yml(两步验证时用到:用户名密码登录及短信验证)