如何在 Gitlab Omnibus 服务器旁边为其他虚拟主机提供服务? [完整的分步解决方案]

How to serve other vhosts next to Gitlab Omnibus server? [Full step-by-step solution]

我在专用 Ubuntu 14.04 服务器版本上安装了 Gitlab CE,带有 Omnibus 软件包

现在我想在 gitlab 旁边安装另外三个 虚拟主机

两个 node.js Web 应用程序由 non-root user 运行 在两个不同的 ports > 1024 上启动,第三个 PHP Web 应用程序需要一个要启动的 Web 服务器。

有:

但是 Omnibus listen 80 并且似乎既没有使用 Apache2 也没有使用 Nginx,因此我不能使用它们来为我的 PHP 应用程序和反向代理我的另外两个节点应用程序

What serving mechanics Gitlab Omnibus uses to listen 80 ? How should I create the three other virtual hosts to be able to provide the following vHosts ?

  • gitlab.mycompany.com (:80) -- already in use
  • bower.mycompany.com (:80)
  • npm.mycompany.com (:80)
  • packagist.mycompany.com (:80)

关于这些

But Omnibus listen 80 and doesn't seem to use neither Apache2 or Nginx [, thus ...].

和@stdob 评论:

Did omnibus not use nginx as a web server ??? –

我回复了

I guess not because nginx package isn't installed in the system ...

事实上

来自Gitlab官方文档:

By default, omnibus-gitlab installs GitLab with bundled Nginx.

是的!

Omnibus package actually uses Nginx !

但它是捆绑的,解释了为什么它不需要作为主机的依赖项安装 OS。

所以是的! Nginx 可以而且应该用于为我的 PHP 应用程序和 reverse-proxy 我的另外两个节点应用程序提供服务。

然后现在

Omnibus-gitlab 允许通过用户 gitlab-www 访问网络服务器 在具有相同名称的组中。允许外部网络服务器访问 GitLab,需要添加外部网络服务器用户 gitlab-www 组。

要使用其他 Web 服务器,例如 Apache 或现有的 Nginx 安装,您必须 以下步骤:

通过在 /etc/gitlab/gitlab.rb

中指定禁用捆绑的 Nginx
nginx['enable'] = false
# For GitLab CI, use the following:
ci_nginx['enable'] = false

检查 non-bundled web-server 用户的用户名。默认情况下,omnibus-gitlab 没有针对外部网络服务器用户的默认设置。 您必须在配置中指定外部网络服务器用户名! 例如,假设网络服务器用户是 www-data。 在 /etc/gitlab/gitlab.rb 中设置

web_server['external_users'] = ['www-data']

此设置是一个数组,因此您可以指定多个用户添加到 gitlab-www 组。

运行 sudo gitlab-ctl reconfigure 使更改生效。

设置 NGINX 监听地址或地址

默认情况下,NGINX 将接受所有本地 IPv4 地址上的传入连接。 您可以更改 /etc/gitlab/gitlab.rb.

中的地址列表
nginx['listen_addresses'] = ["0.0.0.0", "[::]"] # listen on all IPv4 and IPv6 addresses

对于 GitLab CI,使用 ci_nginx['listen_addresses'] 设置。

设置 NGINX 监听端口

默认情况下,NGINX 将监听 external_url 中指定的端口或 隐式使用正确的端口(HTTP 为 80,HTTPS 为 443)。如果你运行宁 GitLab 在反向代理后面,你可能想覆盖监听端口 别的东西。例如,要使用端口 8080:

nginx['listen_port'] = 8080

同样,对于 GitLab CI:

ci_nginx['listen_port'] = 8081

支持代理 SSL

默认NGINX会auto-detect如果external_url是否使用SSL 包含 https://。如果你在反向代理后面 运行ning GitLab,你 可能希望将 external_url 保留为 HTTPS 地址,但与 GitLab NGINX 内部通过 HTTP。为此,您可以使用禁用 HTTPS listen_https 选项:

nginx['listen_https'] = false

同样,对于 GitLab CI:

ci_nginx['listen_https'] = false

请注意,您可能需要配置反向代理以转发某些 headers(例如 HostX-Forwarded-SslX-Forwarded-ForX-Forwarded-Port)到 GitLab。

您可能会看到不正确的重定向或错误(例如“422 Unprocessable Entity”, "Can't verify CSRF token authenticity") 如果你忘了这一步。更多 资料见:

To go further you can follow the official docs at https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md#using-a-non-bundled-web-server

配置我们的 gitlab 虚拟主机

正在安装 Phusion Passenger

我们需要在 OS

中全局安装 ruby (gitlab 运行 in omnibus with a bundled ruby)
$ sudo apt-get update 
$ sudo apt-get install ruby
$ sudo gem install passenger

用passenger模块重新编译nginx

例如Apache2,nginx 不能插入二进制模块on-the-fly。必须为每个要添加的新插件重新编译它。

Phusion passenger 开发团队努力提供“a bundled nginx version of passenger”:使用 passenger 插件编译的 nginx bins。

所以,让我们使用它:

requirement: we need to open our TCP port 11371 (the APT key port).

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
$ sudo apt-get install apt-transport-https ca-certificates
创建 passenger.list
$ sudo nano /etc/apt/sources.list.d/passenger.list

这些线

