ubuntu
カテゴリ
Webサーバー Apache と Nginx
2026/03/14 10時apache
経費節約のため、光回線からホームルーター ZTE Speed Wi-Fi L13 ZTR02での楽天モバイルに切り替えたら、回線環境は問題ないが、Webサーバーの公開が出来なくなり頓挫していたが、Ubuntu Server 導入に伴い、ホームルーターでのポート開放では無く、Nginx と Tailscale Funnel とで、自分だけの公開に向け再トライする。
項目Apachenginx
登場1995年頃2004年
処理方式プロセス/スレッド型イベント駆動型
特徴柔軟・設定豊富高速・軽量・同時接続に強い
CGI(.cgiファイル)得意苦手(別途設定が複雑)
設定のしやすさ簡単やや難しい
Basic認証簡単できるが設定が面倒
特徴1リクエストごとにプロセス/スレッドを使う少数のプロセスで大量接続をさばく(イベント駆動)
柔軟でカスタマイズ性が高い静的ファイル(画像・動画)配信がめちゃ速い
同時アクセスが増えると重くなりやすいリバースプロキシが得意(裏で別サーバー動かす構成)
Web公開のツールはいくつか有るようだが、代表的なのが Apache と Nginx 後者のほうが後発なので良いかと思ったが、掲示板やBasic認証を使用するなら、Apache 一択となった。

■Apache 2 インストール

ホストにApache 2 をインストール
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2
sudo a2enmod cgi

以前Windowsでのcgiファイルは shift-JIS を使用していたが、Ubuntu Server は、UTF-8に変換するので nkf(Network Kanji Filter)をインストール
sudo apt update && sudo apt install nkf -y

基本設定 000-default.conf ファイルの作成
sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /mnt/data/docker/vafee-server/html
    <Directory /mnt/data/docker/vafee-server/html>
        Options +ExecCGI +Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        AddHandler cgi-script .cgi
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

詳細設定 vafee2.conf ファイルの作成
 sudo nano /etc/apache2/sites-enabled/vafee2.conf
<VirtualHost *:80>
    ServerName  vafee-lab.com
    ServerAlias www.vafee-lab.com
    ServerAlias ubuntu-sv.xxxxxxxx.ts.net
    DocumentRoot /mnt/data/docker/vafee-server/html
    ProxyPreserveHost On

    # ═══════════════════════════════════════════
    # アプリケーション プロキシ設定 ⇒internal-vafee-lab.conf 移行
    # ═══════════════════════════════════════════


    # ═══════════════════════════════════════════
    # Vafee2 Lounge (port 5000)
    # ═══════════════════════════════════════════

    # Basic認証
    <Location /vafee2/lounge/>
        AuthType Basic
        AuthName "Restricted Access"
        AuthUserFile /mnt/data/docker/vafee-server/conf/userpass
        Require valid-user
    </Location>

    # 認証不要フォルダ(Apache直接配信)
    <Location /vafee2/lounge/icons/>
        Require all granted
    </Location>
    <Location /vafee2/lounge/lounge_bg.gif>
        Require all granted
    </Location>
    <Location /vafee2/lounge/bg/>
        Require all granted
    </Location>
    <Location /vafee2/lounge/jpg/>
        Require all granted
    </Location>
    <Location /vafee2/lounge/video/>
        Require all granted
    </Location>
    <Location /vafee2/lounge/music/>
        Require all granted
    </Location>

    ProxyPass /vafee2/lounge/icons/      !
    ProxyPass /vafee2/lounge/lounge_bg.gif !
    ProxyPass /vafee2/lounge/bg/         !
    ProxyPass /vafee2/lounge/jpg/        !
    ProxyPass /vafee2/lounge/video/      !
    ProxyPass /vafee2/lounge/music/      !

    RequestHeader set X-Remote-User expr=%{REMOTE_USER}
    ProxyPass        /vafee2/lounge/ http://127.0.0.1:5000/
    ProxyPassReverse /vafee2/lounge/ http://127.0.0.1:5000/

    # 画像・動画ファイル配信
    ProxyPass        /files/ http://127.0.0.1:5000/files/
    ProxyPassReverse /files/ http://127.0.0.1:5000/files/

    # ═══════════════════════════════════════════
    # 静的ファイル・CGI
    # ═══════════════════════════════════════════
    <Directory /mnt/data/docker/vafee-server/html>
        Options +ExecCGI +FollowSymLinks +Indexes
        AddHandler cgi-script .cgi
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog  ${APACHE_LOG_DIR}/vafee2_error.log
    CustomLog ${APACHE_LOG_DIR}/vafee2_access.log combined
