1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

おうちサーバー構築報告:リバースプロキシ with Let's Encrypt で GitLab 18.9.0-ce.0

1
Posted at

能書き

おうちサーバー構築報告:予告からのおうちサーバー構築です。

家庭内にGitLabサーバーを立てたので、他の様々なサービスと共に外部公開したい(グローバルIPアドレスを共有したい)のでリバースプロキシを設定します。

前提

目標

  • リバースプロキシを立ててGitLabサーバーへアクセスできるようにする
    • Let's Encrypt の証明書を取得してhttpsアクセスできるようにする

参考文献

今回の設定に必要な事前準備などは下記になります。

過去、似たような事をやっています。そのリンク。

sshのポートフォワーディングに関しては下記がわかりやすかったです。

systemdのサービスを作成する記事もそこら中に溢れてますが、例えば下記など。

GitLabとsshで通信するには公開鍵の登録が必要です。その手順は下記などを参照して下さい。

DNS

まず最初にDNSを設定します。Let's Encrypt 証明書を取得する為に外からアクセス可能にする必要があります。その関係で、お金を出して買った独自ドメインを使用して下さい。ここでは仮にexample.comにしておきますが、このまま設定しないで下さい。

マシン=172.16.1.103:ユーザー=root
MY_DOMAIN=example.com
マシン=172.16.1.103:ユーザー=root
GITLAB_DOMAIN=gitlab.$MY_DOMAIN

そして外部のDNSにこれを設定し、家庭内ルーターのポートフォワードなども設定しておきます。今回設定するリバースプロキシサーバー172.16.1.103に向けます。この辺りの手順はそれぞれ異なりますので、各々のマニュアルなどを参照して下さい。

家庭内DNSも、前回の設定だと172.16.1.104を向いているので、172.16.1.103に修正します。viなどのエディタで修正してください。

マシン=172.16.1.101(network.local):ユーザー=root
cd /etc/unbound/unbound.conf.d
vi machines.list
unbound-checkconf
systemctl restart unbound

そして/etcをSubversion管理するのが我が家の流儀です。

マシン=172.16.1.101(network.local):ユーザー=root
cd /etc
svn st
svn st | grep "^?" | cut -b9- | xargs -I{} find {} -type f -or -type d -or -type l | xargs -rt svn add
svn ci -m"DNS setting for GitLab proxy"
svn up

リバースプロキシの予備設定

マシン=172.16.1.103:ユーザー=root
apt update && apt upgrade -y

もしnginxをインストールしてなかったらapt install -y nginxします。

マシン=172.16.1.103:ユーザー=root
apt install -y nginx

インストール直後の様子をSubversion登録します。

マシン=172.16.1.103:ユーザー=root
cd /etc
svn st | grep "^?" | cut -b9- | sudo xargs -I{} find {} -type f -or -type d -or -type l | xargs -rt svn add
svn ci -m"install nginx"
svn up

デフォルトサイトを設定します。設定自体は最初から既にあるようなので、表示対象になっているindex.htmlを作成します。セキュリティを考慮して何も表示しないようにします。

マシン=172.16.1.103:ユーザー=root
echo >/var/www/html/index.html

Let's Encrypt 証明書

取得と設定

必要なツールcertbotをインストールします。

マシン=172.16.1.103:ユーザー=root
apt install -y certbot

インストール直後の状態をSubversion登録します。

マシン=172.16.1.103:ユーザー=root
cd /etc
svn st
svn st | grep "^?" | cut -b9- | xargs -rt svn add
svn ci -m"installed certbot"
svn up

Let's Encrypt で証明書を取得して設定します。その為のnginx設定をします。

マシン=172.16.1.103:ユーザー=root
GITLAB_HOST_IPADDR=172.16.1.104
GITLAB_PORT_HTTP=20080
マシン=172.16.1.103:ユーザー=root
cd /etc/nginx/sites-available
cat <<___ >gitlab.conf
server {
    listen        80;
    server_name   $GITLAB_DOMAIN;
    server_tokens off;

    location ^~ /.well-known/acme-challenge/ {
        root   /usr/share/nginx/html/ssl-proof;
    }

    location / {
        proxy_pass http://$GITLAB_HOST_IPADDR:$GITLAB_PORT_HTTP;
    }
}
___
cd ../sites-enabled
ln -s /etc/nginx/sites-available/gitlab.conf

nginxに新しい設定を読み込ませます。

マシン=172.16.1.103:ユーザー=root
nginx -s reload

Let's Encrypt 証明書を取得します。

マシン=172.16.1.103:ユーザー=root
mkdir /usr/share/nginx/html/ssl-proof
certbot certonly --register-unsafely-without-email --agree-tos --webroot -w /usr/share/nginx/html/ssl-proof -d $GITLAB_DOMAIN

certbotの出力を見て成功していたら(Successfully received certificate. の表示があったら)/etc/nginx/sites-available/gitlab.confを書き換えます。

マシン=172.16.1.103:ユーザー=root
cat <<___ >/etc/nginx/sites-available/gitlab.conf
server {
    listen        80;
    server_name   $GITLAB_DOMAIN;
    server_tokens off;

    location ^~ /.well-known/acme-challenge/ {
        root /usr/share/nginx/html/ssl-proof;
    }

    location / {
        return 301 https://$GITLAB_DOMAIN;
    }
}
server {
    listen        443 ssl;
    server_name   $GITLAB_DOMAIN;
    server_tokens off;

    ssl_certificate     /etc/letsencrypt/live/$GITLAB_DOMAIN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/$GITLAB_DOMAIN/privkey.pem;

    location / {
        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_set_header  X-Forwarded-Proto https;
        proxy_set_header  X-Frame-Options   SAMEORIGIN;
        proxy_pass http://$GITLAB_HOST_IPADDR:$GITLAB_PORT_HTTP;
    }
}
___

