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

 <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

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

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"  

Thank you in advance!


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

<Directory />
    AllowOverride none
    Require all denied

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

<IfModule dir_module>
    DirectoryIndex index.html

<Files ".ht*">
    Require all denied

ErrorLog "/var/log/httpd/error_log"

LogLevel warn

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

# 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

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 ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin

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


# 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 (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

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


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!

  • Did you manage to get it working? I believe I am having the same issue. – Luís de Sousa Jun 22 '15 at 9:45
  • Yes I did. Sorry for not posting the solution. Finally seemed that it was a DNS configuration problem from the beginning. Please see the updated answer to get more details ;) – qgadrian Jun 23 '15 at 20:30

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?

  • Yes, I reload at every change I made. Also, my DNS is pointing to the server IP using those domains. I don't get what you men I'm missing, a point? – qgadrian Apr 28 '15 at 20:17

TL;DR delete www

www is already a subdomain of example.com.

so you certainly are able to define 3rd or 4th level domains from your original domain. But you most likely do not want something.www.yourdomain.com as your subdomain.

What you probably want is subdomain.yourdomain.com, thus just delete the wwww.

Havent tested your configuration yet, so I dont know about the rest.

  • I tried, still the same behaviour. Serving first subdomain and the second one as the subdomain1. – qgadrian Apr 28 '15 at 20:18
  • ok, I tested your configuration, and it should work. Could you post your httpd.conf file, or on debian/ ubuntu apache.conf in the parent forlder of your sites-available (virtualhost config files). As it works here locallly , I think it is the some other 'overriding' configuration. – jojomojo Apr 28 '15 at 21:27
  • Thank you, good to know that worked, I posted the config files. They are the default ones, but maybe there is something I'm not looking at... Anyway, I'll try to look into apache configuration to see what is overriding the virtual hosts mapping. – qgadrian Apr 28 '15 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.