Setting up a Django server with Gunicorn and Nginx - django

I've followed this tutorial and i'm running into an ERR_SSL_PROTOCOL_ERROR that i just can't figure out.
I must mention that i have set inside settings.py:
DEBUG = FAlSE
SECURE_SSL_REDIRECT = False
And my Allowed_hosts is ['*'].
I have a number of questions which reading alot of blog posts hasn't answered:
In the nginx configuration, if i'm running the server from a laptop connected to a router, and the external IP is 12.34.56.78 and the port is 50000, what am i supposed to put at server_name?
In the gunicorn configuration, everyone states that this is the config command: gunicorn --bind 0.0.0.0:8800 AWESOME.wsgi:application what IP should i put there in my case? 12.34.56.78:50000 or just leave it like it is?
As i understand the situation, given the setting in django settings.py my server is not serving HTTPS so the error does not come from that. I've also read about certbot but since i don't have even those 2 above questions figured out i cannot understand how to configure certbot..
EDIT
In fact i've tried to:
gunicorn --bind 12.34.56.78:50000 KYng.wsgi:application but i'm getting invalid address error

1.) You actually don't need to put anything in there, as it will default to an empty string.
2.) You should put localhost (127.0.0.1) if you want it to run locally, or 0.0.0.0 if you want it to run publicly.
Concerning the ssl error, check your nginx configuration more closely...that you didn't reference https instead of http somewhere...

Related

Plausible analytics on a server with a webapp

I have Django hosted with Nginx on DigitalOcean. Now I want to install Plausible Analytics. How do I do this? How do I change the Nginx config to get to the Plausible dashboard with mydomain/plausible for example?
Setup plausible by either running the software directly or in a docker container - let's say it runs on port 8080
Then in your nginx.conf - you should have a server block for your domain
Within that add a location block with the path you want plausible on and add a proxy pass directive to forward the requests to localhost:8080
Monitor access.log and error.log to debug any issues that may happen

Hosts file working for custom subdomains but not custom domains

I am developing a multi tenant app and need to modify my etc/hosts file to test the different URLs tenants might use. Some will use a sub domain like tenant1.mysite.com and others will use entirely their own URL like tenant2.com (to mask the fact it's a whitelabel site and pretend its their own).
In my hosts file I have:
127.0.0.1 mytenantsdomain.com
127.0.0.1 localhost
127.0.0.1 thor.localhost
127.0.0.1 potter.localhost
127.0.0.1 testtenant.com
localhost, thor.localhost, potter.localhost all work as expected when adding :8000 to them. i.e. they connect to the local development server and show expected tenant-specific content. But, mytenantsdomain.com and testtenant.com both give ERR_CONNECTION_REFUSED - I'm guessing its for the lack of the port :8000 tbh.
I have tried a few fixes like flushing the cache with the below but nothing has worked.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Anybody know what else I can try to get them all working?
For anyone coming to this later - after a bit more digging (on serverfault.com), the following lessons:
etc/hosts isn't the place for this. It just resolves a text domain
name to an IP. It's nothing to do with ports.
The solution was just to add :8000 after the .com - http://mytenantsdomain.com:8000/ resolves to the local dev server.
To make this permanent (i.e. not have to add :8000 to it), port forwarding would be the way to go. So you need a solution something like this: https://serverfault.com/questions/791181/redirecting-traffic-to-a-specific-address-and-port-using-pf-on-macos

How can I change django runserver url?

I'm trying to change django project url, so that users, who wants to connect to website in local area network will see url instead of localhost:8000 or 127.0.0.1. I need to change localhost:8000/users/board to be http://example.eu. I've tried to python manage.py runserver http://example.euand then thought about changing url's but it doesn't work. Also tried to change hosts file(Windows OS), but as far as I understand I need real ip address not url. Is it possible to do this thing and how?
You can use python manage.py runserver 0.0.0.0:8000. 0.0.0.0 means all IPv4 addresses on the local machine. So the server can be reachable by 127.0.0.1 and your private ip address like 10.10.5.8. So now others can access the server using http://10.10.5.8:8000. You the runserver on port 80, so that port can be removed from the url (by default is 80).
But to use any domain instead of ip, you have to change the hosts file of all the clients using the server to add domain to ip address mapping. Alternatively you can configure local network server to map the particular url to your system ip.
Run a local domain with the same port
Opening the /etc/hosts file on your mac with
sudo nano /etc/hosts
And for windows I believe you need to open:
C:\Windows\System32\Drivers\etc\hosts
In here add the domains you want, for example I added vazkir.com
....
127.0.0.1 localhost
127.0.0.1 vazkirtje.com
127.0.0.1 www.vazkirtje.com
.....
Lastly you can add it the domain to you ALLOW_HOSTS in your settings.py:
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "vazkirtje.com", "www.vazkirtje.com"]
And now you can visit your Django application (with port 8000) at:
http://vazkirtje.com:8000
The solution is not perfect since you do have to specify the port you are using, but this does let you use a domain for your local Django application in a relatively easy manner;)
Run a local non-existent domain without specifying the port
If you do want to run it without specifying the port; so just vazkirtje.com then you can use port 80, because this is the default port for HTTP.
Make sure the domain you are testing does not exist, since the domain lookup will first be done on existing domains. So check if you get a similar message to the one I got on chrome, when visiting the url:
Now you can specify this port by adding port "80" to the "runserver" command. You only do need to use "sudo" to run the command at this port, since you need admin rights for this. So run:
sudo python manage.py runserver 80
And now you should be able to access your Django application by visiting:
http://vazkirtje.com

