如何在Ubuntu 16.04上使用Nginx(SSL)安装GitLab

时间:2020-03-05 15:27:56  来源:igfitidea点击:

GitLab是一个类似于Github的基于Web的Git存储库管理器。
这是一个由Ruby和Rails支持的开源git仓库管理工具。
它提供了一个.deb软件包,其中包含GitLab社区版及其所有依赖项,包括Ruby,PostgreSQL,Redis,Nginx,Unicorn和其他已编译的gem。

它提供了三种不同的版本。
一个是GitLab的托管版本,我们可以其中免费注册,并且可以根据需要创建任意数量的私有和公共存储库。
这是一个很大的优势。
第二个是“ GitLab社区版”。
我们可以在自己的服务器上操作社区版,这不会花钱。
它提供了自动测试和部署代码的可能性,最后但并非最不重要的是“ GitLab企业版”。
它基于社区版,并添加了一些另外的功能,主要针对拥有100个以上用户的组织。

在本文中,我将解释如何在Ubuntu服务器上使用Nginx设置GitLab。
首先,我们需要确保服务器符合基本系统要求。

准备工作

几乎所有的Unix操作系统都支持它。
请在下面查看此安装的最低系统要求:

  • 最低要求Ruby(MRI)2.1.
  • 具有CPU 2内核和2GB内存的最低硬件功能
  • 三位独角兽工作者(1+ CPU内核数)

让我们一步一步地完成安装步骤。

1.更新APT存储库软件包

首先,我们需要确保服务器正在运行最新的软件包,并且它们是最新的。

root@theitroad:~# apt-get update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
Hit:2 http://nyc2.mirrors.digitalocean.com/ubuntu xenial InRelease
Get:3 http://nyc2.mirrors.digitalocean.com/ubuntu xenial-updates InRelease [95.7 kB]
Get:4 http://nyc2.mirrors.digitalocean.com/ubuntu xenial-backports InRelease [92.2 kB]
Get:5 http://nyc2.mirrors.digitalocean.com/ubuntu xenial-updates/universe amd64 Packages [357 kB]
Get:6 http://nyc2.mirrors.digitalocean.com/ubuntu xenial-updates/universe i386 Packages [354 kB]
Fetched 993 kB in 1s (748 kB/s)
Reading package lists... Done
root@theitroad:~# apt-get upgrade

2.创建一个Git用户

下一步是创建一个git用户来管理GitLab中的git存储库。

