本文章適用於須要自建gitlab代碼管理系統,僅供參考javascript
安裝軟件包及版本要求html
1. Ubuntu/Debian/CentOS/RHEL 2. ruby 2.4+ 3. git 2.7.2+ 4. go 1.0.0+ 5. redis 3.2+ 6. node 8.0+ 7. MySQL(5.7+) or PostgreSQ (9.4+) 8. gitlab ce 11-3-stable 9. nignx (1.12.2)
添加EPEL源前端
# wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7 # rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
安裝基礎軟件以及相關依賴包java
# rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm # yum groupinstall "Development tools" # yum install autoconf automake bison expat-devel gcc-c++ zlib-devel mysql-devel \ gdbm-devel re2-devel readline-devel ncurses-devel curl-devel libxml2-devel \ libxslt-devel openssl-devel libicu-devel rsync python-docutils cmake -y
卸載原有的git包node
# sudo yum remove git
安裝git所須要的依賴包python
# sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
下載穩定版本的git源碼包並安裝mysql
# cd /root # curl --remote-name --location --progress https://www.kernel.org/pub/software/scm/git/git-2.18.0.tar.gz
解壓並安裝gitlinux
# cd /root # tar xvf git-2.18.0.tar.gz -C /usr/local/src && cd /usr/local/src/git-2.18.0/ # ./configure prefix=/usr/local/git all # make && make install
設置git命令路徑優化nginx
# vim /etc/profile.d/git.sh 內容以下: export GIT_HOME=/usr/local/git export PATH=$GIT_HOME/bin:$PATH # source /etc/profile.d/git.sh
下載最新穩定版的ruby二進制源碼包c++
# cd /root # curl --remote-name --progress https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.4.tar.gz
編譯安裝ruby
# cd /root && tar zxvf ruby-2.4.4.tar.gz -C /usr/local/src # cd /usr/local/src/ruby-2.4.4/ # ./configure --prefix=/data/apps/ruby --disable-install-rdoc # make && make install
設置ruby命令路徑優化
# sudo vim /etc/profile.d/ruby.sh 內容以下: export RUBY_HOME=/data/apps/ruby export PATH=$RUBY_HOME/bin:$PATH # source /etc/profile.d/ruby.sh
安裝bundle命令
# gem install bundler --no-ri --no-rdoc # ln -sf /data/apps/ruby/bin/* /usr/local/bin/
卸載原有的go命令包
# sudo rm -rf /usr/local/go
下載穩定版的go二進制源碼包
# cd /root # curl --remote-name --progress https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz # sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz # sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/ # rm go1.10.3.linux-amd64.tar.gz
安裝node.js應用 (這裏版本爲8.0以上)
# cd /root # curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - # sudo yum install -y nodejs 安裝完node後安裝yarn # cd /root # sudo yum install gcc-c++ make -y # curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo # sudo yum install yarn -y
yum安裝redis服務
# yum install redis -y
配置redis配置文件
# cat > /etc/redis.conf <<EOF daemonize yes bind localhost protected-mode no port 6379 tcp-backlog 65535 timeout 0 tcp-keepalive 300 pidfile /var/run/redis/redis.pid unixsocket /var/run/redis/redis.sock unixsocketperm 770 loglevel notice logfile "/var/log/redis/redis.log" databases 32 save 900 1 save 300 10 save 60 10000 dbfilename dump.rdb dir /data/apps/redis/db EOF
對redis相關目錄受權
# mkdir /var/run/redis # mkdir /data/apps/redis/db -p # chown -R redis:redis /data/apps/redis # chown redis:redis /var/run/redis # chmod 755 /var/run/redis
這裏使用的數據庫爲MySQL 5.7.0以上版本,數據庫安裝詳情請參考https://docs.gitlab.com/ee/install/database_mysql.html
有關nginx安裝這裏爲yum安裝,固然也能夠根據本身的選擇源碼安裝
# yum install nignx -y
10.1 首先建立運行gitlab服務的git用戶
# groupadd git # useradd -g git -c "Gitlab Service" -d /data/apps/git git # ln -sf /data/apps/git/ /home/git # sudo usermod -aG git nignx # sudo usermod -aG redis git
10.2 下載gitlab源碼(這裏版本爲11-3-stable)
# cd /home/git # sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 11-3-stable gitlab
10.3 修改gitlab相關配置文件並作相應的修改
拷貝gitlab模板並修改相關配置
# cd /home/git/gitlab # sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml # sudo -u git -H vim config/gitlab.yml 找到以下行內容 ## GitLab settings gitlab: host: code.mo9.com 修改成你須要訪問的域名 port: 80 # Set to 443 若是須要https訪問,開啓HTTPS便可; ## Git settings git: bin_path: /usr/local/git/bin/git 修改成git的命令執行路徑 time_zone: 'UTC' 修改成「Asia/Shanghai」
拷貝unicorn模板並編輯配置,修改以下
# sudo -u git -H vim config/unicorn.rb 找到以下內容 worker_processes 3 (值修改成服務器的cpu數+1) listen "127.0.0.1:8080", :tcp_nopush => true (該值爲主機的ip)
拷貝database模板並編輯配置
# sudo -u git cp config/database.yml.mysql config/database.yml # sudo -u git -H chmod o-rwx config/database.yml # sudo -u git -H vim config/database.yml 修改內容以下(此處僅爲模板) # # PRODUCTION # production: adapter: mysql2 encoding: utf8 collation: utf8_general_ci reconnect: false database: gitlab pool: 10 username: root password: "1qaz@WSX" host: rm-bp1lsjz89883fih35.mysql.rds.aliyuncs.com
拷貝resque模板並編輯配置
# sudo -u git -H cp config/resque.yml.example config/resque.yml # sudo -u git -H vim config/resque.yml 修改內容以下(此處僅爲參考模板,若是須要配置redis集羣請詳見官網配置說明) # If you change this file in a Merge Request, please also create # a Merge Request on https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests # production: # Redis (single instance) url: unix:/var/run/redis/redis.sock ##
拷貝secrets文件模板並受權(注意此文件內容自動生成, 在遷移gitlab須要用到該文件)
# sudo -u git -H cp config/secrets.yml.example config/secrets.yml # sudo -u git -H chmod 0600 config/secrets.yml
對gitlab相關目錄受權
# sudo chown -R git log/ # sudo chown -R git tmp/ # sudo chmod -R u+rwX,go-w log/ # sudo chmod -R u+rwX tmp/ # sudo chmod -R u+rwX tmp/pids/ # sudo chmod -R u+rwX tmp/sockets/ # sudo -u git -H mkdir public/uploads/ # sudo chmod 0700 public/uploads # sudo chmod -R u+rwX builds/ # sudo chmod -R u+rwX shared/artifacts/ # sudo chmod -R ug+rwX shared/pages/
拷貝rack_attack.rb文件模板
# sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
設置git初始參數
# sudo -u git -H git config --global core.autocrlf input # sudo -u git -H git config --global gc.auto 0 # sudo -u git -H git config --global repack.writeBitmaps true # sudo -u git -H git config --global receive.advertisePushOptions true
10.4 安裝gitlab所須要的gems包
自bundler1.5.2起,你能夠使用bundle install -jN(N就是cpu核心數)安裝Gems,速度比以前要快大約60%.詳細的內容能夠查看官文.不過首先要確保你的bundler版本>=1.5.2(運行bundle -v查看)
因爲默認的ruby源地址訪問會存在網絡訪問慢的問題,因此須要更換爲國內的ruby源
# cd /home/git/gitlab # gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/ # gem sources -l https://gems.ruby-china.com (確保只有 https://gems.ruby-china.com/) 使用git用戶修改Gemfile 和 Gemfile.lock 更改 https://rubygems.org/ 爲: https://gems.ruby-china.com/
安裝gems包
For PostgreSQL (note, the option says "without ... mysql") # sudo -u git -H bundle install --deployment --without development test mysql aws kerberos Or if you use MySQL (note, the option says "without ... postgres") # sudo -u git -H bundle install --deployment --without development test postgres aws kerberos 上述命令執行完後,結果以下: Bundle complete! 226 Gemfile dependencies, 321 gems now installed. Gems in the groups development, test, postgres, aws and kerberos were not installed. Bundled gems are installed into `./vendor/bundle`
注意:在安裝gems過程當中,可能會遇到如下錯誤:
若是提示下面的錯誤: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> error occurred while installing charlock_holmes (0.7.3), and Bundler cannot continue. Make sure that `gem install charlock_holmes -v ‘0.7.3’` succeeds before bundling. brew install icu4c or apt-get install libicu-dev 解決辦法: # yum install libicu.x86_64 libicu-devel.x86_64 An error occurred while installing rugged (0.25.1.1), and Bundler cannot continue. Make sure that `gem install rugged -v '0.25.1.1'` succeeds before bundling. 解決辦法: # yum install cmake # gem install rugged -v '0.25.1.1' ERROR: Could not find a valid gem 'charlock_holmes' (= 0.6.9.4), here is why: Unable to download data from https://rubygems.org/ - Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org/quick/Marshal.4.8/charlock_holmes-0.6.9.4.gemspec.rz) ERROR: Possible alternatives: charlock_holmes 解決辦法: #yum install libicu-devel An error occurred while installing mysql2 (0.4.20), and Bundler cannot continue. Make sure that `gem install mysql2 -v ‘0.4.20’` succeeds before bundling. #解決辦法 # yum install mysql-devel.x86_64 # gem install mysql2 -v '0.4.20 An error occurred while installing re2 (1.0.0), and Bundler cannot continue. Make sure that `gem install re2 -v '1.0.0'` succeeds before bundling. #解決辦法 # yum install -y re2-devel An error occurred while installing pg (0.18.4), and Bundler cannot continue. Make sure that `gem install pg -v ‘0.18.4’` succeeds before bundling. #解決辦法 # gem install pg -v '0.18.4' # yum install postgresql-devel.x86_64 An error occurred while installing sqlite3 (1.5.14), and Bundler cannot continue. Make sure that `gem install sqlite3 -v ‘1.5.14’` succeeds before bundling. # 解決辦法 # yum install sqlite-devel.x86_64 # gem install sqlite3 -v '1.5.14' Bundler::GemRequireError: There was an error while trying to load the gem ‘coffee-rails’. Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. #解決辦法:由於execjs須要javascript的支持 #參考這裏:NodeJs的安裝
10.5 安裝gitlab-shell
在實際安裝gitlab-shell過程當中會存在克隆gitlab-shell倉庫網絡慢問題,因此咱們必須修改gitlab-shell倉庫源爲國內源:https://git.oschina.net/qiai365/gitlab-shell.git 操做以下:
# cd /home/git/gitlab # sudo -u git -H vim /home/git/gitlab/lib/tasks/gitlab/shell.rake 替換掉gitlab-shell官方倉庫源便可
10.7 執行安裝gitlab-shell腳本
# cd /home/git/gitlab # sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production SKIP_STORAGE_VALIDATION=true
編輯gitlab-shell配置文件,並根據實際狀況修改
# sudo -u git -H vim /home/git/gitlab-shell/config.yml 內容以下: --- user: git gitlab_url: http://code.mo9.com/ (修改成gitlab對外訪問的域名FQDN) http_settings: self_signed_cert: false (若是須要開啓https訪問,修改成ture,並使用ca_file指定) auth_file: "/data/apps/git/.ssh/authorized_keys" redis: bin: "/usr/bin/redis-cli" (該值修改成redis命令路徑) namespace: resque:gitlab socket: "/var/run/redis/redis.sock" (該值修改成redis的sock路徑) log_level: INFO audit_usernames: false
10.6 安裝gitlab-workhorse
# cd /home/git/gitlab # sudo -u git -H bundle exec rake "gitlab:workhorse:install[/home/git/gitlab-workhorse]" RAILS_ENV=production
10.7 安裝gitlab-pages
# cd /home/git # sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-pages.git # cd gitlab-pages # sudo -u git -H git checkout v$(</home/git/gitlab/GITLAB_PAGES_VERSION) # sudo -u git -H make
10.8 安裝gitaly
# cd /home/git/gitlab # sudo -u git -H bundle exec rake "gitlab:gitaly:install[/home/git/gitaly,/home/git/git-data]" RAILS_ENV=production # sudo chmod 0700 /home/git/gitlab/tmp/sockets/private # sudo chown git /home/git/gitlab/tmp/sockets/private
10.9 初始化數據庫
# cd /home/git/gitlab # sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=gitlab.com GITLAB_ROOT_EMAIL=admin@mo9.com 當看到如下內容,表示已經安裝完成 Administrator account created: login: root password: your_passwd == Seed from /home/git/gitlab/db/fixtures/production/010_settings.rb
10.10 打包前端靜態資源文件
編譯gettext文件
# cd /home/git/gitlab # sudo -u git -H bundle exec rake gettext:compile RAILS_ENV=production
生成gitlab前端靜態assets文件
# cd /home/git/gitlab # sudo -u git -H yarn install --production --pure-lockfile # sudo -u git -H bundle exec rake gitlab:assets:compile RAILS_ENV=production NODE_ENV=production
10.11 初始化gilab服務腳本以及日誌切割腳本
# cd /home/git/gitlab # sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab # sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab # sudo service gitlab start
# cd /home/git/gitlab # sudo cp lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf # nginx -t # service nginx start
若是須要開啓https訪問,須要更改三個地方的配置文件:
/home/git/gitlab/config/gitlab.yml 開啓https,並修改訪問域名以及訪問端口; /home/git/gitlab-shell//config.yml 開啓https,並修改訪問域名添加ssl證書; /etc/nginx/conf.d/gitlab.conf 開啓https,並修改訪問域名添加ssl證書;
檢查gitlab組件信息
# sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production 當出現以下信息表示安裝無誤: System information System: CentOS 7.4.1708 Current User: git Using RVM: no Ruby Version: 2.4.4p296 Gem Version: 2.6.14.1 Bundler Version:1.16.6 Rake Version: 12.3.1 Redis Version: 3.2.12 Git Version: 2.18.0 Sidekiq Version:5.2.1 Go Version: go1.10.3 linux/amd64 GitLab information Version: 11.4.0-pre Revision: 0d84dd22872 Directory: /data/apps/git/gitlab DB Adapter: mysql2 URL: http://code.mo9.com HTTP Clone URL: http://code.mo9.com/some-group/some-project.git SSH Clone URL: git@code.mo9.com:some-group/some-project.git Using LDAP: no Using Omniauth: yes Omniauth Providers: GitLab Shell Version: 8.3.3 Repository storage paths: - default: /home/git/repositories Hooks: /home/git/gitlab-shell/hooks Git: /usr/local/git/bin/git
檢查gitlab應用狀態
# sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production 當出現以下信息時,說明整個gitlab部署成功: Checking GitLab Shell ... GitLab Shell version >= 8.3.3 ? ... OK (8.3.3) Repo base directory exists? default... yes Repo storage directories are symlinks? default... no Repo paths owned by git:root, or git:git? default... yes Repo paths access is drwxrws---? default... yes hooks directories in repos are links: ... Administrator / test ... ok Running /home/git/gitlab-shell/bin/check Check GitLab API access: OK Redis available via internal API: OK Access to /data/apps/git/.ssh/authorized_keys: OK gitlab-shell self-check successful Checking GitLab Shell ... Finished Checking Sidekiq ... Running? ... yes Number of Sidekiq processes ... 1 Checking Sidekiq ... Finished Reply by email is disabled in config/gitlab.yml Checking LDAP ... LDAP is disabled in config/gitlab.yml Checking LDAP ... Finished Checking GitLab ... Git configured correctly? ... yes Database config exists? ... yes All migrations up? ... yes Database contains orphaned GroupMembers? ... no GitLab config exists? ... yes GitLab config up to date? ... yes Log directory writable? ... yes Tmp directory writable? ... yes Uploads directory exists? ... yes Uploads directory has correct permissions? ... yes Uploads directory tmp has correct permissions? ... skipped (no tmp uploads folder yet) Init script exists? ... yes Init script up-to-date? ... yes Projects have namespace: ... Administrator / test ... yes Redis version >= 2.8.0? ... yes Ruby version >= 2.3.5 ? ... yes (2.4.4) Git version >= 2.9.5 ? ... yes (2.18.0) Git user has default SSH configuration? ... yes Active users: ... 2 Checking GitLab ... Finished
至此整個gitlab系統搭建完成,固然若是須要基於https訪問能夠參考官方文檔: