目的
サブドメインをユーザごとに割当てた Web スペースを用意したい。例えばユーザ foo と bar がいて
- foo.example.com -> /home/foo/www
- bar.example.com -> /home/bar/www
のように参照する構成にしたい。加えてそれぞれのディレクトリを参照する際の Apache の実行ユーザは、ディレクトリのオーナーと揃えたい(/home/foo/www を参照するときの実行ユーザは www-data ではなく foo)。
必要なもの
サブドメインをそれぞれのユーザに割り当てるには VirtualHost を使います。実行ユーザをディレクトリで変更するには、apache2-mpm-itk を使って設定に AssignUserID username groupname とすればできます。
環境
- OS: Debian 9 (Stretch) 64bit
- VPS: ConoHa 512MB
- DNS: お名前.com
DNS の設定
ドメインを取得したところに応じて先に設定しておいてください。今回は foo.example.com の A レコードに VPS のグローバル IP アドレスを設定しました。
インストール
Debian 9 (Strech) の場合は apache2 と libapache2-mpm-itk を apt で install します。
# apt install apache2 libapache2-mpm-itk
# a2dismod mpm_evnet
# a2enmod mpm_itk
# systemctl restart apache2
サブドメインの VirtualHost を設定する
今回は foo.example.com を /home/foo/www に割当てます。最近の Debian 系の流儀に倣い、/etc/apache2/sites-available 以下に設定ファイルを作成します。
# vi /etc/apache2/sites-available/foo.conf
/etc/apache2/sites-available/foo.conf
<VirtualHost *:80>
ServerName foo.example.com
DocumentRoot /home/foo/www
AssignUserID foo foo
<Directory /home/foo/www>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/foo/error.log
CustomLog ${APACHE_LOG_DIR}/foo/access.log combined
</VirtualHost>
<Directory> ディレクティブは Web スペースの利用用途によって書き換えてください。例えば .htaccess による上書きを全て許可し、CGI を動かすのであれば次のようになります。
<Directory /home/foo/www>
AllowOverride All
Options +ExecCGI
AddHandler cgi-script .cgi
Require all granted
</Directory>
foo.example.com へのアクセスはログを分けます。ログの保存先は予め作成しておきましょう。
# mkdir /var/log/apache2/foo
作ったサイトを有効にします。
# a2ensite foo
ここまで進んだら設定の再読込を行い、foo.example.com にアクセスできるか確認してみましょう。サブドメインが増えても新たに設定ファイル .conf を作ってここまでの手順を繰返すだけです。
# systemctl reload apache2
ワイルドカード証明書
以下の方法は自動更新に対応できていません。3 ヶ月毎に同じ手順を踏む必要があります。
続いて、今の時代は HTTPS が当り前なので SSL 証明書の設定を行います。サブドメイン毎に証明書を取得することもできますが、目的は通信の暗号化だけなのでワイルドカード証明書 (*.example.com) を設定します。
apt install certbot で入る certbot は古くてワイルドカード証明書を取得できないので、github から最新の certbot を git clone して使います。オプションが多いので改行入れていますが、一行で実行しても構いません。
# apt install git
# git clone https://github.com/certbot/certbot.git
# cd certbot
# ./certbot-auto certonly --manual -d *.example.net \
-m [email protected] --agree-tos \
--manual-public-ip-logging-ok --preferred-challenges dns-01
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
y-oBF_mUCy6EafROfLJAvnEujA2ot_xvC4zCcJKkJ8U
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
今回はお名前.com を使っているので、お名前.com の DNS 設定を開きます。”_acme-challenge” の TXT レコードを追加して、内容を following value の値に設定します。DNS の設定が反映されまで少々時間がかかるので、まだ Enter キーは叩かないでください。
Windows PC を使っているならコマンドプロンプトを開き、nslookup を叩いてみます。
C:\> nslookup -type=TXT _acme-challenge.example.com
サーバー: one.one.one.one
Address: 1.1.1.1
権限のない回答:
_acme-challenge.example.com text =
"y-oBF_mUCy6EafROfLJAvnEujA2ot_xvC4zCcJKkJ8U"
設定した値が表示されたら Enter キーを押して進みましょう。まだ反映されてなかったら “_acme-challeng.example.com を見つけられません: Non-existent domain” と表示されます。
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2018-12-10. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- 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
“Congratulations!” と出たら Let’s encrypt からの証明書取得は完了です。
Apache に証明書を設定する
まず SSL モジュールを有効化します。
# a2enmod ssl
ポートを修正し、取得した証明書を書き加えます。
/etc/apache2/site-available/foo.conf
<VirtualHost *:443>
...
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
...
</VirtualHost>
設定を再読込して完了です。
# systemctl reload apache2
参考文献