Apache and Virtual Hosts Question

Issues related to applications and software problems
Post Reply
blueknigh7
Posts: 1
Joined: 2020/01/03 02:09:20

Apache and Virtual Hosts Question

Post by blueknigh7 » 2020/01/03 02:21:30

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?

User avatar
TrevorH
Forum Moderator
Posts: 29472
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Apache and Virtual Hosts Question

Post by TrevorH » 2020/01/03 10:24:56

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.
CentOS 6 will die in November 2020 - migrate sooner rather than later!
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 is dead, do not use it.
Full time Geek, part time moderator. Use the FAQ Luke

User avatar
KernelOops
Posts: 322
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: Apache and Virtual Hosts Question

Post by KernelOops » 2020/01/03 16:47:37

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.
--
I love my computer - all my friends live there.
--

Post Reply

Return to “CentOS 7 - Software Support”