因开发需要,我需要安装一个Git服务器供公司内部做代码托管之用。当前最著名的Git服务器就是GitLab。Scientific Linux是重新编译的Red Hat Enterprise Linux,由费米国家加速器实验室、欧洲核研究组织以及世界各地的大学和实验室共同开发。
下面记录下我在Scientific Linux 6.4版(64位)上安装GitLab 6.0.2的过程。 因为同属RHEL系列,相信在RHEL 6.4和CentOS 6.4上面同样能够正常进行。
安装依赖包
(1)安装额外的包仓库
su -
yum update –y
wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://fedoraproject.org/static/0608B895.txt
rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum-config-manager –enable epel
rpm -ivh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
(2)安装必需的依赖软件包
yum -y groupinstall ‘Development Tools’
yum -y install vim-enhanced readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel openssl-devel curl-devel expat-devel db4-devel byacc sqlite-devel gcc-c++ libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel libicu libicu-devel system-config-firewall-tui python-devel redis sudo wget crontabs logwatch logrotate perl-Time-HiRes
如果一些软件包(例如gdbm-devel, libffi-devel和libicu-devel)未能安装,运行下面的命令:
yum-config-manager –enable rhel-6-server-optional-rpms
然后重新安装上面的软件包。
(3)安装python 2.7
yum install –y python27 python27-devel python-docutils
cd /usr/bin/
rm -rf python
cp python2.7 python
输入下面的命令检查python是否切换到2.7版:
python –version
由于yum命令不兼容python2.7,需修改/usr/bin/yum文件,将第一行由“#!/usr/bin/python”改为“#!/usr/bin/python2.6”。
由于ibus中文输入法默认使用python 2.6,所以要修改/usr/bin/ibus-setup和/usr/libexec/ibus-ui-gtk,把其中的“exec python”改成“exec python2.6”。
安装Git 1.8
cd /tmp
yum -y install git perl-ExtUtils-MakeMaker
git clone git://github.com/git/git.git
cd /tmp/git/
git checkout v1.8.4
autoconf
./configure –prefix=/usr/local
make && make install
rm -rf /tmp/git/
yum erase git
ln -s /usr/local/git /usr/bin/git
安装Ruby 2.0
yum remove ruby1.8
mkdir /tmp/ruby && cd /tmp/ruby
curl –progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz | tar xz
cd ruby-2.0.0-p247
./configure
make
sudo make install
gem install bundler –no-ri –no-rdoc
创建git用户
adduser –system –shell /bin/bash –comment ‘GitLab’ –create-home –home-dir /home/git/ git
安装GitLab shell
GitLab shell是一个ssh访问和repository管理软件,专为Gitlab开发。
# 进入git用户的home目录
cd /home/git
su git# 用git克隆gitlab shell到本机
git clone https://github.com/gitlabhq/gitlab-shell.gitcd gitlab-shell
# 检出最新的版本
git checkout v1.7.1cp config.yml.example config.yml
# 修改config.yml 文件,修改配置内容,将gitlab_url设置为你准备用来提供git服务的url,例如’http://git.dayatang.org/’
vim config.yml# 进行安装设置
./bin/install
安装数据库
Gitlab支持两种数据库:MySQL(首选)和Postgresql。我们选择MySQL:
#返回root用户
exit# 安装数据库软件包
yum install -y mysql-server mysql-devel
chkconfig mysqld –level 235 on
service mysqld start#设置root用户的口令以保证MySQL的安全:
/usr/bin/mysql_secure_installation# 登录MySQL
mysql -u root -p# 输入root用户的口令
# 创建数据库用户gitlab。把下面命令行中的$password改为你自己选定的口令:
mysql> CREATE USER ‘gitlab’@'localhost’ IDENTIFIED BY ‘$password’;# 创建GitLab产品数据库:
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 ‘gitlab’@'localhost’;# 退出MySQL会话:
mysql> \q# 尝试用新创建的gitlab用户连接数据库:
su git
mysql -u gitlab -p -D gitlabhq_production
mysql> \q
exit# 数据库安装完毕,返回正常安装进程。
安装Gitlab
su git
cd /home/git
(1)下载gitlab源代码
git clone https://github.com/gitlabhq/gitlabhq.git gitlab
cd /home/git/gitlab
git checkout 6-1-stable
(2)配置gitlab
# 复制范例GitLab配置
cp config/gitlab.yml.example config/gitlab.yml# 修改config/gitlab.yml,将”localhost”改为你准备用来提供git服务的域名,例如’git.dayatang.org’
# 同时修改email_from, support_email
vim config/gitlab.yml# 保证git用户能够写入log/和tmp/目录
exit
chown -R git log/
chown -R git tmp/
chmod -R u+rwX log/
chmod -R u+rwX tmp/# 为satellites创建目录
su git
mkdir /home/git/gitlab-satellites# 为sockets/pids创建目录, 并保证git用户对它们有写权限
mkdir tmp/pids/
mkdir tmp/sockets/
exit
chmod -R u+rwX tmp/pids/
chmod -R u+rwX tmp/sockets/# 创建public和uploads目录,否则备份将失败
su git
mkdir public/uploads
exit
chmod -R u+rwX public/uploads# 复制范例Unicorn配置
su git
cp config/unicorn.rb.example config/unicorn.rb# 如果你想拥有高负载的实例,请启用集群模式,例如在具有2GB内存的服务器上将workers数量设为3。
# 将”127.0.0.1:8080″改为”127.0.0.1:9292″,因为8080端口一般为tomcat所占据。
vim config/unicorn.rb# 为git用户配置Git全局性配置,当需要通过web编辑时有用
# 根据gitlab.yml中的电子邮件设置设置user.email的值
git config –global user.name “GitLab”
git config –global user.email “gitlab@localhost”
git config –global core.autocrlf input
注意:记得同时编辑gitlab.yml和unicorn.rb中的内容,以保证相互匹配。
(3)配置GitLab数据库
cp config/database.yml.mysql config/database.yml
#修改config/database.yml 数据库设置(仅需要修改产品数据库部分,即第一部分),设置username为gitlab,password为上面设置的口令:
vim config/database.yml# 使config/database.yml仅对git用户可读
chmod o-rwx config/database.yml
(4)安装Gems
cd /home/git/gitlab
exit
gem update rdoc
gem install charlock_holmes –version ’0.6.9.4′
su git
bundle install –deployment –without development test postgres aws
(5)初始化数据库并激活高级特性
bundle exec rake gitlab:setup RAILS_ENV=production
# 键入’yes’创建数据库。当执行完成之后,会显示’Administrator account created:’。这个系统帐号口令信息记录下来,将来要用来登录gitlab管理界面。
(6)安装Init脚本
exit
cp lib/support/init.d/gitlab /etc/init.d/gitlab
chmod +x /etc/init.d/gitlab
chkconfig gitlab –level 235 on
(7)检查应用状态
su git
bundle exec rake gitlab:env:info RAILS_ENV=production
(8)启动GitLab实例
exit
service gitlab start
(9)再次检查应用状态
bundle exec rake gitlab:check RAILS_ENV=production
如果所有的项目都是绿色的,则说明GitLab安装成功
配置Apache
我们要配置Apache Web服务器来提供外界访问GitLab。
(1)创建SSL证书
exit
cd ~
openssl genrsa -out root-key.key 1024
openssl req -new -out root-req.csr -key root-key.key -keyform PEM
openssl x509 -req -in root-req.csr -out root-cert.cer -signkey root-key.key -CAcreateserial -days 3650
openssl pkcs12 -export -clcerts -in root-cert.cer -inkey root-key.key -out root.p12
openssl genrsa -out git.dayatang.org.key 1024
openssl req -new -key git.dayatang.org.key -out git.dayatang.org.csr
openssl x509 -req -in git.dayatang.org.csr -out git.dayatang.org.crt -signkey git.dayatang.org.key -CA /root/root-cert.cer -CAkey /root/root-key.key -CAcreateserial -days 3650mkdir /etc/httpd/ssl/
cp git.dayatang.org.* /etc/httpd/ssl/
cp root-cert.cer /etc/httpd/ssl
(2)设置虚拟主机
yum -y install httpd mod_ssl
chkconfig httpd –level 235 on
wget -O /etc/httpd/conf.d/gitlab.conf https://raw.github.com/gitlabhq/gitlab-recipes/master/web-server/apache/gitlab.conf# 修改/etc/httpd/conf.d/gitlab.conf文件,将git.example.org改为你的域名,将端口8080改为9292。
vim /etc/httpd/conf.d/gitlab.conf可以将GitLab配置为虚拟主机。修改/etc/httpd/conf/httpd.conf文件,加入下面的内容:
NameVirtualHost *:80
<IfModule mod_ssl.c>
# If you add NameVirtualHost *:443 here, you will also have to change
# the VirtualHost statement in /etc/httpd/conf.d/gitlab.conf
# to <VirtualHost *:443>
NameVirtualHost *:443
Listen 443
</IfModule>
(3)穿透SELinux
setsebool -P httpd_can_network_connect on
(4)启动Apache
service httpd start
(5)配置防火墙
vim /etc/sysconfig/iptables
照下表所示打开80和443号端口:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
重启防火墙:
service iptables restart
完成了!
打开浏览器,输入网址:https://git.dayatang.org/,在页面出现后,用下面的帐号口令登录:
admin@local.host
5iveL!fe
你将被重定向到修改口令页面。