使用gitlab+git+mysql+redis+nginx搭建自己的代码管理平台

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

使用gitlab搭建自己的代码管理平台

这里就不去介绍gitlab了,直接进入正题。

安装

有很多需要安装的内容可能已经安装过或者系统已经自带了,可以直接跳过前面几步,从创建用户开始,到后面缺少了哪一步再去安装。

安装git

sudo apt-get install git

git版本需要2.7+以上,使用

git --version

查看版本号

安装邮件服务器postfix

需要安装postfix来发送邮件

sudo apt-get install -y postfix

安装ruby

安装ruby

sudo apt-get install ruby

安装Bundler Gem:

sudo gem install bundler --no-ri --no-rdoc

安装GO

curl -O --progress https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz
echo '43afe0c5017e502630b1aea4d44b8a7f059bf60d7f29dfd58db454d4e4e0ae53  go1.5.3.linux-amd64.tar.gz' | shasum -a256 -c - && \
sudo tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz
sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
rm go1.5.3.linux-amd64.tar.gz

创建用户

创建一个用户git

sudo adduser --disabled-login --gecos 'GitLab' git

数据库mysql

如果还没有安装数据库 安装mysql

安装:

  sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

查看版本:

mysql --version

创建数据库用户。如果要用已有的用户或者root用户,下面的步骤就不需要了。 记得把自己的密码换上: CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';

# Login to MySQL
mysql -u root -p

# Type the MySQL root password

# Create a user for GitLab
# do not type the 'mysql>', this is part of the prompt
# change $password in the command below to a real password you pick
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';

# Ensure you can use the InnoDB engine which is necessary to support long indexes
# If this fails, check your MySQL config files (e.g. `/etc/mysql/*.cnf`, `/etc/mysql/conf.d/*`) for the setting "innodb = off"
mysql> SET storage_engine=INNODB;

# Create the GitLab production database
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

# Grant the GitLab user necessary permissions on the database
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER, LOCK TABLES ON `gitlabhq_production`.* TO 'git'@'localhost';

# Quit the database session
mysql> \q

# Try connecting to the new database with the new user
sudo -u git -H mysql -u git -p -D gitlabhq_production

# Type the password you replaced $password with earlier

# You should now see a 'mysql>' prompt

# Quit the database session
mysql> \q

# You are done installing the database and can go back to the rest of the installation.

安装Redis

redis的版本必须大于2.8

sudo apt-get install redis-server

安装gitlab

我们之前创建了一个用户git,这个git是不可以登录的。

cd /home/git

clone资源

  sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-5-stable gitlab

我这里直接使用的8.5,你也可以在clone完了之后在切换分支

  sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git gitlab
  cd gitlab
  git checkout 8-5-stable

如果想用其它的版本,比如8.10,就

  git checkout 8-10-stable

配置gitlab

# Go to GitLab installation folder
cd /home/git/gitlab

# Copy the example GitLab config
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

# Update GitLab config file, follow the directions at top of file
sudo -u git -H vi config/gitlab.yml

# Copy the example secrets file
sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml

# Make sure GitLab can write to the log/ and tmp/ directories
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/

# Make sure GitLab can write to the tmp/pids/ and tmp/sockets/ directories
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

# Create the public/uploads/ directory
sudo -u git -H mkdir public/uploads/

# Make sure only the GitLab user has access to the public/uploads/ directory
# now that files in public/uploads are served by gitlab-workhorse
sudo chmod 0700 public/uploads

# Change the permissions of the directory where CI build traces are stored
sudo chmod -R u+rwX builds/

# Change the permissions of the directory where CI artifacts are stored
sudo chmod -R u+rwX shared/artifacts/

# Copy the example Unicorn config
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

# Find number of cores
nproc

# Enable cluster mode if you expect to have a high load instance
# Set the number of workers to at least the number of cores
# Ex. change amount of workers to 3 for 2GB RAM server
sudo -u git -H vi config/unicorn.rb

# Copy the example Rack attack config
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

# Configure Git global settings for git user, used when editing via web editor
sudo -u git -H git config --global core.autocrlf input

# Configure Redis connection settings
sudo -u git -H cp config/resque.yml.example config/resque.yml

# Change the Redis socket path if you are not using the default Debian / Ubuntu configuration
sudo -u git -H vi config/resque.yml

配置数据库

# MySQL only:
sudo -u git 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
sudo -u git -H vi config/database.yml

# PostgreSQL and MySQL:
# Make config/database.yml readable to git only
sudo -u git -H chmod o-rwx config/database.yml

修改Gemfile,安装依赖

  1. 修改第一行为:

source "https://ruby.taobao.org"

  1. 找到

gem "mysql2", '~> 0.3.16', group: :mysql

gem "pg", '~> 0.18.2', group: :postgres

修改为:

gem "mysql2"#, '~> 0.3.16', group: :mysql
#gem "pg", '~> 0.18.2', group: :postgres
  1. 执行
sudo -u git -H bundle install --no-deployment
sudo -u git -H bundle install --deployment --without development test mysql aws kerberos

安装GitLab Shell

  1. 你可以用下面的语句安装同时配置gitlab-shell
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
sudo -u git -H vi /home/git/gitlab-shell/config.yml
  1. 或者自己下载gitlab-shell然后配置,具体看这里:https://github.com/gitlabhq/gitlab-shell

安装gitlab-workhorse

8.2之前这个东西叫gitlab-git-http-server

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
sudo -u git -H git checkout 0.6.4
sudo -u git -H make

初始化数据库

初始化完成后会有初始用户名和密码提示,记住它。

cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production

安装启动脚本

  1. 启动脚本
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
  1. 如果需要开机启动
sudo update-rc.d gitlab defaults 21

验证安装

  1. 验证环境是否ok安装
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
  1. 启动gitlab
sudo /etc/init.d/gitlab restart
  1. 验证gitlab是否启动成功
sudo -u git -H ./bin/check

如果有错误的地方,会给出具体的错误信息和解决方法,根据具体情况解决就可以。

nginx

  1. 安装:

我这里就直接安装了,其它系统大家按情况安装。

sudo apt-get install -y nginx
  1. 配置
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
sudo vi /etc/nginx/sites-available/gitlab
  1. 测试配置
sudo nginx -t
  1. 重启nginx
sudo service nginx restart
  1. 最后在测试下安装是否成功了
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

到这里就搭建好了,更多内容请大家参考:https://github.com/gitlabhq/gitlabhq/

转载于:https://my.oschina.net/lengchuan/blog/726124

你可能感兴趣的:(使用gitlab+git+mysql+redis+nginx搭建自己的代码管理平台)