前言:源碼安裝Gitlab其實(shí)并不推薦评甜,既麻煩還容易出現(xiàn)問題灰粮,不是很推薦新手。推薦大家使用 docker 或者官方的一鍵包去安裝忍坷。這里只是記錄自己搭建的過程粘舟,親測好用。
一佩研、yum 安裝依賴
CentOS 7 默認(rèn)自帶了 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下的所有依賴的包嘉抒,相當(dāng)于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/
四岗宣、安裝數(shù)據(jù)庫
MySQL
已經(jīng)不再包含在 CentOS 7 的源中蚂会,而改用了 MariaDB
,先搜索 MariaDB
現(xiàn)有的包:
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 //這個目錄是單獨(dú)創(chuàng)建的耗式,請注意
初始化
mysql_install_db --user=mysql --datadir=/data/mysql/
設(shè)置root的密碼和相關(guān)登錄信息
# 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 [設(shè)置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遠(yuǎn)程登錄]
... 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數(shù)據(jù)庫]
- 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 [刷新權(quán)限]
... 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...
創(chuàng)建一個gitlab用戶胁住,以及庫,用來存儲數(shù)據(jù)
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
源碼安裝彪见,安裝包網(wǎng)上自行下載
tar zxvf redis-3.0.3.tar.gz
cd redis-3.0.3
make
make install
執(zhí)行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的啟動腳本娱挨,并啟動服務(wù)
mv /etc/init.d/redis_6379 /etc/init.d/redis
service redis start
創(chuàng)建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/"
進(jìn)行安裝操作
[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
配置項(xiàng)目跷坝,提供配置文件
cd gitlab
cp config/gitlab.yml.example config/gitlab.yml
將localhost改為本機(jī)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
修改目錄的相關(guān)權(quán)限
[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的配置,指明監(jiān)聽的端口和超時時間
[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 "gitlab@test.com"
[git@gitlab ~]$ git config --global core.autocrlf input
配置數(shù)據(jù)庫柴钻,使gitlab將數(shù)據(jù)存入到MySQL數(shù)據(jù)庫中
[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操作將會報錯靠粪,會提示無法連接數(shù)據(jù)庫。
修改目錄權(quán)限毫蚓,保證其他用戶不能訪問
[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 #默認(rèn)的管理員密碼
為gitlab提供啟動腳本
# run as root I苄@袒拧!
[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服務(wù)
[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"
這是因?yàn)槲业?code>nginx使用的是www
用戶運(yùn)行,而/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket/
用戶組是git
,
解決方案:把git
加入www
用戶組,并賦予實(shí)行權(quán)限
usermod -a -G git www
chmod g+rx /home/git/
2. 在進(jìn)行到Install Gems時出錯:Make sure that gem install RedCloth -v '4.2.9' succeeds before bundling
gem install RedCloth -v '4.2.9'
又出現(xiàn)新問題: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 ‘a(chǎn)pt-get install libmysqlclient-dev’
aptitude install libmysqlclient-dev