Page 1 of 1

Apache and Virtual Hosts Question

Posted: 2020/01/03 02:21:30
by blueknigh7
Hello! I've got a dumb question regarding Virtual Hosts and the conf.d files

I'm trying to setup Apache with two websites using a single IP and name based resolution. For example, let's use: example.com and staging.example.com

I understand that I can place both <Virtual Host *:80> blocks in a single /etc/httpd/conf/httpd.conf file, but I've also seen centOS tutorials where the virtual host blocks are being placed in /etc/httpd/conf.d/example.com.conf and /etc/httpd/conf.d/staging.example.com.conf. The latter makes a little more sense to me to keep the individual settings separate.

Should either solution work? I couldn't find any confirmation for multi-site name based resolution and having my blocks in the separate files. I've actually also tried testing it, and can only get the staging.example.com site to load properly. I can't find any obvious syntax issues, so I'm wondering if the multi-conf files are the issue somehow, or if my DNS is broken.

Here's an example:

example.com.conf

Code: Select all

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/example.com
  <Directory /var/www/html/example.com>
      Options FollowSymLinks
      AllowOverride All
      Require all granted
  </Directory>
</VirtualHost>
staging.example.com.conf

Code: Select all

<VirtualHost *:80>
  ServerName staging.example.com
  ServerAlias www.staging.example.com
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/staging.example.com
  <Directory /var/www/html/staging.example.com>
      Options FollowSymLinks
      AllowOverride All
      Require all granted
  </Directory>
</VirtualHost>
Any thoughts? Should this be working?

Re: Apache and Virtual Hosts Question

Posted: 2020/01/03 10:24:56
by TrevorH
Either way works but it's better to use the separate files as leaving httpd.conf unmodified means that a yum update of the httpd package will replace it with an updated copy if it is changed in the package. If you modify it then the update will not replace it but will create a .rpmnew file alongside it and you will need to manually merge them.

Re: Apache and Virtual Hosts Question

Posted: 2020/01/03 16:47:37
by KernelOops
The default httpd.conf file contains a line that looks like:

Code: Select all

IncludeOptional conf.d/*.conf
Effectively, that loads all conf files from conf.d, so to make management easier, you may create a separate configuration file per virtualhost.

this system is especially helpful when you have 100+ virtualhosts on the same system.