Back to Blog

Running Django Development Server with HTTPS using Ngrok

K
Karan Goyal
--9 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

TL;DR

To test Django features that require HTTPS, such as payment gateways or OAuth authentication, you can use ngrok and django-sslserver to serve your Django application over HTTPS during development. This allows you to test these features locally, making your Django HTTPS development workflow more efficient. By following the steps outlined, you can expose your local server to the internet and access it from anywhere.

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.

Frequently Asked Questions

How do I generate self-signed certificates for Django HTTPS development?

You can generate self-signed certificates using the mkcert tool, which can be installed using Homebrew. Once installed, you can generate a certificate and private key for localhost using the mkcert command. This will allow you to serve your Django application over HTTPS during development.

What is the purpose of using django-sslserver in Django HTTPS development?

Django-sslserver is a package that allows you to run your Django development server with HTTPS. It uses the certificate and private key generated earlier to serve your application over a secure connection, making it ideal for testing features that require HTTPS. By installing django-sslserver, you can easily switch to HTTPS during development.

Can I use ngrok for Django HTTPS development to expose my local server to the internet?

Yes, ngrok can be used to expose your local Django development server to the internet, even when using HTTPS. By running the ngrok command, you can create a tunnel to your local server, allowing you to access it from anywhere. This is particularly useful for testing features that require external access, such as payment gateways or OAuth authentication, during Django HTTPS development.

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

📬 Get notified about new tools & tutorials

No spam. Unsubscribe anytime.

Comments (0)

Leave a Comment

0/2000

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