coisinhas
Posts: 2
Joined: Thu Aug 18, 2016 8:35 pm

Raspberry Pi WebServer Multiple .local domains

Thu Aug 18, 2016 8:41 pm

Hi,

I'm having a trouble with the Apache2 subdomains.

Well, I want to acess different websites from any local computer by using an URL like

website1.raspberrypi.local
website2.raspberrypi.local

I tried using the following:

Code: Select all

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
        ServerName website1.raspberrypi.local
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/website1/public

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
         <Directory /var/www/html/website1/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
         </Directory>
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

and then I run the command

a2ensite website1.raspberrypi.local

I also tried the:

a2ensite website1.raspberrypi.local.conf

and reloaded the server, but it does not work, does anyone have some idea?

Thanks

User avatar
DougieLawson
Posts: 36306
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi WebServer Multiple .local domains

Fri Aug 19, 2016 7:55 am

You'll need to run bind9 with your own zone file to do that.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Raspberry Pi WebServer Multiple .local domains

Fri Aug 19, 2016 8:59 am

coisinhas wrote:Hi,

I'm having a trouble with the Apache2 subdomains.

Well, I want to acess different websites from any local computer by using an URL like

website1.raspberrypi.local
website2.raspberrypi.local

I tried using the following:
The issue is that none of the machines will know what website1.raspberrypi.local or website2.raspberrypi.local are so won't connect to the pi. If you've only made the change in the apache config even the pi won't know where the sites are hosted.

the .local domain is a special one that only works on the local network and names are advertised via mdns/zeroconf/bounjour/ahavi (all are effectively different names for the same thing). You might be able to configure the service on the Pi that's responsible for advertising the raspberrypi.local name to the network to also advertise website1.local and website2.local (I'm not sure you can have website.raspberrypi.local via this method).

Alternatively you can setup a DNS infrastructure as DougieLawson indicates but that's a bigger task.

coisinhas
Posts: 2
Joined: Thu Aug 18, 2016 8:35 pm

Re: Raspberry Pi WebServer Multiple .local domains

Fri Aug 19, 2016 10:09 am

mfa298 wrote:
coisinhas wrote:Hi,

I'm having a trouble with the Apache2 subdomains.

Well, I want to acess different websites from any local computer by using an URL like

website1.raspberrypi.local
website2.raspberrypi.local

I tried using the following:

The issue is that none of the machines will know what website1.raspberrypi.local or website2.raspberrypi.local are so won't connect to the pi. If you've only made the change in the apache config even the pi won't know where the sites are hosted.

the .local domain is a special one that only works on the local network and names are advertised via mdns/zeroconf/bounjour/ahavi (all are effectively different names for the same thing). You might be able to configure the service on the Pi that's responsible for advertising the raspberrypi.local name to the network to also advertise website1.local and website2.local (I'm not sure you can have website.raspberrypi.local via this method).

Alternatively you can setup a DNS infrastructure as DougieLawson indicates but that's a bigger task.
Thanks for your answer, I know I can change the /etc/hostname to have a website1.local, but I'm only able to have one hostname each time.

Also I tried in the /etc/hosts this

Code: Select all

127.0.0.1 raspberrypi    website1    website2 
But nothing happened

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Raspberry Pi WebServer Multiple .local domains

Fri Aug 19, 2016 11:15 am

coisinhas wrote: Thanks for your answer, I know I can change the /etc/hostname to have a website1.local, but I'm only able to have one hostname each time.

Also I tried in the /etc/hosts this

Code: Select all

127.0.0.1 raspberrypi    website1    website2 
But nothing happened
/etc/hostname is just the name of the local device which doesn't have to be anything to do with sites it hosts.

/etc/hosts is used for local host lookups so putting that line on your pi will let you ping those names on the pi but don't go any further.

To get the names visible elsewhere you'll need to push them into the ahavi/zeroconf/mdns service config. I'm not sure how you can do that as it's not something I've used (I have a full dns setup)

Peter Ryan
Posts: 39
Joined: Sat Mar 03, 2012 12:44 am
Location: UK

Re: Raspberry Pi WebServer Multiple .local domains

Sat Aug 20, 2016 9:04 pm

