前言:源码安装Gitlab其实并不推荐,既麻烦还容易出现问题,不是很推荐新手。推荐大家使用 docker 或者官方的一键包去安装。这里只是记录自己搭建的过程,亲测好用。
一、yum 安装依赖
CentOS 7 默认自带了 EPEL 源,如果没有的话请自行添加。
# run as root !!!
yum update
yum install gcc ruby-devel libxml2 libxml2-devel libxslt libxslt-devel
二、安装 Ruby
先卸载自带的ruby
yum remove ruby
源码安装Ruby2.3.1版本
url --remote-name --progress https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
tar xvzf ruby-2.3.1.tar.gz
cd ruby-2.3.1
./configure --disable-install-rdoc
make
make install
安装Bundler Gem (它是帮助管理ruby下的所有依赖的包,相当于yum的功能)
gem install bundler --no-ri --no-rdoc
更换ruby源,自带的源下载软件包太慢。更换为淘宝的。
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
三、安装GO(gitlab-workhorse需要用)
新版本的 gitlab 使用 HTTPS 方式去 clone 或者 push 代码的时候,需要用到 workhorse
curl --remote-name --progress https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz
ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
四、安装数据库
MySQL
已经不再包含在 CentOS 7 的源中,而改用了 MariaDB
,先搜索 MariaDB
现有的包:
rpm -qa | grep mariadb
卸载
rpm -e --nodeps mariadb-*
安装MySQL
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
yum install mysql-community-devel
更改配置文件
vim /etc/my.cnf
[mysqld]
datadir=/data/mysql //这个目录是单独创建的,请注意
初始化
mysql_install_db --user=mysql --datadir=/data/mysql/
设置root的密码和相关登录信息
# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y [设置root用户密码]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y [删除匿名用户]
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y [禁止root远程登录]
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y [删除test数据库]
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y [刷新权限]
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
创建一个gitlab用户,以及库,用来存储数据
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `gitlab` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
Query OK, 1 row affected (0.01 sec)
mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlab`.* TO 'gitlab'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
启动mysql
service mysqld start
四、安装redis
先卸载自带的redis
yum remove redis
源码安装,安装包网上自行下载
tar zxvf redis-3.0.3.tar.gz
cd redis-3.0.3
make
make install
执行redis的安装脚本(不要一路回车,看清楚)
[root@gitlab redis-3.0.3]# ./utils/install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /etc/redis.conf Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis.log Please select the data directory for this instance [/var/lib/redis/6379] /var/lib/redis.pid
Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379
Config file : /etc/redis.conf
Log file : /var/log/redis.log
Data dir : /var/lib/redis.pid
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
更改redis的启动脚本,并启动服务
mv /etc/init.d/redis_6379 /etc/init.d/redis
service redis start
创建git用户
useradd --comment 'GitLab' git
echo "git ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
安装gitlab-shell
切换到git用户,clone新版gitlab-shell到git家目录中
su - git
git clone https://github.com/gitlabhq/gitlab-shell.git -b v3.6.1 gitlab-shell
将分支切换为v3.6.1
cd gitlab-shell
git checkout -b v3.6.1
提供配置文件,修改gitlab的URL地址
cp config.yml.example config.yml
vim config.yml
gitlab_url: "http://192.168.100.100/"
或者 gitlab_url: "http://git.test.com/"
进行安装操作
[git@gitlab~ gitlab-shell]$ ./bin/install
mkdir -p /home/git/repositories: true
mkdir -p /home/git/.ssh: true chmod 700 /home/git/.ssh: true
touch /home/git/.ssh/authorized_keys: true
chmod 600 /home/git/.ssh/authorized_keys: true
chmod -R ug+rwX,o-rwx /home/git/repositories: true
find /home/git/repositories -type d -print0 | xargs -0 chmod g+s: true
安装gitlab
克隆gitlab的代码到git家目录
su - git
cd
git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-10-stable gitlab
配置项目,提供配置文件
cd gitlab
cp config/gitlab.yml.example config/gitlab.yml
将localhost改为本机ip或者是域名
[git@gitlab ~]$ sed -i 's/localhost/192.168.100.100/g' config/gitlab.yml
或者
[git@gitlab ~]$ sed -i 's/localhost/git.test.com/g' config/gitlab.yml
修改目录的相关权限
[git@gitlab ~]$ chown -R git log/
[git@gitlab ~]$ chown -R git tmp/
[git@gitlab ~]$ chmod -R u+rwx log/
[git@gitlab ~]$ chmod -R u+rwx tmp/
[git@gitlab ~]$ mkdir /home/git/gitlab-satellites
#可以视为临时目录,通过web ui的提交请求文件以及检出版本库都会存放在这个位置
提供unicorn的配置
[git@gitlab ~]$ cp config/unicorn.rb.example config/unicorn.rb
修改unicorn的配置,指明监听的端口和超时时间
[git@gitlab ~]$ vim config/unicorn.rb
listen "192.168.100.100:2486", :tcp_nopush => true
timeout 300
配置git的用户和邮件
[git@gitlab ~]$ git config --global user.name "GitLab"
[git@gitlab ~]$ git config --global user.email "[email protected]"
[git@gitlab ~]$ git config --global core.autocrlf input
配置数据库,使gitlab将数据存入到MySQL数据库中
[git@gitlab ~]$ cp config/database.yml.mysql config/database.yml
[git@gitlab ~]$ vim config/database.yml
production:
adapter: mysql2
encoding: utf8
collation: utf8_general_ci
reconnect: false
database: gitlab
pool: 10
username: gitlab
password: "gitlab"
这里注意一下,我的配置如下,如果host和socket的注释不打开,后面gem操作将会报错,会提示无法连接数据库。
修改目录权限,保证其他用户不能访问
[git@gitlab ~]$ chmod o-rwx config/database.yml
切回git用户
[root@gitlab ~]# su - git
[git@gitlab ~]$ cd gitlab
修改Gemfile文件,将gem源改成淘宝的源
[git@gitlab ~]$ vim Gemfile
source "https://ruby.taobao.org"
安装一些依赖包
[git@gitlab ~]$ bundle install --deployment --without development test postgres puma aws
...
Remember to run generator to generate sample file and include mousetrap-rails with Rails Asset Pipeline
$ rails generate mousetrap:install
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay! #显示这个就代表安装基本成功了。
初始化gitlab
[git@gitlab ~]$ bundle exec rake gitlab:setup RAILS_ENV=production
...
This will create the necessary database tables and seed the database.
You will lose any previous data stored in the database. Do you want to continue (yes/no)? yes
Administrator account created:
login.........root
password......5iveL!fe #默认的管理员密码
为gitlab提供启动脚本
# run as root !!!
[root@gitlab ~]# wget -O /etc/init.d/gitlab https://raw.githubusercontent.com/gitlabhq/gitlab-recipes/master/init/sysvinit/centos/gitlab-unicorn --no-check-certificate
[root@gitlab ~]# chmod +x /etc/init.d/gitlab
[root@gitlab ~]# chkconfig --add gitlab
启动gitlab服务
[root@gitlab ~]# service gitlab start
这就可以使用了,nginx
配置一下 proxy_pass
反向代理,即可访问。
错误解决
1. nginx报错
2016/07/19 09:26:11 [crit] 3881#0: *10 connect() to unix:/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket failed (13: Permission denied) while connecting to upstream, client: 192.168.1.180, server: www.gitlab810.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket:/", host: "www.gitlab810.com"
2016/07/19 09:26:11 [error] 3881#0: *10 open() "/home/git/gitlab/public/502.html" failed (13: Permission denied), client: 192.168.1.180, server: www.gitlab810.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket/", host: "www.gitlab810.com"
这是因为我的nginx
使用的是www
用户运行,而/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket/
用户组是git
,
解决方案:把git
加入www
用户组,并赋予实行权限
usermod -a -G git www
chmod g+rx /home/git/
2. 在进行到Install Gems时出错:Make sure that gem install RedCloth -v '4.2.9' succeeds before bundling
gem install RedCloth -v '4.2.9'
又出现新问题:can’t find header files for ruby at /usr/lib/ruby/include/ruby.h
apt-get install ruby-dev
3. Make sure that gem install charlock_holmes -v '0.7.3' succeeds before
gem install charlock_holmes -v '0.7.3'
ERROR: Error installing charlock_holmes: ERROR: Failed to build gem native extension.
apt-get install libicu-dev
4. Make sure that gem install mysql2 -v '0.3.20' succeeds before bundling
gem install mysql2 -v '0.3.20'
libmysqlclient is missing. You may need to ‘apt-get install libmysqlclient-dev’
aptitude install libmysqlclient-dev
参考文档
官方文档
Ubuntu 系统