GitLab詳細部署

   最近由於開發的需求,需要在公司部署一個git的服務器,在剛開始選擇的時候,我打算使用gitolite來管理git,後來發現gitlab這個系統更加方便,它既支持ssh方式,又支持http方式,用戶能夠通過命令行甚至是web來提交自己的代碼,非常的實用。


   gitlab是一個基於ruby的開源項目,能夠實現git倉庫的功能,能夠在網頁上直接瀏覽自己的代碼,下面主要介紹一下部署的過程,主要分爲如下幾個大步驟:

1、提供一些必須包,提供epel,關閉防火牆和selinux等操作;
2、gitlab依賴的python功能比較多,需要升級python到2.7.X;
3、安裝redis的內存數據庫;
4、安裝MySQL的關係型數據庫;
5、爲gitlab提供ruby的環境支持;
6、提供gitlab-shell來管理git倉庫;
7、安裝gitlab系統;
8、安裝nginx的web服務,做反向代理。


1、提供epel源,安裝相應包

[root@gitlab ~]# rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@gitlab ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

下面提示你安裝的包,一定要裝,要不然,你會在部署過程中折返回來裝的

[root@gitlab ~]# yum install -y git gcc-c++ libyaml libicu-devel cmake nodejs

關閉防火牆和selinux,這對你的實驗非常重要,要不然,排了半天錯,原來是它搞的鬼

[root@gitlab ~]# iptables -F
[root@gitlab ~]# setenforce 0


2、升級python到2.7.X

查看當前系統中python版本,CentOS6.5的默認是2.6.6

[root@gitlab ~]# python -V
Python 2.6.6

到python.org上去下載源代碼,進行如下編譯安裝

[root@gitlab ~]# tar Jxf Python-2.7.6.tar.xz 
[root@gitlab ~]# cd Python-2.7.6
[root@gitlab ~]# ./configure --prefix=/usr/local/python
[root@gitlab ~]# make && make install

備份python2.6的版本,並把新的2.7指向到系統中去

[root@gitlab ~]# mv /usr/bin/python /usr/bin/python2.6.6 
[root@gitlab ~]# ln -sv /usr/local/python/bin/python /usr/bin/python

再次查看當前系統的python版本

[root@gitlab ~]# python -V
Python 2.7.6

爲了防止yum安裝軟件出錯,將yum的依賴的python指回2.6.6

[root@gitlab ~]# vim /usr/bin/yum
#!/usr/bin/python2.6.6


3、安裝redis數據庫

首先去redis.io上去下載redis的穩定版源碼,進行編譯安裝

[root@gitlab ~]# tar zxvf redis-3.0.3.tar.gz
[root@gitlab ~]# cd redis-3.0.3
[root@gitlab ~]# make
[root@gitlab ~]# 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的啓動腳本,並啓動服務

[root@gitlab ~]# mv /etc/init.d/redis_6379 /etc/init.d/redis
[root@gitlab ~]# service redis start


4、安裝MySQL數據庫

MySQL的編譯安裝文檔很多,這裏爲了方便,我採用的是yum安裝過程

[root@gitlab ~]# yum install mysql mysql-server mysql-devel -y

爲了保證數據的安全,應該數據和二進制日誌會放在單獨的分區中

[root@gitlab ~]# vim /etc/my.cnf
datadir = /data/mysql

對MySQL進行初始化操作

[root@gitlab ~]# mysql_install_db --user=mysql --datadir=/data/mysql/

啓動MySQL數據庫

[root@gitlab ~]# service mysqld start

設置root的密碼和相關登錄信息

[root@gitlab ~]# 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
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
 ... 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
 - Dropping test database...
 ... Success!
 - 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!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

上面對於數據庫的部署已經OK了,下面就要爲gitlab創建一個庫用來存儲數據

[root@gitlab ~]# mysql -uroot -pgitlab
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)


5、提供ruby環境

下載ruby的源代碼包

[root@gitlab ~]# curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz | tar xz
[root@gitlab ~]# ./configure --prefix=/usr/local/
[root@gitlab ~]# make && make install

由於是在國內,安裝gem的相關包會特別的慢,建議換成淘寶的,此處爲淘寶點贊

[root@gitlab ~]# gem sources --remove https://rubygems.org/
[root@gitlab ~]# gem sources -a http://ruby.taobao.org/

安裝bundler,它是幫助管理ruby下的所有依賴的包,相當於yum的功能

[root@gitlab ~]# gem install bundler


添加一個git用戶,作爲git服務的授權用戶

[root@gitlab ~]# useradd git


6、安裝gitlab-shell來管理git倉庫

[root@gitlab ~]# su - git
[git@gitlab ~]$ git clone https://github.com/gitlabhq/gitlab-shell.git

將分支切換到2.6.3,有些文檔說1.4.0,我建議切到2.6.3,因爲版本低了,不支持用戶ssh形式的提交代碼到git服務中去

[git@gitlab ~]$ cd gitlab-shell/
[git@gitlab ~]$ git checkout v2.6.3

提供配置文件,修改gitlab的URL地址

[git@gitlab ~]$ cp config.yml.example config.yml
[git@gitlab ~]$ vim config.yml
gitlab_url: "http://192.168.190.138/"

進行安裝操作

[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


7、安裝gitlab系統

克隆gitlab的代碼到git家目錄

[git@gitlab ~]$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab
[git@gitlab ~]$ cd gitlab
[git@gitlab ~]$ git checkout 7-10-stable

配置項目,提供配置文件

[git@gitlab ~]$ cp config/gitlab.yml.example config/gitlab.yml

將localhost改爲本機ip或者是域名

[git@gitlab ~]$ sed -i 's/localhost/192.168.190.138/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.190.138:8080", :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"

修改其權限,保證其他用戶不能訪問

[git@gitlab ~]$ chmod o-rwx config/database.yml

安裝gem的相關包,進行gitlab的初始化操作

切回root操作下面步驟

[root@gitlab ~]# gem install charlock_holmes --version '0.6.9.4'

切回git用戶

[root@gitlab ~]# su - git
[git@gitlab ~]$ cd gitlab

修改Gemfile文件,將gem源改成淘寶的源

[git@gitlab ~]$ vim Gemfile
source "http://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

只要顯示上面的管理員賬號被創建,就ok了


爲gitlab提供啓動腳本

切回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


8、安裝nginx做反向代理

配置nginx官方源

[root@gitlab ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch
gpgcheck=0
enabled=1

安裝nginx包

[root@gitlab ~]# yum install nginx -y

爲gitlab配置反向代理的虛擬主機

[root@gitlab ~]# vim /etc/nginx/conf.d/gitlab.conf
server {
    listen 80;
    server_name 192.168.190.138;

    client_max_body_size 512M;

    access_log  /var/log/nginx/gitlab_access.log;
    error_log   /var/log/nginx/gitlab_error.log;

    location / {
          proxy_pass http://192.168.190.138:8080;
    }
}

啓動nginx服務

[root@gitlab ~]# service nginx start


爲提供服務的可靠性,建議將這些服務都設爲開機自啓動


在瀏覽器中訪問我們的IP地址

wKiom1W6RETgbm02AALtr8yxjyk581.jpg


    本篇博客的主要目的是爲了做個筆記,下次部署時,能更加方便快速一些,如果,大家有需要,也可以借鑑,博客中的錯誤之處,請幫忙之處,感謝!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章