root@theitroad:~# adduser --disabled-login --gecos 'GitLab' git
Adding user `git' ...
Adding new group `git' (1000) ...
Adding new user `git' (1000) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
root@theitroad-gitlab:~#

3.安装GitLab依赖包

如前所述,GitLab至少需要Ruby 2,x版本或者更高版本。
我们需要安装一些依赖包和开发工具来安装GitLab所需的软件。

root@theitroad-gitlab:~#apt-get install build-essential cmake zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate

4.安装Git

只需运行此命令,我们便可以从其存储库中安装Git软件包。

root@theitroad:~# apt-get install git

5.设置邮件服务器

为了接收有关我们的Git项目的电子邮件通知,我们需要配置一个电子邮件服务器。
我目前正在使用PostFix来设置我的邮件服务器。
我们可以使用此单个命令安装Postfix。

root@theitroad~# apt-get install postfix

在安装阶段,我们可以选择“ Internet站点”并输入FQDN服务器主机名以完成安装。

6.如何安装/配置Ruby

GitLab是一个纯粹基于Ruby on Rails的应用程序。
它具有一个版本管理器,可以防止推送和拉回SSH。
建议使用大于2.x的Ruby版本。

root@theitroad:~# mkdir /tmp/ruby && cd /tmp/ruby
root@theitroad:/tmp/ruby# wget http://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz
root@theitroad-:/tmp/ruby# tar xvzf ruby-2.1.2.tar.gz
root@theitroad:/tmp/ruby# cd ruby-2.1.2
root@theitroad-:/tmp/ruby/ruby-2.1.2# ./configure --disable-install-rdoc --prefix=/usr/local
root@theitroad:/tmp/ruby/ruby-2.1.2# make
root@theitroad:/tmp/ruby/ruby-2.1.2# make install

我们可以下载受支持的Ruby版本并按上述进行编译。
我们可以在安装后确认Ruby版本。

root@theitroad:/tmp/ruby/ruby-2.1.2# ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]

7.管理GitLab的数据库服务器

我们的下一步是为Git实验室创建数据库。
我正在使用PostgreSQL数据库来管理我的Git软件包。
它同时支持MySQL和PostgreSQL,但是建议在MySQL之上使用PostgreSQL。
我们可以将其与所有必需的模块一起安装,如下所示:

root@theitroad:~# apt-get install postgresql postgresql-client libpq-dev

我们需要为Gitlab创建一个数据库。
我用用户“ git”创建了一个名为“ gitlabhq_production”的新数据库来进行管理。

root@theitroad:~# su - postgres
postgres@theitroad:~$postgres createuser --createdb git
postgres@theitroad:~$createdb --owner=git gitlabhq_production

最后,我们可以使用以下命令用新的用户名和数据库名称确认数据库连接,以检查PostgreSQL版本:

root@theitroad:~# sudo -u git -H psql -d gitlabhq_production -c "SELECT VERSION()"
version
----------------------------------------------------------------------------------------------------------------
PostgreSQL 9.5.5 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20150609, 64-bit
(1 row)

如果该命令生成的是PostgreSQL版本,那么我们就很好了。

8)安装GitLab

现在我们准备开始安装。
我们可以从其官方存储库站点下载稳定的GitLab存储库。

我已经下载并将其安装到我的git主目录中。

root@theitroad:~# cd /home/git
root@theitroad-gitlab:/home/git# sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 6-9-stable gitlab
Cloning into 'gitlab'...
remote: Counting objects: 392871, done.
remote: Compressing objects: 100% (101153/101153), done.
remote: Total 392871 (delta 309481), reused 367895 (delta 286998)
Receiving objects: 100% (392871/392871), 184.55 MiB | 38.25 MiB/s, done.
Resolving deltas: 100% (309481/309481), done.
Checking connectivity... done.

我们可以从此处选择任何稳定的Repo分支。
其中我从GitLab存储库下载了6-9稳定的分支。
需要注意的重要一点是,我们永远不要在生产服务器上安装Master分支。

9)配置GitLab

移至git home目录,并复制示例GitLab配置文件以将其用作主配置文件“ gitlab.yml”。

root@theitroad:/home/git/gitlab# sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

我们需要将主机修改为服务器的标准域。
此外,在配置文件中将email_from和support_email设置为GitLab的首选电子邮件地址。

root@theitroad-gitlab:/home/git/gitlab#cat /home/git/gitlab/config/gitlab.yml
production: &base
gitlab:
host: nodenixbox.com 
port: 80
https: false
email_from: [email protected]
support_email: [email protected]

接下来,我们需要更正文件夹的所有权和权限,以使其按要求工作。
某些文件/文件夹需要写权限,以便GitLab可以在需要时对其进行写操作。

root@theitroad:/home/git/gitlab# chown -R git {log,tmp}
root@theitroad:/home/git/gitlab# chmod -R u+rwX {log,tmp,tmp/pids,tmp/sockets,public/uploads}

创建Satellite文件夹,并复制独角兽和Rack攻击配置的示例:

root@theitroad:/home/git/gitlab# sudo -u git -H mkdir /home/git/gitlab-satellites
root@theitroad:/home/git/gitlab# sudo chmod u+rwx,g+rx,o-rwx /home/git/gitlab-satellites
root@theitroad:/home/git/gitlab# sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
root@theitroad:/home/git/gitlab# sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

10)数据库配置

下一步是配置数据库设置,我们必须对数据库使用PostgreSQL。
我们需要复制配置文件并更改权限以使其对git可读:

root@theitroad:/home/git/gitlab# sudo -u git cp config/database.yml.postgresql config/database.yml
root@theitroad:/home/git/gitlab# sudo -u git -H chmod o-rwx config/database.yml

11)安装bundler

root@theitroad:/home/git/gitlab# sudo gem install bundler
Fetching: bundler-1.13.6.gem (100%)
Successfully installed bundler-1.13.6
Parsing documentation for bundler-1.13.6
Installing ri documentation for bundler-1.13.6
Done installing documentation for bundler after 7 seconds
1 gem installed
root@theitroad-gitlab:/home/git/gitlab# sudo -u git -H bundle install --deployment --without development test mysql aws

12)安装GitLabshell

我们可以安装GitLab shell,它是用于GitLab的SSH访问和存储库管理软件。

root@theitroad:/home/git/gitlab# sudo -u git -H bundle exec rake gitlab:shell:install[v1.9.4] REDIS_URL=redis://localhost:6379 RAILS_ENV=production
git clone 'https://gitlab.com/gitlab-org/gitlab-shell.git' '/home/git/gitlab-shell/'
Cloning into '/home/git/gitlab-shell'...
remote: Counting objects: 3207, done.
remote: Compressing objects: 100% (1203/1203), done.
remote: Total 3207 (delta 2043), reused 3013 (delta 1885)
Receiving objects: 100% (3207/3207), 481.86 KiB | 0 bytes/s, done.
Resolving deltas: 100% (2043/2043), done.
Checking connectivity... done.

现在,我们需要使用确切的gitlab URL编辑gitlab shell配置文件。

root@theitroad:/home/git/gitlab# cat /home/git/gitlab-shell/config.yml
--
user: git
gitlab_url: http://nodenixbox.com/
http_settings:
self_signed_cert: false
repos_path: "/home/git/repositories/"
auth_file: "/home/git/.ssh/authorized_keys"
redis:
bin: "/usr/bin/redis-cli"
host: localhost
port: 6379
namespace: resque:gitlab
log_level: INFO
audit_usernames: false

13)初始化数据库并激活GitLab

现在,移回GitLab主目录并执行此命令。

root@theitroad-:/home/git/gitlab# sudo -u git -H 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)? Do you want to continue (yes/no)? yes
Adding limits to schema.rb for mysql
== Seed from /home/git/gitlab/db/fixtures/production/001_admin.rb
2015-11-09T06:04:38Z 17990 TID-gtc2554j8 INFO: Sidekiq client with redis options {:url=>"redis://localhost:6379", :namespace=>"resque:gitlab"}
Administrator account created:
[email protected]
password......**

我们可以使用此管理员登录凭据来访问GitLab界面。

14)配置初始化脚本

我们可以从GitLab安装目录复制GitLab初始化文件,并添加GitLab以在启动时使用update-rc.d命令启动:

root@theitroad-:/home/git/gitlab# cp lib/support/init.d/gitlab /etc/init.d/gitlab
root@theitroad-:/home/git/gitlab# update-rc.d gitlab defaults 21

其次,复制GitLab logrotate配置以进行日志管理。

root@theitroad-gitlab:/home/git/gitlab# cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

最后,检查应用程序状态并编译资产,为git用户配置Git全局设置,并在一切正常的情况下启动GitLab服务。

root@theitroad-:/home/git/gitlab# sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
System information
System: Ubuntu 16.04
Current User: git
Using RVM: no
Ruby Version: 2.1.2p95
Gem Version: 2.2.2
Bundler Version:1.13.6
Rake Version: 10.3.1
Sidekiq Version:2.17.0
GitLab information
Version: 6.9.2
Revision: e46b644
Directory: /home/git/gitlab
DB Adapter: postgresql
URL: http://nodenixbox.com
HTTP Clone URL: http://nodenixbox.com/some-project.git
SSH Clone URL: [email protected]:some-project.git
Using LDAP: no
Using Omniauth: no
GitLab Shell
Version: 1.9.4
Repositories: /home/git/repositories/
Hooks: /home/git/gitlab-shell/hooks/
Git: /usr/bin/git
Compile status :
root@theitroad-gitlab:/home/git/gitlab# sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
Configure Git global settings:
root@theitroad-gitlab:/home/git/gitlab# sudo -u git -H git config --global user.name "GitLab"
root@theitroad-gitlab:/home/git/gitlab# sudo -u git -H git config --global user.email "[email protected]"
root@theitroad-gitlab:/home/git/gitlab# sudo -u git -H git config --global core.autocrlf input
Service start:
root@theitroad-gitlab:/home/git/gitlab# service gitlab start
root@theitroad-gitlab:/home/git/gitlab#
root@theitroad-gitlab:/home/git/gitlab# service gitlab status
● gitlab.service - LSB: GitLab git repository management
Loaded: loaded (/etc/init.d/gitlab; bad; vendor preset: enabled)
and running.
Nov 09 06:13:23 theitroad-gitlab systemd[1]: Started LSB: GitLab git repository management.

15)安装和配置Nginx

Nginx是GitLab唯一受支持的Web服务器。
其中我们使用Nginx来处理浏览器请求。
我们可以使用以下命令安装Nginx:

root@theitroad-:/home/git/gitlab# apt-get install nginx -y

现在,我们可以从GitLab目录复制Nginx虚拟主机配置,并删除默认的Nginx默认虚拟主机,然后重新启动Nginx服务。

root@theitroad-:/home/git/gitlab# cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
root@theitroad:/home/git/gitlab# rm -f /etc/nginx/sites-enabled/default

确保使用GitLab域编辑服务器名称。
创建一个符号链接来激活GitLab虚拟主机并重新启动Nginx服务。

root@theitroad:/home/git/gitlab# ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
root@theitroad:/home/git/gitlab# service nginx restart

16)保护GitLab

为了保护GitLab,我们可以为GitLab域安装SSL,并为Nginx虚拟主机启用SSL。
我已经使用letsencrypt为我的GitLab域nodenixbox.com安装了SSL,并将其添加到虚拟主机中。

root@theitroad:~/letsencrypt# ./letsencrypt-auto certonly --standalone --email <your email> --agree-tos -d nodenixbox.com
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/nodenixbox.com/fullchain.pem. Your cert will
expire on 2016-02-07. To obtain a new or tweaked version of this
certificate in the future, simply run letsencrypt-auto again. To
non-interactively renew *all* of your certificates, run
"letsencrypt-auto renew"
- If you lose your account credentials, you can recover through
e-mails sent to [email protected].
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG/Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

我已将此生成的SSL添加到我的GitLab虚拟主机中。
这就是现在的样子。

root@theitroad:~# cat /etc/nginx/sites-enabled/gitlab
# GITLAB
# Maintainer: @randx
# CHUNKED TRANSFER
# It is a known issue that Git-over-HTTP requires chunked transfer encoding [0] which is not
# supported by Nginx < 1.3.9 [1]. As a result, pushing a large object with Git (i.e. a single large file)
# can lead to a 411 error. In theory you can get around this by tweaking this configuration file and either
# - installing an old version of Nginx with the chunkin module [2] compiled in, or
# - using a newer version of Nginx.
## At the time of writing we do not know if either of these theoretical solutions works. As a workaround
# users can use Git over SSH to push large files.
## [0] https://git.kernel.org/cgit/git/git.git/tree/Documentation/technical/http-protocol.txt#n99
# [1] https://github.com/agentzh/chunkin-nginx-module#status
# [2] https://github.com/agentzh/chunkin-nginx-module
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen *:80 default_server; # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
server_name nodenixbox.com; # e.g., server_name source.example.com;
}
server {
listen 443 ssl;
server_name nodenixbox.com;
ssl_certificate /etc/letsencrypt/live/nodenixbox.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nodenixbox.com/privkey.pem;
server_tokens off; # don't show the version number, a security best practice
root /home/git/gitlab/public;
# Increase this if you want to upload large attachments
# Or if you want to accept large git objects over http
client_max_body_size 20m;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location/{
# serve static files from defined root folder;.
# @gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html @gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location @gitlab {
# If you use https make sure you disable gzip compression
# to be safe against BREACH attack
# gzip off;
proxy_read_timeout 300; # Some requests take more than 30 seconds.
proxy_connect_timeout 300; # Some requests take more than 30 seconds.
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://gitlab;
}
# Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
# WARNING: If you are using relative urls do remove the block below
# See config/application.rb under "Relative url support" for the list of
# other files that need to be changed for relative url support
location ~ ^/(assets)/{
root /home/git/gitlab/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
error_page 502 /502.html;

进行这些更改后,重新启动GitLab和Nginx服务。

17)通过浏览器访问GitLab

最后,我们准备通过浏览器访问我们的GitLab界面。
在启动它之前,请仔细检查GitLab应用程序状态以确认一切正常。

root@theitroad:/home/git# cd gitlab
oot@theitroad:/home/git/gitlab#sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

如果一切正常,我们可以通过调用URL >> https://GitLAB-domain-name来访问GitLab应用程序。
就我而言,我已将其安装在https://nodenixbox.com上。
我们可以使用在安装过程中创建的管理员凭据来访问它,用户:

登录[email protected]或者root
密码**