[Django]-How to setup SSL on a local django server to test a facebook app?

8đź‘Ť

âś…

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

👤Malcolm Box

77đź‘Ť

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.

👤jdero

23đź‘Ť

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.

👤Mark

7đź‘Ť

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

👤mike

6đź‘Ť

I understand this has already been answered, but for a clearer solution:

Step 1: Install library

pip install django-sslserver

Step 2: Add to installed apps in settings.py

INSTALLED_APPS = [
    'sslserver',
    '...'
]

Step 3: Run the code using runsslserver instead of runserver. Certificate & key are optional.

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

4đź‘Ť

This doesn’t solve the automatic testing issue via

./manage.py test

but to run a server with HTTPS you can use RunServerPlus: http://pythonhosted.org/django-extensions/runserver_plus.html

Just install django-extensions and pyOpenSSL:

pip install django-extensions pyOpenSSL

and then run:

python manage.py runserver_plus –cert cert

👤Googol

1đź‘Ť

I’ve been able to setup ssl on django’s test server by using stunnel. Here is some info on how to set it up

Just a note, I wasn’t able to get it working using the package provided by debian in apt-get and I had to install from source. In case you have to do the same, please check out the excellent instructions debian forums on how to build debian packages.
There are plenty of instructions online and also on stunnel FAQ on how to create your pem certificate, but ultimately dpkg-buildpackage on Debian built it for me.

I would imagine that things could actually be more straight forward on Windows.

I then was able to make pydev in eclipse start the test server (and also attach to it) by adding a HTTPS=1 environment variable under “Debug Configurations” -> “Environment” -> Variables

0đź‘Ť

I got the same problem when wanna test Sign up using Facebook. After use django SSL Server from https://github.com/teddziuba/django-sslserver. This problem is solved. You may need it too.

0đź‘Ť

This discussion page is really old, earlier Django does not supported SSL, it needs to be done through stunnel or Werkzeug.
Django now supports SSL configuration with django-sslserver:
https://djangopackages.org/packages/p/django-sslserver/
Add in install app and pass certs in command line.

👤Rohit_chd

Leave a comment