nginxに新しい設定を読み込ませます。

マシン=172.16.1.103:ユーザー=root
nginx -s reload

普段使いのクライアントマシンからアクセスすると(httpsにリダイレクトされてから)GitLabのサインイン画面が表示されます。

維持の為の設定

どうやらcertbotインストール時に、自動的に毎日2回の更新確認が入るようです。

マシン=172.16.1.103:ユーザー=root
# systemctl status certbot.timer
* certbot.timer - Run certbot twice daily
     Loaded: loaded (/usr/lib/systemd/system/certbot.timer; enabled; preset: enabled)
     Active: active (waiting) since Mon 2026-02-23 07:44:32 UTC; 1h 23min ago
    Trigger: Mon 2026-02-23 19:56:09 UTC; 10h left
   Triggers: * certbot.service

Feb 23 07:44:32 proxy systemd[1]: Started certbot.timer - Run certbot twice daily.

更新可能である事を一応確認しておきましょう。下記コマンドを実行します。

マシン=172.16.1.103:ユーザー=root
certbot renew --dry-run

メッセージは英語ですが。最後の方に下記が表示されたら大丈夫、かな。

Congratulations, all simulated renewals succeeded:

最後にSubversionへ登録します。証明書だけは登録しないようにします。

マシン=172.16.1.103:ユーザー=root
cd /etc
svn add --depth empty letsencrypt
svn add --depth empty letsencrypt/archive letsencrypt/renewal letsencrypt/live
svn propset svn:ignore "*" letsencrypt/archive letsencrypt/renewal letsencrypt/live
svn add letsencrypt/*
マシン=172.16.1.103:ユーザー=root
svn st
svn st | grep "^?" | cut -b9- | xargs -rt svn add
svn ci -m"GitLab"
svn up

GitLab

リバースプロキシを設定した事によってURLが変わりましたので、それを反映します。

設定

マシン=172.16.1.104(docker.local):ユーザー=administrator
cd /srv/gitlab/config
sudo cp -p gitlab.rb gitlab.rb.$(date +%Y%m%d)
sudo vi gitlab.rb

修正内容は下記になります。

マシン=172.16.1.104(docker.local):ユーザー=administrator
$ sudo diff gitlab.rb{.$(date +%Y%m%d),}
32c32
< external_url 'http://gitlab.example.com:20080'
---
> external_url 'https://gitlab.example.com'
1884c1884
< # nginx['listen_port'] = nil
---
> nginx['listen_port'] = 20080
1888c1888
< # nginx['listen_https'] = nil
---
> nginx['listen_https'] = false

コンテナを再起動してGitLabを再設定します。

マシン=172.16.1.104(docker.local):ユーザー=administrator
cd /srv/gitlab
docker compose restart

確認

例えば人を追加するとメールが飛びますが、そのメールの中にあるリンク「Click here to set your password」のURLがきちんとリバースプロキシのURLになっているかどうかを確認します。

ssh

GitLabはSSHプロトコルを使用してgitと通信できます。そのSSHもリバースプロキシ経由で接続できるようにしておきます。sshポートフォワーディングですな。

設定

まず、proxyコンテナ(172.16.1.103)からGitLabサーバーマシン(172.16.1.104)に接続する際、パスワード不要で接続できるようにします。

マシン=172.16.1.103:ユーザー=root
ssh-keygen -t ed25519
マシン=172.16.1.103:ユーザー=root
ssh-copy-id administrator@172.16.1.104

パスワード不要になったか確認。

マシン=172.16.1.103:ユーザー=root
ssh administrator@172.16.1.104

それから下記を実行してsystemdサービスを作成し、コンテナ再起動時に自動起動するようにします。

マシン=172.16.1.103:ユーザー=root
cat <<___ >/etc/systemd/system/portforward.service
[Unit]
Description = Port Forwarding with ssh

[Service]
ExecStart = /usr/bin/ssh -gNL 20022:localhost:20022 administrator@172.16.1.104
Restart = always
Type = simple

[Install]
WantedBy = multi-user.target
___
systemctl daemon-reload
systemctl enable portforward

とりあえず起動してみます。

マシン=172.16.1.103:ユーザー=root
systemctl start portforward

起動確認。

マシン=172.16.1.103:ユーザー=root
systemctl status portforward

確認

少々面倒臭いですが。

  1. 普段使いのマシンにgit操作の設定
    私はWindows11にWSL2を導入しました。Ubuntuにすると自動でgitクライアントがインストールされてるようです。WSL2導入手順は省略します。
  2. GitLab上にプロジェクトを作成
    具体的な手順は省略します。テスト用なので空のプロジェクトを作成し、READMEファイルだけ生成すればよいでしょう。表示レベルは、テスト用なので権限とか考えずに済むように公開で。プロジェクト名をtestとするとプロジェクトのURLはhttps://gitlab.example.com/root/testになります。
  3. sshキーの登録
    sshでGitLabと通信するには公開鍵をGitLabに登録する必要があります。その手順は参考文献を参照ください。ここでは説明しません。
  4. GitLab画面上の表示をコピペしてsshでgit cloneできるかどうか確認
    WSL2でUbuntuなら下記コマンドになります。
    普段使いのWindowsマシンのWSL2
    git clone ssh://git@gitlab.example.com:20022/root/test.git
    

仕舞い

GitLabサーバーをリバースプロキシ経由で利用する事が出来るようになりました。やりましたぜ!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?