</VirtualHost>

アプリケーション プロキシ設定
プレフィックスパス方式
メリット
・新しいサブドメインのDNS登録(AdGuard書き換え or Cloudflareレコード)が不要。既存ドメイン配下に1行ProxyPass追加するだけで済む
・証明書は1つ(vafee-lab.com用)で足りる
・同じ公開サイトの一部として見せたいコンテンツには自然な構造
デメリット
・リンクの相対パスやJS内URLがサブパスを想定していないと壊れるため、golfアプリのように RequestHeader unset Accept-Encoding や、ProxyHTML的な小細工、tennisアプリのようにBlueprintでプレフィックス対応をアプリ側に作り込む必要が出る
・Cookie/セッションがドメイン単位で共有されるため、アプリ間でCookie名が衝突するリスクがある
・全アプリが1つのVirtualHostブロックに同居するため、設定が肥大化し見通しが悪くなる
・個別アプリごとに独立したアクセス制御・ログ分離がしにくい
・アプリ側の内部ルーティングが変わるたびにApache側のProxyPass記述も追従が必要、結合度が高い

サブドメイン方式
メリット
・アプリはルート/で普通に動けばよく、パス書き換えの小細工が一切不要(golf/tennisのような特殊対応が今後不要になる)
・Cookie/セッションがサブドメインごとに自然に分離される
・VirtualHostブロックが独立しているので、1アプリの設定ミスが他に波及しにくい
・ログ・Basic認証・アクセス制御をアプリ単位で綺麗に分離できる
・ワイルドカード証明書1枚で全サブドメインをカバーできるので証明書自体は増えない
デメリット
・サブドメインを増やすたびにDNS側の作業が必要(内部限定ならAdGuardの書き換え、外部公開が要るならCloudflareのレコード追加——vaultの単一障害点対策のときのような追加作業)
・VirtualHostブロックの数だけファイルが長くなる(ただし1ブロックの中身はシンプル)
・外部公開する場合はCloudflare側の設定も意識する必要がある(現状の内部サブドメイン群はTailscale経由限定、公開サイトはCloudflare経由という2系統が混在している状態)

当初「vafee2.conf」に、アプリケーション プロキシ設定はプレフィックスパス方式で設定していたが、自作アプリを作る過程で、サブドメイン方式で新たに「internal-vafee-lab.conf」を作成し移行した

# ============================================================
# 内部サービス用 vhost 群 (*.vafee-lab.com)
# 証明書: Let's Encrypt ワイルドカード (DNS-01 / Cloudflare)
# 名前解決: AdGuard Home の DNS 書き換えで 100.89.99.23 へ
# ============================================================

# --- EPGStation (WebSocket対応) ---
<VirtualHost *:443>
    ServerName epg.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        / http://127.0.0.1:8888/ upgrade=websocket
    ProxyPassReverse / http://127.0.0.1:8888/
</VirtualHost>

# --- AdGuard Home (管理画面) ---
<VirtualHost *:443>
    ServerName dns.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

# --- health (port 5003) ---

<VirtualHost *:443>
    ServerName health.vafee-lab.com

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5003/
    ProxyPassReverse / http://127.0.0.1:5003/

    ErrorLog ${APACHE_LOG_DIR}/health-error.log
    CustomLog ${APACHE_LOG_DIR}/health-access.log combined
</VirtualHost>

