Running multiple virtual hosts
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:
- Move firstDomain.com virtual host declaration to sites-available/firstDomain.
- Remove everything from httpd.conf (you may try to define ServerName in apache2.conf)
- Ensure that you have sites-available/firstDomain and sites-available/secondDomain symlinked to sites-enabled/firstDomain and sites-enabled/secondDomain respectively.
Related videos on Youtube

cherrun
Updated on September 18, 2022Comments
-
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 almost 12 yearscan you run
apachectl -t
and paste any errors you see? -
cherrun almost 12 yearsedited post with apachectl -t output.
-
-
cherrun almost 12 yearsThis 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 almost 12 yearsWhere's your wordpress configured? And what output do you get from
apache2ctl -S
? -
cherrun almost 12 yearsedited first post with output.
-
ravi yarlagadda almost 12 years@cherrun You have an three total vhosts that are configured to be
firstdomain.com
; the one in yourhttpd.conf
file (which is empty by default in the Debian packaging that you're using) is getting precedence over the ones inapache2.conf
andsites-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
, andapache2.conf
, which is making things a lot more complicated than they need to be. -
cherrun almost 12 yearsWhich 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 almost 12 yearsYes, 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 usea2ensite filename
to make a symlink for it in/etc/sites-enabled
. -
ravi yarlagadda almost 12 yearsIf 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.