Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm running the Django webserver locally, and I want be able to enter www.my-site.dev or username.my-site.dev and have that map to my Django application.

I've seen a lot of solutions for Apache, and have tried to add the following to my /etc/hosts file, assuming the Django server would also check /etc/hosts.

127.0.0.1:8000 www.my-site.dev  
127.0.0.1:8000 username.my-site.dev  
share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

A few things

1) Django probably isn't referencing your hosts file. ( I say probably, because I don't use Django, but it doesn't make sense for it to reference the hosts file as it shouldn't need to do a lookup on itself).

2) The hosts is for local name resolution. It has nothing to do with ports. If you want those two sites to resolve to your local machine, you should have this in your hosts file.

127.0.0.1     www.my-site.dev    username.my-site.dev  

You can then access the site via your web browser by going to

www.my-site.dev:8000

The only way for you to not have to type in the 8000 is for you to make Django listen on port 80 (or providing you are doing SSL, 443). This should be configurable in Django, and shouldn't cause any issues (unless you are already running something that is using port 80).

share|improve this answer
    
You can't have a duplicate 127.0.0.1 as these have to be unique. Instead, append hostnames space-separated on a single line. –  gertvdijk Jan 27 '13 at 0:27
3  
@gertvdijk no they do not. You can have multiple 127.0.0.1, you however cannot have multiple www.my-site.dev. It's the second part that has to be unique (since it wouldn't make sense to point a DNS address at two servers) –  tgm4883 Jan 27 '13 at 0:29
    
@tgm4883: THANK YOU!! Been beating my head against the wall on a bunch of different stuff today, so this was a big relief. –  Brian Dant Jan 27 '13 at 0:45
    
@tgm4883 They really do in Linux. See man hosts(5): one line per IP address. Not all software can cope with the duplicate IP address entries as you've just proposed. –  gertvdijk Jan 27 '13 at 0:47
    
@gertvdijk While I agree that it does say "one line per IP address" I'm interpreting that a different way than you are I guess. (since it doesn't say anything about being unique). I'll change my answer though since it should work either way. –  tgm4883 Jan 27 '13 at 1:15
show 1 more comment

There are a few problems here that I will outline. Others have already gone some way to explain them but as Django developer, I can add some overlooked issues:

  • Don't misunderstand the relation between servers, browsers and DNS. The browser needs DNS to look up a name and get an IP address to connect to. The server doesn't give a flying banana which names points to its IP.
    Note: The httpd will, but not for connection purposes — it uses it to host multiple virtual hosts on one IP.
  • /etc/hosts can point a domain name to an IP but that's it. They can't specify port 8000. That's the browser's job.
  • To host something on port 80, you need to run it as root, redirect port 80, or use setcap to allow Python to hog port 80. The last two are very hacky but they're infinitely better than running a Django dev server as root. Please never ever do that.
  • Hosting multiple Django dev servers on one IP's port 80 is impossible. They will all try to greedily bind to it and disallow further binds. You either have to stagger ports, or stick a httpd/reverse-proxy in front to split virtual hosts to Django servers.

For development I just load up the dev-server as and when I need it. I only run one at a time and it runs on the default 127.0.0.0:8000. If that model suits you and you just want to host on a custom domain name, just something like this to your /etc/hosts:

127.0.0.1 my.domain.ext sub.my.domain.ext

You can just keep chaining them on but remember that this is going to override all outbound traffic for requests on those domains. Aka: don't forget you've hacked your own DNS! From there you just load http://my.domain.ext:8000 and you're looking at your dev server.

If you want http://my.domain.ext, you're going to have to either hack things up (see above) or move to a more traditional infrastructure (below).


If you need to run multiple servers, I can really only suggest running a proper stack. I would run nginx + uwsgi + virtualenv stack. Something like what you'd use in production. In fact, the closer you can mirror your production environment, the better. If you're using Apache and modwsgi, do that.

This gives you a better testing platform. If you need to hook in on debug, I find setting uwsgi up to log (and monitoring the log) a suitable replacement for a live console output.

share|improve this answer
add comment

I'm no expert, but it sounds like you need a DNS Server. Otherwise, your browser will check the world wide web to try to find a www.my-site.dev, and will not find your local machine unless you set up a DNS server ans set up any clients accessing www.my-site.dev to use the dns server that you set up. Then the DNS Server will point the client to your local machine. Ignore me if I am wrong :)

share|improve this answer
    
Name resolution goes hosts file -> DNS server, so if an address is found in the hosts file it will not contact DNS to try and resolve the name. –  tgm4883 Jan 27 '13 at 0:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.