apache default virtual host configuration for www domain name

5,301

You have to tell apache the server names of each virtualhost using the ServerName and ServerAlias parameters.

Like:

<Virtualhost *:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com whatever.mydomain.com
    ...
</Virtualhost>
Share:
5,301

Related videos on Youtube

atyagi
Author by

atyagi

Updated on September 18, 2022

Comments

  • atyagi
    atyagi over 1 year

    Here is my apache default configuration for virtual host

    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    
    DocumentRoot /var/www
    <Directory />
        Options +FollowSymLinks
        AllowOverride None
    </Directory>
    
    <Directory "/var/www/mydir">
        AllowOverride FileInfo Limit Options Indexes
       </Directory>
    <Directory /var/www/>
        Options Indexes +FollowSymLinks MultiViews
        AllowOverride none
        Order allow,deny
        allow from all
    </Directory>
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    

    This works well for http://mydomain.com but not for http://www.mydomain.com How to serve my domain with www ?

    This problem is only for default domains, my other domains work for both with this config file

    <VirtualHost *:80>
    ServerName otherdomain.com
    ServerAlias www.otherdomain.com
    ServerAdmin webmaster@localhost
    
    DocumentRoot /var/www/otherdomain
    
    ServerSignature Off
    <Directory />
        Options +FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/otherdomain>
        Options Indexes +FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>
    
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog /var/log/apache2/otherdomain.com_access.log combined
    ErrorLog /var/log/apache2/otherdomain.com_error.log
    </VirtualHost>
    
    • Jenny D
      Jenny D about 11 years
      What do you mean by "not working"? What do you expect to happen, what happens instead, and what do the logs say?
    • atyagi
      atyagi about 11 years
      If I access defaultdomain.com then browser shows "server not found" error. It works for defaultdomain.com
    • lg.
      lg. about 11 years
      Maybe it is a stupid question, but...DNS record for www is properly configured?
  • atyagi
    atyagi about 11 years
    Please check default config file code, it has no ServerName <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www .... serverAlias works for otherdomain config, but there is no servername in default so how to put serverAlias there?
  • Sean Brill
    Sean Brill about 11 years
    besides the serverconfig, you also have to make sure that your DNS is pointing to the right IPs
  • atyagi
    atyagi about 11 years
    DNS is pointing to correct IP. It should be simple but seems I am missing something. All other domain urls are working with ServerName and ServerAlias but there is no ServerName in default config so I can not put any ServerAlias. Still thanks for your help.
  • Jenny D
    Jenny D about 11 years
    If this is the default domain, then all requests that don't match any other virtualhost will go to that domain by default, so in this particular case there's no need to enter any ServerAlias.