uWSGI behind AWS application load balancer

I'm having trouble getting an ALB -> uWSGI container setup working in AWS. I want to leave nginx out of the stack if possible.
Assume security groups aren't an issue - I have confirmed ELB can reach the containers on the dynamically-allocated host ports.
From the uWSGI docs, --http is the way to go to make this work, but I must be missing something. Relevant ini:
[uwsgi]
socket = /tmp/uwsgi.sock
http-to = /tmp/uwsgi.sock
http = 0.0.0.0:8000
Is this correct? How should I configure uWSGI to receive http traffic from ALB?
Figured it out. Its actually http-socket that I needed. Uwsgi was indeed receiving traffic, but I was seeing this strange issue where the subdomain was being stripped off and the resulting site getting a 404.
For example, http://www.example.com was being immediately redirected to http://example.com and failing.
This was happening because of django, not uwsgi. Our subdomain.middleware was configured in such a way that a wildcard subdomain caused it to bail, and in doing so chop off that subdomain and redirect to http://example.com.
This was specific to our app, not uwsgi + django, but I thought I'd leave it here if it might move someone in the right direction.

Django app running in EC2, but trying to visit elastic URL returns page not found

I'm just starting out with EC2, and I've pulled down a git repo that I started on my local machine and so I know that it works running the server from there, and it seems to works when I run my server from the EC2 instance I have running, but for some reason, when I visit the elastic IP address of that instance I get a page-not-found. Any idea on why that might be?
So, I've now started using nginx, and made a conf file following the instructions here: https://code.djangoproject.com/wiki/DjangoAndNginx that is as follows:
server {
listen 80;
server_name ec2-54-242-149-154.compute-1.amazonaws.com;
access_log /var/log/nginx/USBag.access.log;
error_log /var/log/nginx/USBag.error.log;
location /basicMap/ {
alias /home/www/ec2-54-242-149-154.compute-1.amazonaws.com/basicMap/;
expires 30d;
}
location / {
include fastcgi_params;
fastcgi_pass 127.0.0.1:8080;
}
}
basicMap is a place that I have already defined in my django app, and the linked ec2 ip is the one my server is running on. I am having a lot of difficulty finding documentation on how to proceed or how to determine if my conf file is correct or not. Using the standard python manage.py runserver doesn't work however. Advice on how to proceed?
There is a lot of info about setting up a production django server out there, and I'll give you my personal preferences below, but before all that let's backup and see if we can just get any response from the production server.
To start the development server on your EC2 instance run:
manage.py runserver 0.0.0.0:8000
That command will cause runserver to bind to all interfaces and serve files to the external world. You'll never want to do this outside of development, but it is a good way just to test if your django app is setup before complicating things. Now try hitting your EC2 instance and see if you get a response.
If that's still not working, make sure you allow incoming connections to the server's port (8000 in the command above, 80 once live). You could test that you have ports open using netcat (nc -l).
Once you are satisfied that you have your app setup, I'd recommend you use nginx as your front end webserver and gunicorn as your django webserver in production. You'll likely want to look into setting up a virtualenv, supervisord etc for your production setup (here is a tutorial: http://senko.net/en/django-nginx-gunicorn/), but all that depends on the specifics of your project.