How to setup SSL on a local django server to test a facebook app?

Workaround to run https on django.

This can be done with stunnel that lets the Facebook server and stunnel on your machine communicate in SSL and stunnel turns around to communicate with Python in HTTP. First install stunnel. For instance in Mac OS X:

brew install stunnel

Then you need to create a settings file for stunnel to execute. You can create a text file anywhere. For instance, you can create dev_https and input:

pid=
cert=/usr/local/etc/stunnel/stunnel.pem
foreground=yes
debug=7

[https]
accept=8001
connect=8002
TIMEOUTclose=1

stunnel creates a fake certificate. By default on Mac, it’s at /usr/local/etc/stunnel/stunnel.pem. It’ll bring up a warning on your browser saying that your webpage can be fake but Facebook operations still work right. Since stunnel has to listen on one port and Python’s development server cannot run on the same server, you must use different ports for accept (incoming) and connect (internal). Once you have your dev_https file or whatever you called it, run

sudo stunnel dev_https

to start the tunnelling. Then start your Python server.

HTTPS=1 python manage.py runserver 0.0.0.0:8002

Environment variable HTTPS must be set to 1 for it to return secure responses and since we previously set the internal port to 8002, we listen on 8002 from all incoming IPs. Then, your IP:8001 can accept HTTPS connections without changing your webserver and you can continue running another instance of HTTP Python server on a different port.

ref: https://medium.com/xster-tech/django-development-server-with-https-103b2ceef893


With django-extensions you can run the following command:

python manage.py runserver_plus --cert certname

It will generate a (self-signed) certificate automatically if it doesn't exist. Almost too simple.

You just need to install the following dependencies:

pip install django-extensions
pip install Werkzeug
pip install pyOpenSSL

Now, as Ryan Pergent pointed out in the comments, you lastly only need to add 'django_extensions', to your INSTALLED_APPS and should be good to go.

I used a tunnel before, which worked, but this is much easier and comes with many other commands.


Short answer is you'll need to setup a proper webserver on your development machine. Use whichever one (Apache, nginx, cherokee etc) you're most familiar with.

Longer answer is that the django development server (manage.py runserver) isn't designed to do SSL etc and the effort to make it do so is likely greater than you'd want to spend.

See discussions of this passim on the django-users list: http://groups.google.com/group/django-users/browse_thread/thread/9164126f70cebcbc/f4050f6c82fe1423?lnk=gst&q=ssl+development+server#f4050f6c82fe1423


Not to necro a thread, but I found this tool to be extremely easy to use.

It's a premade django application with very simple install instructions.

You can add a certified key once it is installed simply by running:

python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key

I hope this helps any passer-by who might see this.