Running multiple virtual hosts

15,103

Solution 1

You have a NameVirtualHost directive, but you're not using it. You're specifying the IP address and/or the hostname in your <VirtualHost> declaration - that's not what you want.

Change:

<VirtualHost xx.xx.xx.xx:80>

and

<VirtualHost secondDomain.com>

To both be simply:

<VirtualHost *:80>

Solution 2

You may try next:

  1. Move firstDomain.com virtual host declaration to sites-available/firstDomain.
  2. Remove everything from httpd.conf (you may try to define ServerName in apache2.conf)
  3. Ensure that you have sites-available/firstDomain and sites-available/secondDomain symlinked to sites-enabled/firstDomain and sites-enabled/secondDomain respectively.
Share:
15,103

Related videos on Youtube

cherrun
Author by

cherrun

Updated on September 18, 2022

Comments

  • cherrun
    cherrun about 1 year

    I want to run multiple domains on my server (only two at the moment). I already changed both nameservers to point to my server.

    In the apache2.conf file I enabled "included sites-enabled".

    My ports.conf file looks as follows

    NameVirtualHost *:80
    Listen 80
    Listen 443
    
    <VirtualHost xx.xx.xx.xx:443>
        SSLEngine On
        SSLCertificateFile /etc/apache2/ssl/apache.pem
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key
    
        ServerAdmin [email protected]
        ServerName firstDomain.com
        DocumentRoot /var/www/
        ErrorLog /var/log/error.log
        CustomLog /var/log/access.log combined
    </VirtualHost>
    

    This is the setting for my main page.

    My httpd.conf file looks like this

    ServerName firstDomain.com
    
    <VirtualHost xx.xx.xx.xx:80>
    ServerAdmin [email protected]
    ServerName firstDomain.com
    DocumentRoot /var/www/
    ErrorLog /var/log/error.log
        CustomLog /var/log/access.log combined
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName secondDomain.com
        ServerAlias *.secondDomain.com
        DocumentRoot /home/user/www/secondDomain.com/htdocs
    </VirtualHost>
    
    Options -Indexes All FollowSymLinks MultiViews
    

    Last but not least my "secondDomain" file in "sites-avaiable"

    <VirtualHost secondDomain.com>
        ServerAdmin [email protected]
        ServerName  secondDomain.com
    
        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/user/www/secondDomain/htdocs/
    
        # CGI Directory
        ScriptAlias /cgi-bin/ /home/user/www/seoncDomain/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>
    
    
        # Logfiles
        ErrorLog  /home/user/www/secondDomain/logs/error.log
        CustomLog /home/user/www/secondDomain/logs/access.log combined
    </VirtualHost>
    

    I also get an error message, everytime I restart the apache server, saying that VirtualHosts xx.xx.xx.xx:80 overlaps with VirtualHosts xx.xx.xx.xx:80.

    When I call my secondDomain.com only the firstDomain.com pops up. It seems like the mapping is wrong.

    apachectl -t output:

    [warn] VirtualHost xx.xx.xx.xx:80 overlaps with VirtualHost xx.xx.xx.xx:80, the first has   precedence, perhaps you need a NameVirtualHost directive
    Syntax OK
    

    apache2ctl -S output:

    [Wed Feb 29 02:05:17 2012] [warn] VirtualHost xx.xx.xx.xx:80 overlaps with VirtualHost xx.xx.xx.xx:80, the first has precedence, perhaps you need a NameVirtualHost directive
    VirtualHost configuration:
    127.0.0.1:*           secondDomain.com (/etc/apache2/sites-enabled/secondDomain.com:1)
    xx.xx.xx.xx:443       firstDomain.com (/etc/apache2/ports.conf:12)
    xx.xx.xx.xx:80        firstDomain.com (/etc/apache2/httpd.conf:3)
    wildcard NameVirtualHosts and _default_ servers:
    *:80                   is a NameVirtualHost
         default server secondDomain.com (/etc/apache2/httpd.conf:11)
         port 80 namevhost secondDomain.com (/etc/apache2/httpd.conf:11)
         port 80 namevhost firstDomain.com (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost firstDomain.com (/etc/apache2/apache2.conf:241)
         port 80 namevhost secondDomain.com (/etc/apache2/apache2.conf:249)
    Syntax OK
    

    Wordpress is installed through apt-get. I have a symlink in /var/www/ which points to /usr/share/wordpress and activated in /etc/apache2/sites-available and a2ensite.

    • qweet
      qweet almost 12 years
      can you run apachectl -t and paste any errors you see?
    • cherrun
      cherrun almost 12 years
      edited post with apachectl -t output.
  • cherrun
    cherrun almost 12 years
    This fixed the error output, when calling apachectl -t, but somehow now the firstdomain.com maps to my wordpress install, which lies in www/wordpress, although that's not what I want. Therefore I changed the values back to the IP address. I think something is completely messed up with my settings.
  • ravi yarlagadda
    ravi yarlagadda almost 12 years
    Where's your wordpress configured? And what output do you get from apache2ctl -S?
  • cherrun
    cherrun almost 12 years
    edited first post with output.
  • ravi yarlagadda
    ravi yarlagadda almost 12 years
    @cherrun You have an three total vhosts that are configured to be firstdomain.com; the one in your httpd.conf file (which is empty by default in the Debian packaging that you're using) is getting precedence over the ones in apache2.conf and sites-available/default. Get rid of the copies that shouldn't exist, and set all port 80 vhosts to the correct host specification as outlined in my answer. You have vhost definitions sprinkled all over the place: ports.conf, httpd.conf, and apache2.conf, which is making things a lot more complicated than they need to be.
  • cherrun
    cherrun almost 12 years
    Which one is the correct file to write all the vhosts to? Is it alright that the ports.conf only have the Listen 80 and 443 option without specifying further vhosts?
  • ravi yarlagadda
    ravi yarlagadda almost 12 years
    Yes, that's all ports.conf is for. You can really choose however you want to set it up, but if you want to follow the Debian package's conventions, create a new file for each vhost within /etc/sites-available, then when you want to turn it on use a2ensite filename to make a symlink for it in /etc/sites-enabled.
  • ravi yarlagadda
    ravi yarlagadda almost 12 years
    If you change that, it will just use the one in /etc/apache2/sites-enabled/000-default instead. You should delete the duplicate host definitions first.