CentOS 6 搭建 GitLab 完整流程
前言
鉴于原有项目托管于公网 GitLab 存在的安全隐患,决定在内网环境中部署一套具备图形化界面的私有 GitLab 实例,以便直接从外网仓库导入数据。经过实际测试,该方案操作便捷,现将完整的安装流程整理如下。参考文档源自官网 Recipes 目录:https://gitlab.com/gitlab-org/gitlab-recipes/tree/master/install/centos ,读者也可前往查阅官方原版说明。
首先需完成基础运行环境的安装,包括 Git、Redis、Ruby 运行时及 MySQL 数据库等核心组件及其依赖库。
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 redis sudo wget crontabs logwatch logrotate perl-Time-HiRes git cmake libcom_err-devel.i686 libcom_err-devel.x86_64 nodejs
yum -y install python-docutils
yum -y install postfix
git --version #安装高于2.7.4的git版本
yum -y remove git
yum install zlib-devel perl-CPAN gettext curl-devel expat-devel gettext-devel openssl-devel
mkdir /tmp/git && cd /tmp/git
curl --progress https://www.kernel.org/pub/software/scm/git/git-2.9.0.tar.gz | tar xz
cd git-2.9.0
./configure
make
make prefix=/usr/local install
yum remove ruby #安装高于2.1版本的ruby,删除老的
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.9.tar.gz | tar xz
cd ruby-2.1.9
./configure --disable-install-rdoc
make
make prefix=/usr/local install
gem install bundler --no-doc
yum install redis mysql
/Data/apps/mysql/bin/mysqld_safe &
/Data/apps/mysql/bin/mysql -uroot
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> grant all on gitlabhq_production.* to 'git'@'localhost' identified by 'git';
接下来对 Redis 服务进行基础配置,确保其能正常监听本地套接字并设置相应的访问权限。
编辑 Redis 配置文件 /etc/redis.conf,调整相关参数以适配 GitLab 的运行需求。
unixsocket /var/run/redis/redis.sock
unixsocketperm 0770
mkdir -p /var/run/redis
chown redis:redis /var/run/redis
chmod 755 /var/run/redis
/etc/init.d/redis start
usermod -aG redis git
随后进入 GitLab 应用本身的配置阶段,包括下载源码、修改主机名及密钥文件等。
# 下载git,用的国内源。国外的太慢了
cd /home/git
sudo -u git -H git clone http://git.oschina.net/qiai365/gitlab-ce gitlab
cd /home/git/gitlab
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
sudo -u git -H editor config/gitlab.yml
# 修改成自己的host host: iaasgit1.prod.bj1
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 public/uploads/
sudo chmod 0700 public/uploads
sudo chmod ug+rwX,o-rwx /home/git/repositories/
sudo chmod -R u+rwX builds/
sudo chmod -R u+rwX shared/artifacts/
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
nproc
sudo -u git -H editor config/unicorn.rb
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
sudo -u git -H git config --global core.autocrlf input
sudo -u git -H git config --global gc.auto 0
sudo -u git -H cp config/resque.yml.example config/resque.yml
sudo -u git -H editor config/resque.yml
针对数据库部分,若使用 MySQL 作为后端存储,需专门配置数据库连接信息,仅适用于 MySQL 场景。
sudo -u git cp config/database.yml.mysql config/database.yml
sudo -u git -H editor config/database.yml #配置production部分的password: "密码"
sudo -u git -H chmod o-rwx config/database.yml
执行 GitLab 相关依赖包的安装脚本,并完成服务的启动初始化工作。
# install gems
cd /home/git/gitlab
sudo -u git -H bundle install --deployment --without development test postgres aws kerberos
# install gitlab shell
sudo -u git -H bundle exec rake gitlab:shell:install[v3.3.3] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
sudo -u git -H editor /home/git/gitlab-shell/config.yml
# install gitlab-workhorse
cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
sudo -u git -H git checkout v0.7.5
sudo -u git -H make
# Initialize Database and Activate Advanced Features
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail
cp lib/support/init.d/gitlab /etc/init.d/gitlab
cp lib/support/init.d/gitlab.default.example /etc/default/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
service gitlab start
配置 Nginx 作为反向代理服务器,将 HTTP 请求转发至 GitLab 应用端口。
yum install nginx
cp lib/support/nginx/gitlab /Data/apps/nginx/conf/include/gitlab.conf
usermod -a -G git nginx
chmod g+rx /home/git/
/Data/apps/nginx/conf/include/gitlab.conf #把这个配置里的server_name配置好
最后通过官方提供的检查脚本来验证整个环境的健康状态。
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
# 启动gitlab
/etc/init.d/gitlab restart
# 直接打开server_name对应的url,发现样式都没了,执行
sudo -u git -H bundle exec rake assets:clean assets:precompile REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
最后说明一下
本记录仅为操作流水账,实际部署中可能涉及复杂的权限问题。建议重点检查 Nginx 用户与 Git 用户之间的文件读写权限,并利用 check 脚本进行自检,通常提示友好的错误信息均能顺利解决,预祝部署成功。
文中使用的 yum 包管理器基于重打包的 RPM 源,导致部分路径与默认情况不同。若读者使用默认源,请根据实际环境调整路径,或直接参照官网文档。此处内容为基于个人环境的简化版示例。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。