Why is setting up a subdomain for lighttpd so confusing...

With apache it is relatively easy but I am finding lighttpd to be extremely frustrating. I have found several sites including the linode library that explain how to set a vhost/subdomain up, but for the life of me I cannot do it. Maybe it's an awkward thing to explain but I'm finding documentation on this subject around the web to be lacking in clarity. As a result I am finding setting up a vhost in lighttdp to be frustrating and convoluted, in my novice experience of course.

I haven't yet registered my domain name, so I am still accessing my site via my IP address until it is all sorted. http ://myipaddy takes me to where I want to go. My cms however is located out side my web root and I would like to set up a subdomain for it i.e http ://subdomain.myipaddy.

Could someone please explain to me how I might get such a thing to work.

$HTTP["host"] == "subdomain.my_ip_addy" {
    server.document-root = "/path/to/subdomain/files"
}

The above doesn't work. I have also tried the Simple vHost setup located here, to no avail. I find it very confusing. My linode setup has gone largely without a hitch surprisingly for me and this one thing that should be quite straight forward has as taken the majority of my time to sort out. Any help would be greatly appreciated as this is just driving me insane now.

Final note (just popped into my head). If I wanted to keep the url of my cms more secure or secret, could a vhost be setup to take me to a domain that is something other than that of my main website. So instead of:

w ww.myDomain.com - main site
http://cms.myDomain.com - cms access point

I could have:

w ww.myDomain.com - main site
http://cms.myOtherDomain.com - cms access point

Or should I just restrict access to the cms to my PCs IP address?

9 Replies

@lew0810:

I haven't yet registered my domain name, so I am still accessing my site via my IP address until it is all sorted. http ://myipaddy takes me to where I want to go. My cms however is located out side my web root and I would like to set up a subdomain for it i.e http ://subdomain.myipaddy.
The way name-based virtual hosting works, you cannot access sites with different names/subdomains by typing an IP address into the browser's address bar. You need to fool your machine (the one you're sitting in front of, not your Linode) into thinking that the name(s) you're planning on giving it resolve to the Linode's IP address. For example, if your Linode's IP is 11.22.33.44 and you want to have sites hosted on it at foo.example.net and bar.example.net, you'd add the following line to your desktop machine's /etc/hosts file:

11.22.33.44    foo.example.net bar.example.net

Once you have DNS properly set up for the new site (or to access the "real" site with those names), you should delete or comment out this line.

> http ://subdomain.myipaddy

The Internet doesn't work that way.

@Vance:

The way name-based virtual hosting works, you cannot access sites with different names/subdomains by typing an IP address into the browser's address bar. You need to fool your machine (the one you're sitting in front of, not your Linode) into thinking that the name(s) you're planning on giving it resolve to the Linode's IP address.
Yup. Another way is to cheat totally and use "telnet", which may be sufficient to determine that the vhost functionality works

eg

 % telnet 1.2.3.4 80
Trying 1.2.3.4...
Connected to 1.2.3.4
Escape character is '^]'.
GET / HTTP/1.0
Host: virtual.host1

The blank line is important. Then you should get the home page for "virtual.host1" showing up. Do the telnet again with different Host: lines and you should get the other home pages.

(The web server just uses the Host: header to determine what virtual host to use; your web browser normally sends this).

sweh, wouldn't that have to be HTTP 1.1, not 1.0? I think it's 1.1 that allows you to specify the domain you're looking for, enabling name-based hosting.

Requests using HTTP/1.1 MUST provide a host header. HTTP/1.0 doesn't require it (but also doesn't deny it). I've not come across a web server which does Host: based virtual servers which doesn't work with the 1.0 header. There might be some out there, but I've not found one :-)

lighttpd, specifically, works just fine with 1.0 in the header line.

Thanks for your replies. I now have my domain registered, setup and showing me my site at example.com but I still can't get the subdomain to work:

$HTTP["host"] == "subdomain.example.com" {

server.document-root = "/var/subdomain/"

}

I'm just not getting it. Do all subdomains have to be located within the same folder as the public web root (/var/webroot/)? I'm still after something like http://subdomain.example.com

I've been doing some digging and found that simple-vhosts configuration doesn't occurr in the lighttpd.conf file but in the conf-available/10-simple-vhost.conf file instead. At least this is how it appears as every time I tried to configure simple-vhost in lighttpd.conf it said there was some sort of conflict when I tried to restart the server. Anyway this is what I have in conf-available/10-simple-vhost.conf:

simple-vhost.server-root = "/var/vhost/"

simple-vhost.default-host = "cms.example.com"

simple-vhost.document-root = "/"

My understanding of this is when cms.example.com is requested simple-vhost will search for the files within the server-root + default-host + document-root as so:

/var/vhost/cms.example.com/

At which point it should server the pages it finds. Is this correct as it still wont work?

What won't work, and how won't it work?

What exactly happens when you point your browser to your subdomain?

Do you get any errors? if so, what are they?

What do the log files for lighttpd show? are they showing access to the proper vhost?

Lighttpd is fairly simple to configure. Its not that hard.

Lighttpd is really simple to configure. My practice is to put each vhost into a separate file, e.g. in /etc/lighttpd/vhosts:

drwxr-xr-x 2 root root 4096 Aug 25 10:48 .
drwxr-xr-x 5 root root 4096 Jul  4 12:33 ..
-rw-r--r-- 1 root root 1239 Jul 29 12:27 ecample.com.conf
-rw-r--r-- 1 root root 1553 Jul  6 12:05 otherdomain.com.conf
-rw-r--r-- 1 root root 1023 Aug  2 10:49 anotherdomain.net.conf

and I have my last line in lighttpd.conf:

include_shell "cat /etc/lighttpd/vhosts/*.conf"

Then each vhost file contains something similar:

`$HTTP["host"] =~ "^(|www\.)example\.com$" {
    # redirect from www to non-www domain
    $HTTP["host"] =~ "^www\.example\.com$" {
        url.redirect = ( "^/(.*)" => "http://example.com/$1" )
    }
    server.name = "example.com"
    server.document-root = "/websites/example/content.example.com/"
    accesslog.filename = "/websites/example/log/example.com_access.log"

### custom vhost settings go here
    $HTTP[url] =~ "\.(png|jpe?g|gif|js|css)$" {
        expire.url = ( "" => "access 14 days" )
    }
    url.access-deny = ( "~", ".inc", ".myotherfiles-that-needs2be-denied" )
    #etc etc.
}`

I usually keep a 'template' for this vhost and tweak them for my needs. If I need something with my defaults, I just copy the template and replace all 'example' to 'mydomain' and it's done.

If I want to remove a vhost, I just simply rename the [hostname].conf to something like [hostname].conf.off and reload lighttpd.

Of course you need all your virtual hosted domains to point your server IP.[/url]

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct