0
votes

I've been trying to get my head around this all night with no success no matter how many tutorials and resources I've looked through.

My aim is to be able to go to two different websites on the one running apache server.

End goal is something like this(using local ip);

192.168.1.8/site1 -> first site 192.168.1.8/site2 -> second site

I have included the following in my httpd.conf

<VirtualHost *:80>
        DocumentRoot "/root/sites/site1"
        ServerName site1
</VirtualHost>
<VirtualHost *:80>
        ServerName site2
        DocumentRoot "/root/sites/site2"
</VirtualHost>

Its worth mentioning that the default DocumentRoot and Directory settings are still in the http.conf and actually point to the site1 files.

Going to 192.168.1.8 takes me to site1 (guessing from the DocumentRoot settings in the http.conf)

Going to /site1 or /site2 both give me a 404 Not Found

I've run -S to see any problems but it looks good as far as I can tell;

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server site1 (/usr/local/etc/apache24/httpd.conf:244)
         port 80 namevhost site1 (/usr/local/etc/apache24/httpd.conf:244)
         port 80 namevhost site2 (/usr/local/etc/apache24/httpd.conf:248)
ServerRoot: "/usr/local"
Main DocumentRoot: "/usr/local/www/apache24/data"
Main ErrorLog: "/var/log/httpd-error.log"
Mutex default: dir="/var/run/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www" id=80
Group: name="www" id=80

Can anyone spot anything obvious that I have missed?

1

1 Answers

0
votes

NameVirtualHosts are intended to be accessed by name, not by IP address. With your configuration, you've told Apache to listen for incoming connections to the hostnames "site1.example.com" and "site2.example.com" (substitute your own domain for "example.com")

Without a name to distinguish them, you can't have two virtual hosts listening to the same IP address and same port. When you go to http://192.168.1.8 , Apache has no way of knowing whether you're intending to reach site1 or site2. So it sends you to the first host that matches, which is the first <VirtualHost *:80> in your configuration. Then it takes your request for /site1 and appends that to the DocumentRoot of the first VirtualHost. But since you don't have a /root/sites/site1/site1 directory, you get a 404 error.

There are a couple of alternatives.

You could run the two VirtualHosts on two different ports.

<VirtualHost *:80>
    DocumentRoot "/root/sites/site1"
    ...
</VirtualHost>

<VirtualHost *:8080>
    DocumentRoot "/root/sites/site2"
    ...
</VirtualHost>

Or, you could simply have a single site with a single document root:

DocumentRoot "/root/sites"

and then just structure your filesystem so that content for site1 goes under /root/sites/site1 and site2 under /root/sites/site2. Then the URLS http://192.168.1.8/site1 and http://192.168.1.8/site2 will work automatically.

You might also be able to do make some mod_rewrite rules, but that would start to get more complex.