运维人

基于CentOS6.5 x86_64 源码搭建GitLab

系统:CentOS6.5 X86_64 已完成初始化:防火墙、SELinux 关闭、不必要服务停止,不必要用户删除………

1.添加epel源

wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://www.fedoraproject.org/static/0608B895.txt && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

#查看Key是否安装成功
rpm -qa gpg*

 

2.安装epel源

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

 

3.添加PUIAS源

wget -O /etc/yum.repos.d/PUIAS_6_computational.repo https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/install/centos/PUIAS_6_computational.repo

 

4.下载并安装gpg-key

wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-puias http://springdale.math.ias.edu/data/puias/6/x86_64/os/RPM-GPG-KEY-puias && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-puias

#查看源是否添加成功[如果不成功,执行:yum-config-manager --enable epel --enable PUIAS_6_computational]
yum repolist

 

5.安装整个搭建gitlab的相关的依赖包

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

 

6.配置默认编辑器

yum -y install vim-enhanced
update-alternatives --set editor /usr/bin/vim.basic
ln -s /usr/bin/vim /usr/bin/edito

#reStructuredText markup语法支持,需要安装依赖包:
yum install -y python-docutils

 

7.安装git(git>=1.7.10 如果低于我们可以从新编译安装)

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.1.3.tar.gz | tar xz
cd git-2.1.3/ && ./configure --prefix=/usr/local/git && make && make install

 

8.安装ruby(如果系统中Ruby的版本是2.0以前的那么请移除,GitLab只支持Ruby 2.0+版本)

yum remove ruby

mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz
cd ruby-2.1.2
./configure --prefix=/usr/local/ --disable-install-rdoc
make
make install
#记得检查ruby的版本是否是最新
ruby --version

 

9.安装Bundler Gem(由于`http://rubygems.org`已被墙,这里替换源为`https://ruby.taobao.org`)

# 添加淘宝源并且移除官方源
gem sources -a https://ruby.taobao.org
gem sources -r https://rubygems.org/

 

10.安装Bundler

gem install bundler -v'1.5.2' --no-doc

 

11.System Users为GitLab创建用户

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

 

12.编辑sudoers文件,将ruby和git的程序路径添加到PATH中,使git用户作为root来使用gem命令

sed -ie 's@Defaults\    secure_path\ \=\ \/sbin\:\/bin\:\/usr\/sbin\:\/usr\/bin@Defaults\    secure_path\ \=\ \/sbin\:\/bin\:\/usr\/sbin\:\/usr\/bin\:/usr\/local\/bin@g' /etc/sudoers

 

13.安装mysql

yum install -y mysql mysql-devel mysql-server
service mysqld start

#为gitlab创建数据库
#创建用户
mysql -e "CREATE USER 'git'@'localhost' IDENTIFIED BY 'xxxxx';"

#创建数据库
mysql -e "CREATE DATABASE IF NOT EXISTS \`gitlabhq_production\` DEFAULT CHARACTER SET \`utf8\` COLLATE \`utf8_unicode_ci\`;"

#授权
mysql -e "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \`gitlabhq_production\`.* TO 'git'@'localhost';"

#用git用户尝试登陆验证是否成功
sudo -u git -H mysql -u git -pxxxxxx  -e 'show databases;'

#记得要给root用户密码哦。

 

14.安装redis(已经在前面的依赖包安装时安装了)

chkconfig redis on

#配置Redis使用sockets
cp /etc/redis.conf /etc/redis.conf.orig

# 关闭Redis 监听于TCP
sed 's/^port .*/port 0/' /etc/redis.conf.orig | sudo tee /etc/redis.conf

#启用Redis socket
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis.conf
echo -e 'unixsocketperm 0770' | sudo tee -a /etc/redis.conf

#设置socket目录属主(组)为redis
chown redis:redis /var/run/redis
chmod 755 /var/run/redis

#将git用户添加至redis组
usermod -aG redis git

#启动redis服务
service redis start

 

15.安装gitlab,以及相关配置

cd /home/git/
sudo -u git -H git clone http://git.oschina.net/Yxnt/gitlab

# 配置gitlab
cd /home/git/gitlab && sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