# Ubuntu 14.04
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

use the right repo for your ubuntu version. For Ubuntu 15.04 for example: deb https://oss-binaries.phusionpassenger.com/apt/passenger vivid main

编辑权限:

$ sudo chown root: /etc/apt/sources.list.d/passenger.list
$ sudo chmod 600 /etc/apt/sources.list.d/passenger.list

正在更新软件包列表:

$ sudo apt-get update

允许它作为unattended-upgrades

$ sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

在文件顶部查找或创建此配置块:

// Automatically upgrade packages from these (origin:archive) pairs
Unattended-Upgrade::Allowed-Origins {

  // you may have some instructions here

};

添加以下内容:

// Automatically upgrade packages from these (origin:archive) pairs
Unattended-Upgrade::Allowed-Origins {

  // you may have some instructions here

  // To check "Origin:" and "Suite:", you could use e.g.:
  // grep "Origin\|Suite" /var/lib/apt/lists/oss-binaries.phusionpassenger.com*
    "Phusion:stable";

};

现在(重新)安装 nginx-extrapassenger:

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak_"$(date +%Y-%m-%d_%H:%M)"
$ sudo apt-get install nginx-extras passenger

配置它

取消注释 /etc/nginx/nginx.conf 文件中的 passenger_rootpassenger_ruby 指令:

$ sudo nano /etc/nginx/nginx.conf

... 获得如下内容:

##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;

创建 nginx 站点配置(虚拟主机 conf)

$ nano /etc/nginx/sites-available/gitlab.conf

server {
  listen *:80;
  server_name gitlab.mycompany.com;
  server_tokens off;
  root /opt/gitlab/embedded/service/gitlab-rails/public;

  client_max_body_size 250m;
  access_log  /var/log/gitlab/nginx/gitlab_access.log;
  error_log   /var/log/gitlab/nginx/gitlab_error.log;

  # Ensure Passenger uses the bundled Ruby version
  passenger_ruby /opt/gitlab/embedded/bin/ruby;

  # Correct the $PATH variable to included packaged executables
  passenger_env_var PATH "/opt/gitlab/bin:/opt/gitlab/embedded/bin:/usr/local/bin:/usr/bin:/bin";

  # Make sure Passenger runs as the correct user and group to
  # prevent permission issues
  passenger_user git;
  passenger_group git;

  # Enable Passenger & keep at least one instance running at all times
  passenger_enabled on;
  passenger_min_instances 1;

  error_page 502 /502.html;
}

现在我们可以启用它了:

$ sudo ln -s /etc/nginx/sites-available/gitlab.cong /etc/nginx/sites-enabled/

There is no a2ensite equivalent coming natively with nginx, so we use ln, but if you want, there is a project on github: nginx_ensite: nginx_ensite and nginx_dissite for quick virtual host enabling and disabling

This is a shell (Bash) script that replicates for nginx the Debian a2ensite and a2dissite for enabling and disabling sites as virtual hosts in Apache 2.2/2.4.

完成:-)。最后,重启nginx

$ sudo service nginx restart

使用这个新配置,您可以 运行 gitlab 旁边的其他虚拟主机来提供您想要的服务

只需在 /etc/nginx/sites-available 中创建新配置。

In my case, I made running and serving this way on the same host :

例如,服务 npm.mycompany.com :

为日志创建一个目录:

$ sudo mkdir -p /var/log/private-npm/nginx/

并填写一个新的虚拟主机配置文件:

$ sudo nano /etc/nginx/sites-available/npm.conf

有了这个配置

server {
  listen *:80;
  server_name npm.mycompany.com

  client_max_body_size 5m;
  access_log  /var/log/private-npm/nginx/npm_access.log;
  error_log   /var/log/private-npm/nginx/npm_error.log;

  location / {
    proxy_pass http://localhost:8082;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

然后启用并重启:

$ sudo ln -s /etc/nginx/sites-available/npm.conf /etc/nginx/sites-enabled/
$ sudo service nginx restart

因为我不想更改 gitlab 的 nginx 服务器(与其他一些集成),最安全的方法是下面的解决方案。

也按照

Gitlab:Ningx =>Inserting custom settings into the NGINX config

编辑你的 gitlab 的 /etc/gitlab/gitlab.rb:

nano /etc/gitlab/gitlab.rb

并滚动到 nginx['custom_nginx_config'] 并如下修改确保取消注释

# Example: include a directory to scan for additional config files
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"

创建新的配置目录:

mkdir -p /etc/nginx/conf.d/
nano /etc/nginx/conf.d/new_app.conf

并将内容添加到您的新配置

# my new app config : /etc/nginx/conf.d/new_app.conf
# set location of new app 
upstream new_app {
  server localhost:1234; # wherever it might be
}
# set the new app server
server {
  listen *:80;
  server_name new_app.mycompany.com;
  server_tokens off;
  access_log  /var/log/new_app_access.log;
  error_log   /var/log/new_app_error.log;
  proxy_set_header Host      $host;
  proxy_set_header X-Real-IP $remote_addr;
  location / { proxy_pass  http://new_app; }
}

并重新配置 gitlab 以插入新设置

gitlab-ctl reconfigure

重启nginx

gitlab-ctl restart nginx

查看nginx错误日志:

tail -f /var/log/gitlab/nginx/error.log