Django + https on localhost - django

I have files of TLS certificate(.crt) and key(.key) How can i add them in settings.py and run localhost with https( it is need to add them as text variables not path to these files)? Thanks in advance!

Django runserver does not implement HTTPS and it was not meant to be used in production anyway. There is a package that works similarly with support for SSL (django-sslserver) but rather than using it I would consider setup of NGINX or Apache similar to what could be used in production.

Related

Is it a good practice use Django integrated web server behind a proxy?

I implement a Django website behind a NGINX configured as reverse proxy which serve also the static for Django when it is out of debug mode, all this is in a docker-compose app.
I know by read the Django docs they do not recommend to use integrated web server in a production environment (and it is not at the moment), put it behind a reverse proxy is acceptable to avoid security issue or it is the same as expose it directly?
If it is not an acceptable solution, could you suggest any implementation to not use the Django integrated web server?
Consider that the container structure like the following:
Proxy is the NGINX Official image
www1 & www2 are Python3 Official image with Django installed as described here.
database is the Postgres Official image.
Only the Proxy container is exposed to the external world.
Thanks.
I get my answer, I gonna use gunicorn instead the integrated Django web browser.
I had use documentation present here That describe how to configure gunicorn and nginx on the same host, but using http instead linux socket, instead the command to run the Django integrate web server I have just to run gunicorn like this:
gunicorn --workers=4 --bind=0.0.0.0:9000 --access-logfile - --error-logfile - --log-level debug myapp.wsgi:application
with the previous command I also get logs managed by container.
Off course I also add gunicorn in the requirement file.
I hope this question will help also some one else.

Django runserver giving a 301 error

I'm trying to setup a development version of a Django site (developed by someone else) so that I can make edits and test before putting changes live. I'm getting a HTTP 301 error when I try going into 127.0.0.1:8000.
I've tried other ports to ensure I'm not already using it.
I believe the settings should allow me to run this app, for example I've got:
DEBUG = True
ALLOWED_HOSTS = ['*']
The live site is running from HTTPS so I figure there is something within settings.py which is specifying SSL but I cannot find anything.
Two questions:
Is there any way to emulate HTTPS using runserver?
If not, what should I be looking for which might be forcing HTTPS?
I never managed to get this running using runserver. In the end I set it up on a apache server and it ran fine

how to configure nginx for all error handling when using with Django and Gunicorn?

If I am using Nginx, Django, and Gunicorn, how to use Nginx to handle all errors with static contents, e.g. 404, etc?
Reason) If for some reasons, Django or Gunicorn crashes (and either of them won't come back up), normally error content handled by Django cannot be served anymore, how to configure nginx to handle the error properly?
Thank you.
Check this blog:
Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL
Has great content for setting up Django on production using gunicorn & nginx. Also covers setting up static and media files.
Might help :)

Running Django on Apache Behind nginx - What Apache optimizations can I make

I have a configured and running setup that I am looking to optimize. I do not want to swap out Apache for gunicorn or other options at this stage.
My setup is so:
Ubuntu 11.04
Default nginx from apt-get
Default apache from apt-get
Nginx serves static files, and passes application requests through to Apache. Apache will have between 5-8 Django projects (ie - distinct websites). Small to medium traffic. Apache only has django projects (served via mod_wsgi) - I don't need php or anything that Django does not need.
From the default Ubuntu/Apache, what mods can I disable, and are there any other configuration tweaks I can do to more optimally use resources on my machine.
One configuration tweak is that, if you don't need apache to serve anything else than django sites, ditch apache completely.
Use a dedicated WSGI server like uwsgi ( http://projects.unbit.it/uwsgi/ ) or gunicorn ( http://gunicorn.org/ ). They are well documented and so low on resource usage.
You can use WSGIOptimize option to make all your .py files into .pyo. You can also use Memcached to enable cache. This blog describes how to run several django sites in one daemon. It is said to be useful in case where you need to serve a lot of sites, each with low traffic. This contains tips from Jacob Kaplan-Moss.

Django: Restarting Shared Server When Changing MVC Python Files

Essentially,
I am doing some of the development for my Django app straight on my shared server rather than on a local machine (I know that initially is the problem) however regardless, I would like to know if there is a way to restart the apache server on my shared hosted server (Bluehost) for when I modify any of the python files.
I'm not sure how the Django framework is setup, maybe restarting the webserver is not needed?
THanks!
How is the apache configured to run python codes? are you using fcgi, wsgi or something else?
What i do is, i use a fcgi process and restart fcgi process without the need to restart the webserver. In my case web server is nginx but it should be same in apache if you are using fcgi.