How to run Django Web app on port 443 and 80 linux - Ubuntu or Raspberry PI?

Viewed 4204

I am pretty new to Django. I think I cannot run django application as sudo since all pip related modules are installed for the user and not for the sudo user. So, it's a kind of basic question like how do I run django app that can listen for port 80 as well as port 443.

So, far I have tried following option - i.e pre-routing - NAT

I run my app using the following command -

$python manage.py runserver 
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

September 13, 2018 - 03:04:41
Django version 2.1.1, using settings 'WebBlogger.settings'
Starting development server at http://127.0.0.1:8000/

Next, here is my iptables settings nothing worked for me though

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

$sudo iptables -t nat --line-numbers -n -L
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
2    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 8000

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           
2    RETURN     all  --  192.168.122.0/24     224.0.0.0/24        
3    RETURN     all  --  192.168.122.0/24     255.255.255.255     
4    MASQUERADE  tcp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
5    MASQUERADE  udp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
6    MASQUERADE  all  --  192.168.122.0/24    !192.168.122.0/24    

Chain DOCKER (2 references)
num  target     prot opt source               destination         
1    RETURN     all  --  0.0.0.0/0            0.0.0.0/0           

I did http:// and I see connection refused. I have no idea how to debug the NAT stuff whether it is actually hitting NAT or not. How can I debug and also what is the correct solution to it?

2 Answers

In ideal world you will need a web serve to talk to your Django.

Web server (port 80/443) -> gunicorn (wigs) -> Django (port 8000)

And if you just want Django development server to run on 80 then try

python manage.py runserver 0.0.0.0:80 

And make sure no other process is using port 80.

You do not talk to a Django app directly. While Django has a simple development server available via the runserver command, it is not meant for anything but development work.

What you want is to setup a WSGI server to run your app and a web server to accept actual user requests and proxy them to the WSGI. The commonly used WSGI servers (don't worry about what WSGI is) are gunicorn and uWSGI. Both can be installed using PIP and you don't have to install them as the same user as your app. Gunicorn is a bit easier to use, so I would recommend that one. The most common web server nowadays is Nginx.

Also, you should pack your application into a virtual environment, so you can pack it together with all the dependencies without relying on a specific system having everything installed.

Here is a somewhat dated guide on how to do this. It should be mostly accurate though and is a good place to start https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04

Related