如何在 DirectAdmin 服务器上使用 HTTPS 设置 Gitlab EE?

How to setup Gitlab EE with HTTPS on a DirectAdmin Server?

我已经在我的 Centos7 VPS (DirectAdmin) 上使用这个 post 设置了一个 Omnibus Gitlab 服务器:

它非常适合 HTTP 请求。 出于安全原因,我想在 GitLab 子域 gitlab.domain.com 上设置 HTTPS。 我想使用 LetsEncrypt 免费 SSL 证书。问题是 LetsEncrypt 无法使用 Certbot 验证我的域: certbot certonly --webroot --webroot-path=/var/www/letsencrypt -d gitlab.domain.com

失败并输出:

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: gitlab.domain.com
   Type:   unauthorized
   Detail: Invalid response from
   http://gitlab.domain.com/.well-known/acme-challenge/8Xj5vc-KMfhHYgH7PhXCFEetcxzQBDk-puiA2tRfoB4:
   "<!DOCTYPE html>\n<html class=\"devise-layout-html\">\n<head
   prefix=\"og: http://ogp.me/ns#\">\n<meta charset=\"utf-8\">\n<meta
   content=\"IE"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

我用谷歌搜索了一下,似乎 LetsEncrypt 必须到达文件夹路径:

http://gitlab.domain.com/.well-known/acme-challenge/xxxxxxxxxx

所以我创建了路径并授予了 777 权限,并出于测试目的在其中放置了一个 test.html。 现在我可以使用 HTTP 访问文件,但无法使用 HTTPS 访问它。

curl -I -k https://gitlab.domain.com/.well-known/acme-challenge/test.html

输出:

HTTP/1.1 301 Moved Permanently
Date: Wed, 06 Feb 2019 10:05:40 GMT
Server: Apache/2
Location: http://gitlab.domain.com/.well-known/acme-challenge/test.html
Content-Type: text/html; charset=iso-8859-1

我的服务器上已经有 DirectAdmin,但我不知道如何自定义我的子域的 HTTPD.conf 文件,以便一切正常。

直接管理员的自定义 HTTPD.conf 部分:

ServerName gitlab.domain.com
ServerSignature Off

ProxyPreserveHost On

# Ensure that encoded slashes are not decoded but left in their encoded state.
# http://doc.gitlab.com/ce/api/projects.html#get-single-project
AllowEncodedSlashes NoDecode

<Location />
  Order deny,allow
  Allow from all

  #Allow forwarding to gitlab-workhorse
  ProxyPassReverse http://127.0.0.1:8181
  ProxyPassReverse http://gitlab.domain.com/
</Location>

# Apache equivalent of nginx try files
# http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
# 
RewriteEngine on

# Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.* [NC,OR]
RewriteCond %{REQUEST_URI} !^.*/\.well-known/acme-challenge/.*$ [NC]
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

Alias /.well-known/acme-challenge/ /var/www/letsencrypt/
<Directory "/var/www/letsencrypt/">
     Order allow,deny
     Options Indexes FollowSymLinks MultiViews
     AllowOverride None
     Allow from all
</Directory>

# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public

值得一提的是,使用 https://letsdebug.net/ 和 HTTP-01 和 DNS-01 方法测试我的域 return 一切正常。

我认为如果我可以处理 HTTPS 请求以保证通过 HTTP 和 HTTPS 访问 LetsEncrypt API 到 http://gitlab.domain.com/.well-known/acme-challenge/ URL 就没问题了。

至于没有人回答我的问题,我一直在努力,终于找到了答案。

其实问题来源于两部分:

1.When 您想指向一个路径,该路径包含特殊字符,如“.”、“\”或 space,字符串中带有“...”,您必须使用它在 '\。'格式。我坚持认为它是适用的,以防万一你有一个用双引号括起来的字符串,而不是你配置文件中的每个参数。

<Directory "/var/www/default/\.well-known/acme-challenge/">

所以Alias/Directory部分的正确形式如下:

Alias /.well-known/acme-challenge/ /var/www/default/.well-known/acme-challenge/
<Directory "/var/www/default/\.well-known/acme-challenge/">
    Options None
    AllowOverride None
    ForceType text/plain
    RedirectMatch 404 "^(?!/\.well-known/acme-challenge/[\w-]{43}$)"
</Directory>

当您通过'curl'调用测试文件的URL时,您可能会得到"HTTP/1.1 302 Found"而不是"HTTP/1.1 200 OK"。它 returns 302 导致您以某种方式将请求重定向到您在地址栏中真正看到的其他地方。但是LetsEncrypt域授权是可以的

curl -I http://example.com/.well-known/acme-challenge/test.html

2.When 您调用 certbot 或 letsencrypt 命令,在 webroot 参数中它必须指向您的 ACME 挑战目录的根目录 (/var/www/default/) 而不是证书的最终目的地 (/var/www/default/.well-known/acme-challenge/) 因为 acmetool 本身在您提供给它的每个 webroot 中创建目录路径。因此,上面 Alias/Directory 的 certbot 或 letsencrypt 命令的正确形式应该如下所示:

letsencrypt certonly --webroot -w /var/www/default/ -d example.com -d www.example.com --renew-by-default

certbot certonly --webroot --webroot-path=/var/www/default/ -d example.com

我对 Apache 配置非常陌生,但就我搜索问题而言,对于那些想在 Apache 服务器上使用 LetsEncrypt 的人来说,这是一个非常受欢迎的问题。 如果这对你们大多数人来说是一个初级问题,请原谅我。

尽情享受吧!