#访问地址为本机IP
sudo -u git -H sed -ie "s/host: .*/host: `hostname --all-ip-addresses`/g" config/gitlab.yml

#创建satellites目录
sudo -u git -H mkdir /home/git/gitlab-satellites && chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites

#确认gitlab可以写入tmp/pids、tmp/sockets以及public/uploads/目录
chmod -R u+rwX tmp/pids/ && chmod -R u+rwX tmp/sockets/ && chmod -R u+rwX  public/uploads

#复制Unicorn配置文件
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

#复制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"
sudo -u git -H git config --global core.autocrlf input

#配置Redis连接设置
sudo -u git -H cp config/resque.yml.example config/resque.yml
sudo -u git -H sed -ie "s/develo.*/development:\ unix:\/var\/run\/redis\/redis.sock/g" config/resque.yml

#配置Gitlab 数据库设置(注意这里的socket文件位置,不同版本的mysql位置不一样)
sudo -u git cp config/database.yml.mysql config/database.yml
sudo -u git sed -ie "12s/password: \"secure password\"/password: $MYSQL_PASS/g" config/database.yml
sudo -u git sed -ie "13,14s/#\ //g" config/database.yml
sudo -u git sed -ie "14s@socket: \/tmp\/mysql.sock@socket: \/var\/lib/mysql\/mysql.sock@g" config/database.yml

#确定database.yml文件只为git用户可读
sudo -u git -H chmod o-rwx config/database.yml

 

16.安装Gems

cd /home/git/gitlab

#更换配置文件中的ruby源
sudo -u git -H sed -ie "s@source\ \"http:\/\/rubygems.org\"@source\ \"http:\/\/ruby.taobao.org\"@g" ../gitlab-shell/Gemfile

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

 

17.安装,配置gitlab-shell

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

#配置(在安装过程中其实gitlab-shell配置已经自动生成了,但是有必要检查核实一下里面的配置,尤其是gitlab_url)
cp /home/git/gitlab-shell/config.yml /home/git/gitlab-shell/config.yml.bak

 

18.初始化数据并且激活高级特性

#这里会提示输入yes/no,输入yes即可(我这里直接传递一个yes过去)
echo yes| sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
#这里会生成一个账户和密码:
#login.........root
#password......5iveL!fe 这个密码可以更改: sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=YOUR_NETPASSWORD

#生成js.css等文件(如果没有这一步的话,你在访问时网页css等是乱的)
cd /home/git/gitlab
bundle exec rake assets:precompile RAILS_ENV=production

 

19.安装gitlab启动管理脚本

wget -O /etc/init.d/gitlab https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/init/sysvinit/centos/gitlab-unicorn
chmod +x /etc/init.d/gitlab
chkconfig --add gitlab
chkconfig gitlab on

#设置gitlab服务的日志滚动
cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

 

20.检查应用程序状态

sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
#执行该命令 如果顺利的话,会将关于gitlab的 相关详细信息展示出来

和下图差不多

gitlab-information

 

21.启动gitlab

#启动gitlab
service gitlab start

 

22.安装nginx ,将访问请求反代到后端的8080端口

yum install -y nginx && chkconfig nginx on

#删除nginx中的这个默认配置文件
rm -rf /etc/nginx/conf.d/default.conf

#获取gitlab的nginx模板配置文件
wget -O /etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab

#对模板文件的一些修改
sed -ie "38s/.*/server\ localhost:8080;/g" /etc/nginx/conf.d/gitlab.conf
sed -ie "s/YOUR_SERVER_FQDN/`hostname`/g" /etc/nginx/conf.d/gitlab.conf
sed -ie "52d" /etc/nginx/conf.d/gitlab.conf

#将用户nginx加入到git组(这步非常的关键)
usermod -a -G git nginx
chmod g+rx /home/git/

#启动nginx
service nginx start

 

ok ,至此gitlab的安装已经结束。我们通过http://YOUR-SERVER-IP/ 就可以访问到你的gitlab平台了

gitlab-login

    分享到:
码字很辛苦,转载请注明来自运维人《基于CentOS6.5 x86_64 源码搭建GitLab》

评论