# --- Golf (port 5001) ---
<VirtualHost *:443>
    ServerName golf.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        /golf/ http://127.0.0.1:5001/golf/
    ProxyPassReverse /golf/ http://127.0.0.1:5001/golf/
    ProxyPass        /      http://127.0.0.1:5001/golf/
    ProxyPassReverse /      http://127.0.0.1:5001/golf/
    <Location />
        RequestHeader unset Accept-Encoding
    </Location>
</VirtualHost>

# --- Tennis (port 5002) ---
<VirtualHost *:443>
    ServerName tennis.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        /tennis/ http://127.0.0.1:5002/tennis/
    ProxyPassReverse /tennis/ http://127.0.0.1:5002/tennis/
    ProxyPass        /        http://127.0.0.1:5002/tennis/
    ProxyPassReverse /        http://127.0.0.1:5002/tennis/
</VirtualHost>

# --- PassManager (port 8100) ---
<VirtualHost *:443>
    ServerName pass.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        /passmanager/ http://127.0.0.1:8100/
    ProxyPassReverse /passmanager/ http://127.0.0.1:8100/
    ProxyPass        / http://127.0.0.1:8100/
    ProxyPassReverse / http://127.0.0.1:8100/
</VirtualHost>

# --- Netdata (port 19999) ---
<VirtualHost *:443>
    ServerName netdata.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        / http://127.0.0.1:19999/
    ProxyPassReverse / http://127.0.0.1:19999/
</VirtualHost>

# --- Vaultwarden (port 8080, WebSocket対応) ---
<VirtualHost *:443>
    ServerName vault.vafee-lab.com
    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/vafee-lab.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/vafee-lab.com/privkey.pem

    ProxyPreserveHost On
    ProxyPass        / http://127.0.0.1:8080/ upgrade=websocket
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

Apache 2 起動
sudo systemctl start apache2

設定ファイルのバックアップ用シンボリックリンク(バックアップ対象に含める
sudo ln -s /etc/apache2/sites-available/000-default.conf \
    /mnt/data/docker/vafee-server/apache-000-default.conf

■メール発信ソフト msmtp インストール

msmtp インストール
sudo apt install msmtp msmtp-mta -y

設定ファイル msmtprc の作成
sudo nano /etc/msmtprc
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        /var/log/msmtp.log
account        gmail
host           smtp.gmail.com
port           587
from           ◯◯@gmail.com
user           ◯◯@gmail.com
password       (Gmailアプリパスワード16文字)
account default : gmail

Ubuntu 側のファイアウォール80番ポートを開ける
sudo ufw allow 80/tcp
sudo ufw reload

■Windowsで使用していたcgiファイル等の修正

CGIプログラムと設定ファイル(.cgi, .pl, .dat)だけをShift-JIS /CRLF⇒UTF-8 / LF 一括変換
cd /mnt/data/docker/nginx/html
sudo find . -name ".cgi" -o -name ".pl" -o -name "*.dat" | xargs -n 10 sudo nkf -w -Lu --overwrite

全ファイルの持ち主をユーザーに変え、読み書き権限を与える
sudo chown 33:33 .
sudo chmod 777 .

htmlファイルのヘッダーを一個一個変更
<......" CONTENT="text/html; charset=SHIFT_JIS">

<......" CONTENT="text/html; charset=UTF-8">

cgiファイルは、Windows ActivePerlで、動かしていたので、Ubuntu Perl用に 先頭の一行のヘッダーを一個一個変更
#!/usr/local/bin/perl

#!/usr/bin/perl

掲示板フォルダ全体の権限を読み書き可能にする
sudo chmod -R 755 /mnt/data/docker/vafee-server/html/

windows と違って、 Ubuntu は、cgiファイルの修正を行うとコマンドを打たないと表示されなくなる
chmod 755 /mnt/data/docker/vafee-server/html/vafee2/view25/view25.cgi
sed -i 's/\r//' /mnt/data/docker/vafee-server/html/vafee2/view25/view25.cgi
docker exec -it vafee-apache perl -c /var/www/html/vafee2/view25/view25.cgi

これで、自宅内での掲示板が復活した。
記事一覧