2

You might help me on these three questions.

1) Can I remove, i mean is it a good practice remove DocumentRoot and Directory directives from Apache main configuration file when it is set per virtual host configuration file?

2) I have the ServerName directive configured per virtual host, should I also set the ServerName on apache main configuration file? If so, should it be the same as the host machine meaning the hostname not the FQDN as I have some VirtualHosts on that host?

3) How exactly apache choose to serve one VirtualHost over another? On my current scenario when I try to access a virtual host when it is down (configuration not loaded) it is automatically redirected to the other VirtualHost DocumentRoot. Same happens where both virtual hosts are loaded but I access using the IP instead of virtual host name.

Main configuration file:

Listen [host machine IP]:80

ServerName [host machine IP]:80
<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www"

<Directory "/var/www/html/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
.
.
.

First VirtualHost: mydomain.com

<VirtualHost *:80>  
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot "/var/www/mydomain.com/html"
    ServerAdmin adm@rmydomain.net
    ErrorLog "/var/log/httpd/mydomain.com.log"
    CustomLog "/var/log/httpd/mydomain.com" combined

<Directory "/var/www/mydomain.com/html/">
    DirectoryIndex index.php index.html
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

Second VirtualHost: mydomain**.net**

<VirtualHost *:80>
    DocumentRoot "/var/www/mydomain.net/html"
    ServerName mydomain.net
    ServerAlias www.mydomain.net

    ServerAdmin adm@mydomain.net
    ErrorLog "/var/log/httpd/mydomain.net_error.log"
    CustomLog "/var/log/httpd/mydomain.net_access.log" combined

<Directory "/var/www/mydomain.net/html/">
    AuthType Basic
    AuthName "Protegido"
    AuthUserFile /etc/httpd/conf/htpasswd
    Require valid-user
    DirectoryIndex index.php index.html
    Options FollowSymLinks
    AllowOverride All

</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.net [OR]
RewriteCond %{SERVER_NAME} =mydomain.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Thank you.

0 Answers0