官網環境要求html
參見:https://github.com/gitlabhq/gitlabhqnode
GitLab is a Ruby on Rails application that runs on the following software:python
For more information please see the architecture documentation.mysql
##########################################nginx
關閉防火牆,關閉SELinuxgit
1、安裝採用環境github
2、系統添加EPEL、PUIAS源web
1.添加EPEL源redis
wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://www.fedoraproject.org/static/0608B895.txt --no-check-certificate
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
rpm -qa gpg*
#顯示
gpg-pubkey-41a40948-4ce19266
2.添加PUIAS源spring
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 --no-check-certificate 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 rpm -qa gpg* #顯示gpg-pubkey-41a40948-4ce19266
3.查看源列表,驗證
輸入以下命令:
yum repolist
結果以下:
repo id repo name status PUIAS_6_computational PUIAS computational Base 6 - x86_64 2,678 base CentOS-6 - Base 6,575 epel Extra Packages for Enterprise Linux 6 - x86_64 11,775 extras CentOS-6 - Extras 3510 updates CentOS-6 - Updates 254 repolist: 21,327
If you can't see them listed, use the folowing command (from yum-utils
package) to enable them:
yum-config-manager --enable epel --enable PUIAS_6_computational
3、Install the required tools for GitLab
1.利用yum安裝依賴包
yum -y update
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 python-devel logwatch logrotate perl-Time-HiRes libcom_err-devel.x86_64vim-enhanced
2.源碼安裝Git
參見:http://www.cnblogs.com/jim-hwg/p/4748008.html
3.源碼安裝Ruby
參見:http://www.cnblogs.com/jim-hwg/p/4746977.html
4.源碼安裝MySQL
參見:http://www.cnblogs.com/jim-hwg/p/4747165.html
4、建立用戶
1.建立一個Gitlab系統用戶git:
adduser --system --shell /bin/bash --comment 'GitLab' --create-home --home-dir /home/git/ git
爲了方便添加git用戶擁有root權限 [sudoers文件默認沒有寫權限須要強制保存:wq!]
使用root用戶執行下述命令
vim /etc/sudoers
最後添加
git ALL=(ALL) NOPASSWORD: ALL
2.建立一個Gitlab數據庫用戶並賦權
登錄MySQL,輸入密碼
mysql -u root -p
建立Gitlab數據庫用戶git:
CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';
#替換$password設定本身的密碼 CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`; GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'git'@'localhost'; quit
5、配置服務
1.配置MySQL
配置MySQL max_allowed_packet的大小,避免POST太大的內容致使出現500錯誤,例如GitLab 發出MergeRequest的時候返回500錯誤。
vim /etc/my.cnf
#在mysqld中添加max_allowed_packet,調整值,加大爲一個合適的數字便可。
[mysqld]
max_allowed_packet=512M
重啓mysql服務
service mysqld restart
2.配置redis
Make sure redis is started on boot:
chkconfig redis on
Configure redis to use sockets:
cp /etc/redis.conf /etc/redis.conf.orig
Disable Redis listening on TCP by setting 'port' to 0:
sed 's/^port .*/port 0/' /etc/redis.conf.orig | sudo tee /etc/redis.conf
Enable Redis socket for default CentOS path:
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis.conf echo -e 'unixsocketperm 0775' | sudo tee -a /etc/redis.conf
Activate the changes to redis.conf:
service redis restart
Add git to the redis group:
usermod -aG redis git
6、安裝Gitlab
切換到git用戶
su - git
設置不驗證SSL
vim /home/git/.bash_profile export GIT_SSL_NO_VERIFY=1
1.克隆Gitlab
git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-12-stable gitlab
或克隆中文版Gitlab
git clone https://gitlab.com/larryli/gitlab.git -b 7-12-zh gitlab
2.配置Gitlab
cd gitlab cp config/gitlab.yml.example config/gitlab.yml
配置config/gitlab.yml,配置Gitlab服務的端口,IP
vim config/gitlab.yml host: 10.149.153.81 port: 8049 email_from: gitlab@example.com email_enabled: false default_theme: 1
mkdir /home/git/gitlab-satellites chmod 750 /home/git/gitlab-satellites cp config/unicorn.rb.example config/unicorn.rb
配置config/unicorn.rb,配置Ruby提供的服務端口,IP
# Find number of cores #優化系統參數/etc/security/limits.conf # Enable cluster mode if you expect to have a high load instance # Ex. change amount of workers to 3 for 2GB RAM server # Set the number of workers to at least the number of cores vim config/unicorn.rb worker_processes 3 # Set the number of workers to at least the number of core
listen "127.0.0.1:8081", :tcp_nopush => true #注意端口,避免衝突
cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
git config --global user.name "GitLab"
git config --global user.email "gitlab@example.com"
git config --global core.autocrlf input
cp config/resque.yml.example config/resque.yml
配置config/resque.yml
#若是不使用默認的端口,則須要配置
3. Configure GitLab DB settings
cp config/database.yml.mysql config/database.yml
# MySQL and remote PostgreSQL only:
# Update username/password in config/database.yml.
# You only need to adapt the production settings (first part).
# If you followed the database guide then please do as follows:
# Change 'secure password' with the value you have given to $password
# You can keep the double quotes around the password
vim config/database.yml
修改成正確的用戶名和密碼
分別修改git用戶和root用戶
chmod o-rwx config/database.yml
4.Install Gems
Note: As of bundler 1.5.2, you can invoke bundle install -jN
(where N
the number of your processor cores) and enjoy the parallel gems installation with measurable
difference in completion time (~60% faster). Check the number of your cores with nproc
.
For more information check this post.
First make sure you have bundler >= 1.5.2 (run bundle -v
) as it addresses some issues
that were fixed in 1.5.2.
cd /home/git/gitlab # Or for MySQL (note, the option says "without ... postgres") 修改成淘寶的ruby源 vim Gemfile 修改成 source 'https://ruby.taobao.org/' bundle install --deployment --without development test postgres aws 這一步的時間會等好久
5.Install GitLab shell
GitLab Shell is an SSH access and repository management software developed specially for GitLab.
cd /home/git/gitlab
bundle exec rake gitlab:shell:install[v2.6.2] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
報錯:ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
處理:切換到root用戶
yum -y install nodejs
配置/home/git/gitlab/Gemfile
vim /home/git/gitlab/Gemfile #末尾添加 gem 'execjs' gem 'therubyracer'
cd /home/git/gitlab
bundle install --no-deployment
驗證:
bundle show execjs
bundle show therubyracer
切回git用戶,再次執行
cd /home/git/gitlab
bundle exec rake gitlab:shell:install[v2.6.2] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
配置/home/git/gitlab-shell/config.yml,配置gitlab-shell要調用的API接口
# By default, the gitlab-shell config is generated from your main GitLab config. # You can review (and modify) the gitlab-shell config as follows: vim /home/git/gitlab-shell/config.yml # Ensure the correct SELinux contexts are set # Read http://wiki.centos.org/HowTos/Network/SecuringSSH restorecon -Rv /home/git/.ssh
6.Initialize Database and Activate Advanced Features
cd /home/git/gitlab/
bundle exec rake gitlab:setup RAILS_ENV=production
Type yes to create the database.
When done you see Administrator account created:.
Note: You can set the Administrator password by supplying it in environmental variable GITLAB_ROOT_PASSWORD
, eg.:
若是要修改gitlab管理員的密碼,則執行這一句,不然執行上一句
bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=newpassword
7.Install Init Script
Download the init script (will be /etc/init.d/gitlab):
切換到root用戶
wget -O /etc/init.d/gitlab https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/init/sysvinit/centos/gitlab-unicorn --no-check-certificate chmod +x /etc/init.d/gitlab chkconfig --add gitlab chkconfig gitlab on
8.Set up logrotate
切換到root用戶
cd /home/git/gitlab cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
9.Check Application Status
Check if GitLab and its environment are configured correctly:
切回git用戶
cd /home/git/gitlab bundle exec rake gitlab:env:info RAILS_ENV=production #Compile assets bundle exec rake assets:precompile RAILS_ENV=production
切回root,執行
service gitlab start
7、Configure the web server
Use either Nginx or Apache, not both. Official installation guide recommends nginx.
You will need a new version of nginx otherwise you might encounter an issue like this.
To do so, follow the instructions provided by the nginx wiki and then install nginx with:
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum update yum -y install nginx chkconfig nginx on 使用SSl wget -O /etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab-ssl --no-check-certificate 不使用SSL wget -O /etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab --no-check-certificate
最後加上--no-check-certificate不檢查證書,這裏不使用SSL
Edit /etc/nginx/conf.d/gitlab.conf
and replace git.example.com
with your FQDN. Make sure to read the comments in order to properly set up SSL.
vim /etc/nginx/conf.d/gitlab.conf 去掉listen後面的default_server,修改成正確的端口號 去掉 listen [::]: 修改server_name 爲本機的IP地址 修改client_max_body_size 256m; 不然當推送較多數據到 gitlab 上時,會因爲數據過大,而出現錯誤 fatal: The remote end hung up unexpectedly fatal: The remote end hung up unexpectedly error: RPC failed; result=22, HTTP code = 413
Add nginx
user to git
group:
usermod -a -G git nginx
Finally start nginx with:
service nginx start
Test Configuration
Validate your gitlab
or gitlab-ssl
Nginx config file with the following command:
nginx -t
8、Double-check Application Status
To make sure you didn't miss anything run a more thorough check with:
su - git cd /home/git/gitlab bundle exec rake gitlab:check RAILS_ENV=production
9、Initial Login
Visit YOUR_SERVER in your web browser for your first GitLab login.
The setup has created an admin account for you. You can use it to log in:
root
5iveL!fe
#####################以上即完成了Gitlab的搭建################################
10、後期維護
1.Upgrade GitLab Shell
GitLab Shell might be outdated, running the commands below ensures you're using a compatible version:
su - git cd /home/git/gitlab-shell git fetch git checkout v`cat /home/git/gitlab/GITLAB_SHELL_VERSION`
2.Upgrade GitLab
One line upgrade command
You've read through the entire guide and probably already did all the steps one by one.
Here is a one line command with step 1 to 5 for the next time you upgrade:
切換到git用戶
cd /home/git/gitlab; \ sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production; \ sudo service gitlab stop; \ if [ -f bin/upgrade.rb ]; then sudo -u git -H ruby bin/upgrade.rb -y; else sudo -u git -H ruby script/upgrade.rb -y; fi; \ cd /home/git/gitlab-shell; \ sudo -u git -H git fetch; \ sudo -u git -H git checkout v`cat /home/git/gitlab/GITLAB_SHELL_VERSION`; \ cd /home/git/gitlab; \ exit; \ sudo service gitlab start; \ sudo service nginx restart; sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
3.Upgrade /etc/init.d/gitlab
wget https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/init/sysvinit/centos/gitlab-unicorn mv gitlab-unicorn gitlab cp -f gitlab /etc/init.d/gitlab rm gitlab
複製完後,要刪除/etc/init.d/gitlab.swap文件
4.Gitlab源代碼更新
cd /home/git/gitlab/ git fetch origin git merge origin/7-5-zh #重啓 gitlab service gitlab restart
11、Gitlab 備份
官網的備份說明
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/raketasks/backup_restore.md
查看備份設置
vim /home/git/gitlab/config/gitlab.yml
檢查Backup Settings設置項
默認狀況下,備份文件是存放在/home/git/gitlab/tmp/backups/
執行備份
sudo service gitlab stop # 先中止Gitlab,能夠不暫停 cd /home/git/gitlab/ sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production
執行完成後,會在/home/git/gitlab/tmp/backups/目錄下建立一個備份俄文件,以時間戳_gitlab_backup命名如 1417040627_gitlab_backup.tar
從新啓動
sudo service gitlab start sudo service nginx restart
還原
須要給其餘用戶配置讀寫執行的權限
chmod o+wrx /home/git/.ssh/authorized_keys.lock
不然會出現以下錯誤,是因爲沒有權限
/home/git/gitlab-shell/lib/gitlab_keys.rb:101:in `initialize': Permission denied @ rb_sysopen - /home/git/.ssh/authorized_keys.lock (Errno::EACCES)
須要使用 git 用戶來執行,不然會沒有權限操做 git 目錄下的文件,timestamp_of_backup
爲時間戳如 1417040627
sudo service gitlab stop cd /home/git/gitlab/ sudo -u git -H bundle exec rake gitlab:backup:restore BACKUP=timestamp_of_backup RAILS_ENV=production sudo service gitlab start sudo service nginx restart sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
設置自動備份
sudo service gitlab stop; cd /home/git/gitlab; sudo -u git -H editor config/gitlab.yml; # Enable keep_time in the backup section to automatically delete old backups
keep_time參數默認是604800(單位是秒),所以會保留最近7天內的備份
sudo -u git crontab -e # Edit the crontab for the git user
將以下內容添加到文件末尾
# Create a full backup of the GitLab repositories and SQL database every day at 2am 0 2 * * * cd /home/git/gitlab && PATH=/usr/local/bin:/usr/bin:/bin bundle exec rake gitlab:backup:create RAILS_ENV=production CRON=1
天天凌晨2點自動備份
The CRON=1 environment setting tells the backup script to suppress all progress output if there are no errors. This is recommended to reduce cron spam.
從新啓動
sudo service gitlab start; sudo service nginx restart; sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production;