29

I've configured my local machine's HOSTS configuration to access the local server ( @ 127.0.0.1 ) whenever I hit http://www.mydomain.com on the browser.

And I was using this to interact with facebook's graph api to build my app. But now facebook requires us to have an HTTPS url or rather an SSL secured url to interact with their api.

So the question is -> How do I setup SSL on a local django server ?

Sussagittikasusa
  • 2,525
  • 9
  • 33
  • 47

9 Answers9

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
  • 1,797
  • 3
  • 21
  • 36
  • 4
    Dude, this is awesome. Obviously, setting up self-signed certs is the way to go,but this is easy as hell. And it does just that: sets up self-signed certs. Damn, wish I could give you +2 ;-) Thanks. – nicorellius Feb 08 '14 at 06:34
  • 1
    This solution woked well for me. I just had to do **pip install django-sslserver** , then added **'sslserver'** to INSTALLED_APPS, then run **python manage.py runsslserver** and boom! I had my (https://localhost:8000) up in no minute. Didn't have to provide path to certificate. Thumbs up – manpikin Oct 08 '19 at 12:18
  • Great solution. Worked for me like a charm – Junaid Nov 02 '19 at 03:40
  • Any thoughts on how to run this during testing? – Brian Dolan Dec 28 '21 at 17:57
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.

Tom Connery
  • 110
  • 2
  • 8
Mark
  • 18,730
  • 7
  • 107
  • 130
  • 3
    Important detail: you have to add 'django_extensions' to your INSTALLED_APPS for this to work – Ryan Pergent Apr 24 '18 at 09:43
  • 1
    (Thanks @ihhcarus for the edit, as a note, "stunnel" was not a typo, it's the name of a library for creating tunnels). – Mark Apr 24 '18 at 10:33
  • 1
    inside the project folder, where there is __init__.py for the project, there is settings.py file. Inside this file, there is a list of name INSTALLED_APPS. add 'django_extensions' here. Thank you for this very helpful answer! – Anchal Agrawal Feb 20 '19 at 16:09
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
  • 3,968
  • 1
  • 26
  • 44
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

ABN
  • 1,024
  • 13
  • 26
mike
  • 187
  • 3
  • 8
  • 4
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Antony Jan 16 '14 at 02:23
  • Thanks for the suggestion, added the essential parts here. – mike May 22 '15 at 18:29
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
  • 3,466
  • 1
  • 17
  • 31
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
  • 2,815
  • 2
  • 22
  • 13
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

Ivan Alagenchev
  • 552
  • 6
  • 13
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
  • 101
  • 1
  • 1
  • 6