4.系统用户
为GitLab创建一个名为git的用户:
sudo adduser --disabled-login --gecos 'GitLab' git
5.数据库
Gitlab官方建议我们用PostgreSQL数据库.如果喜欢用Mysql请前往Gitlab使用Mysql数据库的安装说明.
注意:Gitlab使用的部分扩展插件需要PostgreSQL版本至少为9.1.
# 安装数据库软件包
sudo apt-get install -y postgresql postgresql-client libpq-dev
# 使用系统用户postgres登录到PostgreSQL,目标数据库为template1
sudo -u postgres psql -d template1
# 为Gitlab创建一个用户
# 不要输入 'template1=#', 这是PostgreSQL的提示符
template1=# CREATE USER git CREATEDB;
# 创建Gitlab生产环境数据库并赋予git用户属主权限
template1=# CREATE DATABASE gitlabhq_production OWNER git;
# 退出数据库会话
template1=# \q
# 用git用户测试下是否能登录刚才创建的数据库
sudo -u git -H psql -d gitlabhq_production
# 退出数据库会话
gitlabhq_production> \q
6.Redis
sudo apt-get install redis-server
# 配置redis使用socket来监听
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
# 把'post'设置为0以禁止监听TCP端口
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf
# Enable Redis socket for default Debian / Ubuntu path
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf
# 给redis用户组的所有成员授权
echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf
# 创建存放socket的目录
mkdir /var/run/redis
sudo chown redis:redis /var/run/redis
sudo chmod 755 /var/run/redis
# Persist the directory which contains the socket, if applicable
if [ -d /etc/tmpfiles.d ]; then
echo 'd /var/run/redis 0755 redis redis 10d -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi
# 应用新的 redis.conf
sudo service redis-server restart
# 把git用户加入redis组
sudo usermod -aG redis git
7.Gitlab(重头戏来了)
# 我们将gitlab安装到git用户的HOME目录
cd /home/git
克隆Gitlab源码
# 克隆GIT@OSC上的Gitlab源码
sudo -u git -H git clone http://git.oschina.net/qiai365/gitlab-ce.git -b 8-1-stable gitlab
如果你想体验最新的非稳定版,你也可以克隆master
分支,但是不赞同在生产服务器上使用master分支.
配置Gitlab
# 进入Gitlab安装目录
cd /home/git/gitlab
# 创建Gitlab主配置文件'gitlab.yml'
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
# 更新配置文件
sudo -u git -H vim config/gitlab.yml
# 创建 secrets 配置文件
sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml
# 修改log/和tmp的权限
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/
# 修改 tmp/pids/ 和 tmp/sockets/ 的权限
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/
# 修改public/uploads/ 权限
sudo -u git -H mkdir -p public/uploads
sudo chmod -R u+rwX public/uploads
# 修改CI编译和存储目录的权限
sudo chmod -R u+rwX builds/
# 创建 Unicorn 配置文件
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
# 查询CPU核心数
nproc
# 如果你想搭建一个高负载的Gitlab实例,可启用集群模式.
# 修改'worker_processes'参数,至少要跟cpu核心数一样.
# 举例:为2G RAM的服务器修改workers数量为3
sudo -u git -H vim config/unicorn.rb
# 创建Rack attack 配置文件
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
# 配置 Redis 选项
sudo -u git -H cp config/resque.yml.example config/resque.yml
# 如果之前修改过redis socket的路径,在这个配置文件里面修改为当前的路径.
sudo -u git -H vim config/resque.yml
重要提示: 一定要按照你自己的情况修改gitlab.yml
和unicorn.rb
修改Gitlab 数据库设置
# 此命令仅针对PostgreSQl:
sudo -u git cp config/database.yml.postgresql config/database.yml
# 此命令仅针对MySQL:
sudo -u git cp config/database.yml.mysql config/database.yml
# 以下修改针对MySQL和远程PostgreSQL:
# 修改username/password.
# 生产环境只需要修改第一部分即可.
# 修改'secure password' 为你设置的密码
# 密码字段可以使用"双引号"
sudo -u git -H editor config/database.yml
# PostgreSQL MySQL都适用:
# 修改database.yml的权限,确保git用户可以读取该文件.
sudo -u git -H chmod o-rwx config/database.yml
安装Gems
这个步骤是很多新手头疼的问题,不过你只要严格按照本文关于Ruby环境的搭建来做.还是可以保证你顺利的安装下来的.
Note: 自bundler1.5.2起,你可以使用bundle install -jN
(N
就是cpu核心数)安装Gems,速度比之前要快大约60%.详细的内容可以点此处查看.不过首先要确保你的bundler版本>=1.5.2(运行bundle -v查看).
# PostgreSQL 环境
sudo -u git -H bundle install --deployment --without development test mysql aws kerberos
# MySQL 环境
sudo -u git -H bundle install --deployment --without development test postgres aws kerberos
Note:如果想使用kerberos
来验证用户,在--with-out
选项里面排除kerberos
安装GitLab Shell
GitLab Shell是专为GitLab开发的ssh访问和仓库管理的软件.
# 运行安装gitlab shell的任务 (根据自己的redis安装情况修改`REDIS_URL`),这里如果你事先没有clone gitlab-shell的仓库,就会自动clone官方的仓库进行安装:
sudo -u git -H bundle exec rake gitlab:shell:install[v2.6.6] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
# 默认情况下,gitlab-shell的配置是根据Gitlab的配置生产的.
# 你可以运行下面的命令查看和修改gitlab-shell的配置:
sudo -u git -H vim /home/git/gitlab-shell/config.yml
Note: Make sure your hostname can be resolved on the machine itself by either a proper DNS record or an additional line in /etc/hosts ("127.0.0.1 hostname"). This might be necessary for example if you set up gitlab behind a reverse proxy. If the hostname cannot be resolved, the final installation check will fail with "Check GitLab API access: FAILED. code: 401" and pushing commits will be rejected with "[remote rejected] master -> master (hook declined)".
安装gitlab-git-http-server
cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-git-http-server.git
cd gitlab-git-http-server
sudo -u git -H git checkout 0.3.0
sudo -u git -H make
初始化数据库,激活高级特性
# 进入Gitlab安装目录
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
# 输入 'yes' 来创建数据库表.
# 初始化完成后,会显示 'Administrator account created:',这里会输出默认账号和密码
Administrator account created:
login.........root
password......5iveL!fe
Note:你也可以设置环境变量GITLAB_ROOT_PASSWORD
,这样在初始数据库的时候就会使用你指定的密码,否则就是上面的默认密码.
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword
安全设置 secrets.yml
secrets.yml
文件为每个会话和安全变量存储密钥.把这个文件备份到别的地方,但是不要和数据库备份放在一块,否则你的数据库备份损坏会导致这个文件丢失.
安装Gitlab启动脚本
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
设置Gitlab为开机自启动
sudo update-rc.d gitlab defaults 21
配置Logrotate
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
检查应用状态
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
生成资源(Assets)
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
启动Gitlab实例
sudo service gitlab start
# or
sudo /etc/init.d/gitlab restart