Before I start, I'll just say that I don't have the answers for you - but hopefully I can help you better understand the problem!

When you enter a URL into your browser, e.g. http://website1.raspberrypi.local/foo/bar.txt, the browser splits the URL up into protocol (http), hostname (website1.raspberrypi.local), and path (/foo/bar.txt). The protocol is basically the communications method, but we can ignore that.

The hostname it needs to translate into an IP address, which it typically does by first trying to resolve it locally - by looking at the /etc/hosts file, and then by asking something upstream which for most people is their router, and this in turn will ask their ISP which will then ask something else "some where in the cloud". Since this is a local address, and you won't have a public domain name set up, that's not going to help.

In the /etc/hosts file, you can have as many entries as you like:

Code: Select all

127.0.0.1         website1.raspberrypi.local
127.0.0.1         website2.raspberrypi.local
127.0.0.1         subdomain.website2.raspberrypi.local
Once the web browser has resolved the host-name to an IP address, it can then open communications with that computer (127.0.0.1) and request the path (/foo/bar.txt) from it. However, it will also specify the host-name so that the web server (Apache) will know which virtual host to use if it's hosting multiple sites. A simplified request will look like this:

Code: Select all

GET /foo/bar/txt HTTP/1.1
Host: website1.raspberrypi.local
So at this point, you should be able to get your websites to serve locally on your Pi? I notice that you did both "a2ensite website1.raspberrypi.local" and "a2ensite website1.raspberrypi.local.conf".... which leads me to wonder if either or both of these commands responded with an error?

You should have something like:

Code: Select all

$ sudo a2ensite website1.raspberrypi.local.conf
Enabling site website1.raspberrypi.local
To activate the new configuration, you need to run:
  service apache2 reload
And your Apache host configuration file should be at /etc/apache2/sites-available/website1.raspberrypi.local.conf

You can test Apache on your Pi from the command line using something like:

Code: Select all

echo -ne "GET / HTTP/1.1\r\nHost: website1.raspberrypi.local\r\n\r\n" | nc 127.0.0.1 80
Often people recommend using Telnet for basic testing, but I like using echo piped into nc (netcat) because it allows you to type and edit the HTTP request (the text beginning GET in-between double quotes) before communicating; with telnet, it can sometimes timeout, and it's annoying if you make a mistake and need to edit text. The \r is a carriage return character, and \n is a line-feed which the HTTP protocol requires.

So the above echo/nc command will directly request the home page from website1.raspberrypi.local using the local address of 127.0.0.1 and this will work even without getting the domain name in the /etc/hosts file.

If this isn't working yet, you need to get that solved first not least, because that's the easy bit!

The bigger problem you've got, as pointed out by DougieLawson and mfa298, is getting the domain name "website1.raspberrypi.local" resolved to the address of your Pi. On your Pi, you can deal with this by adding "127.0.0.1 website1.raspberrypi.local" to your /etc/hosts file. Then if you use a web browser running on your Pi, it will be able to resolve the host-name to an IP address.

But 127.0.0.1 is a special local address that even if you could get that onto other devices on your network, still won't resolve to the Pi. Your Pi has likely been allocated a local network address by a thing called a DHCP server that is likely running on your router. That address is probably begins 192.168.something.something. You can find the IP address that your Pi is using using the command ifconfig.

One problem is that this address might change the next time you power your Pi off and on again. So you may need to investigate configuring a static address... but this might be as simple as setting it up on your router.

Next up, somehow, you need to let other devices on your network know that the host-name "website1.raspberrypi.local" resolves to 192.168.something.something. Typically that is done using a DNS server such as bind9 as suggested by Dougie. However, that will lead to then needing to get all devices on your network to use your DNS server to resolve IP addresses.

The alternative to that is using Zeroconf which allows devices to advertise available services on the local network, without needing an administrator to configure it. However, I'm not too familiar with it myself so I can't help you there! The linked wikipedia page is pretty good at explaining it though. The linux implementation, avahi, is installed on Raspbian by default so may be worth investigating.

So... that's a long post to not give you a solution, but hopefully it helps explain the problem. ;)

Return to “Advanced users”