User avatar
Skraaj
Posts: 29
Joined: Wed Nov 20, 2019 11:35 pm
Contact: Website

[SOLVED] Apache2 multiple VHosts on LAN not working

Thu Jan 02, 2020 1:15 am

Hi,

I've been trying to set up multiple Virtual Hosts accessible only on my local network, this is for testing purposes before anything goes live. I don't want testing battleground to be visible from web and I've run into a brick wall. Vhost setup works like a charm when I use a domain name (live scenario) instead of an IP address on LAN, however, on LAN I'm stuck with IP and it doesn't want to cooperate.

Apache serves only the website from the first host and trying the second one returns "The requested URL was not found on this server" error. My question is -- is it possible to configure Apache2 with VHosts on LAN, and if so - what am I missing? I feel like it's some rookie mistake I'm making but not seeing.

Apache2 conf file:

Code: Select all

<VirtualHost *:80>
	#test
	ServerName 192.168.3.13
	ServerAdmin webmaster@test.eu
	ServerAlias 192.168.3.13
	DocumentRoot "/var/www/test.eu"

	ErrorLog ${APACHE_LOG_DIR}/test.log
	CustomLog ${APACHE_LOG_DIR}/test_access.log combined

</VirtualHost>

<VirtualHost *:80>
	#Forum@test
	ServerName 192.168.3.13/forum
	ServerAdmin webmaster@test.eu
	ServerAlias 192.168.3.13/forum
	DocumentRoot "/var/www/forum.test.eu"

	ErrorLog ${APACHE_LOG_DIR}/test_forum.log
	CustomLog ${APACHE_LOG_DIR}/test_forum_access.log combined

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Both directories exist and contain index.html, there are no typos in names. sudo apt-get update/upgrade done, fresh install. From what I understand Apache VHosts serve pages according to "ServerName" so "192.168.3.13" and "192.168.3.13/forum" should be treated as separate entities, right?
Last edited by Skraaj on Thu Jan 02, 2020 6:20 pm, edited 1 time in total.
codedoneright.eu – newbie friendly raspberry tutorials

knute
Posts: 589
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Apache2 multiple VHosts on LAN not working

Thu Jan 02, 2020 2:54 am

192.168.3.13/forum is not a fully qualified domain name so that won't work. Say your domain name is www.example.com and the IP is 123.123.123.123. You could have a server name of www.example.com and an alias of 123.123.123.123 and all of the documents served up whether with a http://www.example.com or http://123.123.123.123 would come from that same VirtualHost. You could however have another virtual host with 123.123.123.123 as a ServerName and point that to a different document root. What separates them is the host name that is sent from the browser.

User avatar
Skraaj
Posts: 29
Joined: Wed Nov 20, 2019 11:35 pm
Contact: Website

Re: Apache2 multiple VHosts on LAN not working

Thu Jan 02, 2020 4:00 pm

For now I've just placed /var/www/forum.test.eu in the subfolder of test.eu and I can access both. Not a great long-term solution, to put all websites in a subfolder, but at least it works for testing purposes.
knute wrote: What separates them is the host name that is sent from the browser.
I may have forgot to point out that I'm using a headless Pi and I'm working on it and accessing it with a different PC so if I type www.example.com it will literally search for www.example.com and would go to a live version of the page :) That's why I'm asking about configuring apache for local access so multiple sites will be available on LAN - from a different PC. If I were accessing those from the Pi itself I'd just modify /etc/hosts and would have used domain names.
codedoneright.eu – newbie friendly raspberry tutorials

knute
Posts: 589
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Apache2 multiple VHosts on LAN not working

Thu Jan 02, 2020 4:44 pm

If you can resolve the host names from your PC it will work fine. And yes adding a name to /etc/hosts works. You are going to have to have different host names for the different VirtualHosts no matter how you do it. One of those can be the IP address.

User avatar
Skraaj
Posts: 29
Joined: Wed Nov 20, 2019 11:35 pm
Contact: Website

Re: Apache2 multiple VHosts on LAN not working

Thu Jan 02, 2020 6:44 pm

knute wrote:
Thu Jan 02, 2020 4:44 pm
You are going to have to have different host names for the different VirtualHosts no matter how you do it. One of those can be the IP address.
Thanks for the help! :) Solved by modifying hosts on the PC I'm using to access Pi and by using dummy FGDNs used just for testing purposes. Out of curiosity Apache works ONLY with FQDNs?

--

In case anyone needs the solution (or if I forget it and need to get back to it):
  • On the PC that will be used to access the pi on LAN
  • Edit /etc/hosts (or equivalent on Windows)
  • Add the following "SERVER_IPADDRESS address_you_want_to_use" e.g. "192.168.0.2 test.testsite.com"
  • Add as many as needed for separate websites using the same IP e.g. second entry could be "1192.168.0.2 test.forum.testsite.com"
  • On the server
  • Set up Virtual Hosts as needed BUT modify "ServerName" so it reflects domains given above e.g. "test.testsite.com" for one and "test.forum.testsite.com" for the other
  • Restart Apache2
  • When moving server to live just modify Virtual Hosts in order to reflect actual domain you are using, hosts on the PC can remain unchanged so you can use it for further testing
  • "test" is added to the address so the PC used for accessing the server won't go to actual website that is live
/etc/hosts example

Code: Select all

127.0.0.1 localhost
192.168.0.2 test.testsite.com
192.168.0.2 test.forum.testsite.com
Apache2 Vhost example

Code: Select all

<VirtualHost *:80>
	ServerName test.testsite.com
	ServerAdmin webmaster@testsite.com
	ServerAlias www.test.testsite.com
	DocumentRoot "/var/www/test.com"
	ErrorLog ${APACHE_LOG_DIR}/test.log
	CustomLog ${APACHE_LOG_DIR}/test_access.log combined
</VirtualHost>
<VirtualHost *:80>
	ServerName test.forum.testsite.com
	ServerAdmin webmaster@testsite.com
	ServerAlias www.test.forum.testsite.com
	DocumentRoot "/var/www/forum.test.com"
	ErrorLog ${APACHE_LOG_DIR}/test.log
	CustomLog ${APACHE_LOG_DIR}/test_access.log combined
</VirtualHost>
codedoneright.eu – newbie friendly raspberry tutorials

hydra3333
Posts: 194
Joined: Thu Jan 10, 2013 11:48 pm

Re: [SOLVED] Apache2 multiple VHosts on LAN not working

Sat Jan 04, 2020 5:56 am

Mine isn't that fiddly, but it does work when being accessed either by IP or by hostname (not a full fqdn).

example code in here: https://github.com/hydra3333/Pi4CC

glad you got yours working.

Return to “Networking and servers”