Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Apache2 virtual host configuration without dns?

Thu Jan 30, 2020 9:21 am

I want to use apache2 virtual host configuration without also setting up (dynamic) dns...

I like to set up apache2 on my RPi4 to serve a couple of named sites, but the howto:s I have found state that I must do the following to access them:
- Create the virtual host configs in sites-available and enable them in apache, then restart apache. (Check)
- Edit the /etc/hosts file to include an entry for the virtual hosts with the IP address set to the RPi IP address (Check?)
- If the sites are accessed from other devices, edit the hosts file also on them to suit. (???)

The problem with this is the need to edit the hosts file, especially on devices like phones and tablets (not available for editing).

Question
Is there some workaround for this or am I hosed?
I do not want to mess with dynamic dns systems or the like, makes it hard to maintain on multiple RPi:s.

Workaround?
Maybe the only way is to not use multiple virtual hosts but instead create a couple of webroot level directories, one per site, on a single host....

That would mean something like:

Code: Select all

/var/www/mysite.tst/public_html/site1
/var/www/mysite.tst/public_html/site2
/var/www/mysite.tst/public_html/site3
And the conf file would point to /var/www/mysite.tst/public_html/

This would be the only host running so the sites are accessed via url:s like http://<rpi-address>/site2/
I.e. all using the same IP address....

PS:
I have read that one should avoid using /var/www/html as the base directory since it is likely going to be modified if apache2 updates.
So therefore I have suggested the /var/www/mysite.txt dir above.
DS
Bo Berglund
Sweden

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Apache2 virtual host configuration without dns?

Thu Jan 30, 2020 9:33 am

How are you planning on setting the host names? Some home routers are able to assign names either manually or on what the machines self-report.

Another option would be port-based vhosts.

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Apache2 virtual host configuration without dns?

Thu Jan 30, 2020 10:54 am

tpyo kingg wrote:
Thu Jan 30, 2020 9:33 am
Another option would be port-based vhosts.
Excellent suggestion! :)
Did not think about that before...
The url itself would then contain a :portnumber part and then it would be all right!

The sites are for use and config of an RPi-based control system where the RPi must provide a WiFi Access Point for the user to connect to via WiFi.
I want to isolate the usage and config sites from each other and using different ports would solve the problem because the URL itself will direct the browser to one or the other based on the port entry.
Like http://192.168.111.1:81/ for the config site and http://192.168.111.1 for the use site.

I "just" need to figure out how to make the RPi4 use its WiFi to implement a WiFi AP so it can be connected to and interacted with.
There will be no Internet connection on this AP obviously so I hope the phones do not refuse to connect due to that....
Anyway that is a different topic.

Thanks for your suggestion, saved me a lot of time I am sure.
Last edited by Bosse_B on Thu Jan 30, 2020 2:06 pm, edited 1 time in total.
Bo Berglund
Sweden

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Apache2 virtual host configuration without dns?

Thu Jan 30, 2020 11:09 am

If you are setting the Raspberry Pi up as an IPv4 access point, then these instructions would help:

https://www.raspberrypi.org/documentati ... s-point.md

For your use-case, just skip the part about the packet forwarding and the Raspberry Pi will remain stand-alone.

However, since the above uses dnsmasq for DHCP you get DNS functionality as part of the deal. It is a matter of adding some lines to the dnsmasq configuration file:

Code: Select all

. . .
address=/site01.example.com/192.168.111.1
address=/site02.example.com/192.168.111.1
. . .
See the output from

Code: Select all

dnsmasq --help | less

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Apache2 virtual host configuration without dns?

Thu Jan 30, 2020 2:38 pm

tpyo kingg wrote:
Thu Jan 30, 2020 11:09 am
If you are setting the Raspberry Pi up as an IPv4 access point, then these instructions would help:

https://www.raspberrypi.org/documentati ... s-point.md

For your use-case, just skip the part about the packet forwarding and the Raspberry Pi will remain stand-alone.
Where is the packet forwarding part in the document you linked to?
Is it starting from "Add routing and masquerade" and then onwards?
However, since the above uses dnsmasq for DHCP you get DNS functionality as part of the deal. It is a matter of adding some lines to the dnsmasq configuration file:

Code: Select all

. . .
address=/site01.example.com/192.168.111.1
address=/site02.example.com/192.168.111.1
. . .
See the output from

Code: Select all

dnsmasq --help | less
At some point I will have to add Internet connectivity to this system, like connecting a USB mobile broadband router to the RPi4.
This is in order for it to be able to upload measurement results to a server.
Can the WiFi AP still be used in that situation or do I have to switch between the two work modes? Like unplugging the modem before connecting to the AP for interacting with its web server...
Bo Berglund
Sweden

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Apache2 virtual host configuration without dns?

Thu Jan 30, 2020 2:50 pm

Bosse_B wrote:
Thu Jan 30, 2020 2:38 pm
Where is the packet forwarding part in the document you linked to?
Is it starting from "Add routing and masquerade" and then onwards?
Yes. Doing just the first part will set it up as a wireless access point with the Raspberry Pi itself as the end point and no further connection onward to the net. That takes only a few minutes the second time, so I recommend giving it a try and experimenting a little.
Bosse_B wrote:
Thu Jan 30, 2020 2:38 pm
Can the WiFi AP still be used in that situation or do I have to switch between the two work modes? Like unplugging the modem before connecting to the AP for interacting with its web server...
Yes, you can set it up so that it is stand alone yet then have it connect to the net when you plug in an Ethernet cable or a USB modem. You can decide whether your chosen configuration also does forwarding (masquerading) of the wireless connections, or if the wireless stays separate. Again, experimentation is pretty easy on that end, too. Though you can get advanced options by playing with udev rules to launch specific scripts or services when plugging or unplugging the modem, and that would take some reading.

Return to “Raspberry Pi OS”