Back to Blog

Running Django Development Server with HTTPS using Ngrok

K
Karan Goyal
--2 min read

Learn how to serve your Django application over HTTPS during development using ngrok and django-sslserver.

Running Django Development Server with HTTPS using Ngrok

Introduction

When developing web applications with Django, it's often necessary to test features that require HTTPS, such as payment gateways or OAuth authentication. By default, Django's development server runs over HTTP, making it challenging to test these features locally. In this post, we'll explore how to serve your Django application over HTTPS during development using ngrok and django-sslserver.

Step 1: Generate Self-Signed Certificates

To serve your Django application over HTTPS, you'll need a certificate and private key. You can generate self-signed certificates using the mkcert tool. First, install mkcert using Homebrew by running:

bash
brew install mkcert

Then, install the root CA in your system's trust store:

bash
mkcert -install

Generate a certificate and private key for localhost:

bash
mkcert -cert-file cert.pem -key-file key.pem localhost 127.0.0.1

Step 2: Install django-sslserver

To serve your Django application over HTTPS, you'll need to install the django-sslserver package. Run the following command:

bash
pip install django-sslserver

Add 'sslserver' to your INSTALLED_APPS in settings.py:

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

Step 3: Run Django Development Server with HTTPS

You can now run your Django development server with HTTPS using the following command:

bash
python manage.py runsslserver

By default, runsslserver will use the certificate and private key generated earlier.

Step 4: Expose Your Local Server to the Internet using Ngrok

To expose your local server to the internet, you'll need to use ngrok. Run the following command:

bash
ngrok http https://localhost:8000

Ngrok will create a tunnel to your local server, allowing you to access it from anywhere.

Conclusion

In this post, we demonstrated how to serve your Django application over HTTPS during development using ngrok and django-sslserver. By following these steps, you can test features that require HTTPS locally, making your development workflow more efficient.

Tags

#Django#HTTPS#ngrok#django-sslserver#web development

Share this article

Comments (0)

Leave a Comment

0/2000

No comments yet. Be the first to share your thoughts!