Friday, December 29, 2023

VirtualHost の設定(複数バーチャル・ホスト)

小ネタですが、サブドメインなど複数バーチャル・ホストを設定したときのメモ

/etc/httpd/conf.d/vhost.conf 内、下記の記述があるので、ServerName をサブドメイン(もしくは別ドメイン)に変更した <VirtualHost *:80> から </VirtualHost> までを丸ごとコピー&ペーストして、vhost.conf に追加(オリジナルのドメインの記述はそのまま):

<VirtualHost *:80>

  # REQUIRED. Set this to the host/domain/subdomain that
  # you want this VirtualHost record to handle.

  ServerName ほげほげ.com

  # Optional. You can specify additional host names that
  # serve up the same site. This can be top-level, domains,
  # sub-domains, and can even use wildcard subdomains such
  # as *.yourdomain.com - just separate each host name
  # with a single space.

  #ServerAlias ほげほげ.com

  # REQUIRED. Set this to the directory you want to use for
  # this vhost site's files.

  DocumentRoot /var/www/html/ほげほげ

  # Optional. Uncomment this and set it to your admin email
  # address, if you have one. If there is a server error,
  # this is the address that Apache will show to users.

  ServerAdmin admin@ほげほげ.com

  # Optional. Uncomment this if you want to specify
  # a different error log file than the default. You will
  # need to create the error file first.

  #ErrorLog /var/www/vhosts/logs/error_log

  # REQUIRED. Let's make sure that .htaccess files work on 
  # this site. Don't forget to change the file path to
  # match your DocumentRoot setting above.
  
  <Directory /var/www/html>
    AllowOverride All
  </Directory>

</VirtualHost>

Apache を再起動して設定を反映:

 sudo systemctl restart httpd 

DocumentRoot は別ディレクトリに作成して、アクセス権を設定(参照

SSL は追加したドメイン(またはサブドメイン)ごとに新規に証明書を生成しなくてはいけないが(参照今回はスキップ。

参考:https://qiita.com/hx_kei/items/60519b6e04fc5e6ce83d
https://httpd.apache.org/docs/current/vhosts/examples.html

No comments:

Post a Comment