Can’t configure two subdomains apache 2.4

apache-2.2apache-2.4subdomainvirtualhost

I'm trying to configure two subdomains using apache 2.4, but seems that there is a problem I can't resolve.

Here is the apache configuration file

 <VirtualHost *:80>
    ServerName www.subdomain1.myweb.com
    ServerAlias subdomain1.myweb.com
    DocumentRoot /srv/webapps/mywebapp
    <Directory /srv/webapps/mywebapp>
       AllowOverride all
       Options -MultiViews
       Require all granted
    </Directory>
 </VirtualHost>

 <VirtualHost *:80>
   ServerName www.subdomain2.myweb.com
   ServerAlias subdomain2.myweb.com
   DocumentRoot /srv/webapps/mywebapp2
   <Directory /srv/webapps/mywebapp2>
      AllowOverride all
      Options -MultiViews
      Require all granted
  </Directory>

I tried defining the ServerName, but didn't solve the problem:

ServerName www.myweb.com

And tried using the server IP in VirtualHost, using wildcards, using domain in VirtualHost tag, etc… Anything worked as desired yet 🙁

The point is that apache server subdomain1.myweb.com, but the subdomain.myweb.com don't. The only way I cant serve both subdomains it's settings the last one as:

 <VirtualHost *:80>
   ServerName www.myweb.com
   ServerAlias myweb.com
   DocumentRoot /srv/webapps/mywebapp2
   <Directory /srv/webapps/mywebapp2>
      AllowOverride all
      Options -MultiViews
      Require all granted
  </Directory>

So I can serve one subdomain and the other one in the root path, but that's not the desired behaviour.

I guess that somehow apache it's matching the request with the first subdmain, don't know why.

Apache shows this as virtual servers mapping:

 VirtualHost configuration:
 *:80                   is a NameVirtualHost
        default server www.myweb.com
        port 80 namevhost www.myweb.com
        alias subdomain1.myweb.com
        port 80 namevhost www.subdomain2.myweb.com
        alias subdomain2.com
 ServerRoot: "/etc/httpd"
 Main DocumentRoot: "/srv/http"
 Main ErrorLog: "/var/log/httpd/error_log"
 Mutex proxy: using_defaults
 Mutex default: dir="/run/httpd/" mechanism=default 
 Mutex mpm-accept: using_defaults
 Mutex proxy-balancer-shm: using_defaults
 PidFile: "/run/httpd/httpd.pid"  
 Define: DUMP_VHOSTS
 Define: DUMP_RUN_CFG

Thank you in advance!

EDIT

httpd.conf basically the default configuration because I got this problem since first moment and I didn't configure anything yet, excepting the virtual hosts.

ServerRoot "/etc/httpd"

Listen 80

<IfModule unixd_module>
User http
Group http
</IfModule>


<Directory />
    AllowOverride none
    Require all denied
</Directory>


DocumentRoot "/srv/http"
<Directory "/srv/http">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>


<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>


<Files ".ht*">
    Require all denied
</Files>

ErrorLog "/var/log/httpd/error_log"

LogLevel warn


<Directory "/srv/http/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf

# Multi-language error messages
Include conf/extra/httpd-multilang-errordoc.conf

# Fancy directory listings
Include conf/extra/httpd-autoindex.conf

# Language settings
Include conf/extra/httpd-languages.conf

# User home directories
Include conf/extra/httpd-userdir.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

# Various default settings
Include conf/extra/httpd-default.conf

#PHP
Include conf/extra/php5_module.conf

# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>

<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

http-vhosts.conf is the same as the initial question

httpd-default.conf

#
# This configuration file reflects default settings for Apache HTTP Server.
#
# You may change these, but chances are that you may not need to.
#

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 60

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5

#
# UseCanonicalName: Determines how Apache constructs self-referencing 
# URLs and the SERVER_NAME and SERVER_PORT variables.
# When set "Off", Apache will use the Hostname and Port supplied
# by the client.  When set "On", Apache will use the value of the
# ServerName directive.
#
UseCanonicalName Off

#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride 
# directive.
#
AccessFileName .htaccess

#
# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of:  Full | OS | Minor | Minimal | Major | Prod
# where Full conveys the most information, and Prod the least.
#
ServerTokens Full

#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory 
# listings, mod_status and mod_info output etc., but not CGI generated 
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of:  On | Off | EMail
#
ServerSignature Off

#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

#
# Set a timeout for how long the client may take to send the request header
# and body.
# The default for the headers is header=20-40,MinRate=500, which means wait
# for the first byte of headers for 20 seconds. If some data arrives,
# increase the timeout corresponding to a data rate of 500 bytes/s, but not
# above 40 seconds.
# The default for the request body is body=20,MinRate=500, which is the same
# but has no upper limit for the timeout.
# To disable, set to header=0 body=0
#
<IfModule reqtimeout_module>
  RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
</IfModule>

I tried to play with http-default.conf changing UseCanonicalName or HostnameLookups to give them a shot, but nothing worked so far…

SOLUTION

Finally seems that it was a DNS configuration problem, more than a virtual host configuration.

Using in this case Google domains, just adding a custom resource record for every subdomain, and pointing to the IP address that will handle the request (in my case the same server, that will handle the request with the virtual hosts as the OP shows), made it work.

Thank you all!

Best Answer

I noticed in your subdomain2, you didn't include the . Not sure if that's just in the config here, or yours. Did you do service httpd reload (or service apache2 reload depending on OS) Is your DNS pointed at your IP using those domain names?

Related Topic