Question

What are some best practices for securing a Django application on a DigitalOcean Droplet?

I know that I can run Django on the DigitalOcean App Platform, but for my current use-case I have to use a Droplet. Can someone share some tips on what I need to keep in mind when running my Django on a Droplet?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
October 4, 2024

Heya,

To Summarize all that has been said -

  1. Always keep your Django application and in fact all software up to date. This will prevent any known exploits.
  2. Set the following in your settings.py file once you go to production:
  • Set DEBUG = False for production.
  • Specify your domain(s) in ALLOWED_HOSTS.
  • Enable SECURE_SSL_REDIRECT = True to force HTTPS.
  • Set CSRF_COOKIE_SECURE = True and SESSION_COOKIE_SECURE = True for secure cookies.
Bobby Iliev
Site Moderator
Site Moderator badge
October 2, 2024

Hey there!

Great question! Securing a Django app on a DigitalOcean Droplet is essential to ensure your app stays safe and performs well. Here are some best practices to get you started:

  1. Regular updates for your OS, Django, and any dependencies are critical for security. Run the following to make sure everything is up to date:

    sudo apt update && sudo apt upgrade
    
  2. Configure a firewall with ufw to allow only necessary traffic, like SSH and HTTP/HTTPS. You can set it up with these commands:

    sudo ufw allow OpenSSH
    sudo ufw allow 'Nginx Full'
    sudo ufw enable
    

    For more details on using ufw, refer to this guide. Alternatively you could also use a Cloud Firewall.

  3. SSL certificates are critical for encrypting traffic to your site. Let’s Encrypt makes it easy to set up SSL for free. You can install Certbot and configure Nginx for HTTPS:

    sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx
    

    Detailed instructions are available in this tutorial.

  4. In your settings.py, make sure the following settings are enabled:

    • Set DEBUG = False for production.
    • Specify your domain(s) in ALLOWED_HOSTS.
    • Enable SECURE_SSL_REDIRECT = True to force HTTPS.
    • Set CSRF_COOKIE_SECURE = True and SESSION_COOKIE_SECURE = True for secure cookies.

    For more on Django’s security settings, check this guide.

  5. For better security, reliability, and ease of management, consider using DigitalOcean’s Managed PostgreSQL service. This eliminates the need to manage backups, updates, or scaling manually.

    You can learn more about the benefits of managed databases here.

  6. DigitalOcean offers built-in backups for Droplets. Enable automatic backups under the “Backups” tab in your Droplet’s dashboard. This ensures you have recovery options in case of a disaster.

    More info on backups can be found here.

  7. Keep an eye on your logs for suspicious activity, and use fail2ban to block malicious login attempts. It monitors logs for failed SSH login attempts and automatically bans IPs.

    Follow this guide to set up Fail2Ban.

  8. Set up Gunicorn as the WSGI server to serve your Django app, and use Nginx as a reverse proxy. This improves the performance and security of your application. You can follow the steps outlined in this DigitalOcean tutorial.

  9. Make sure Django’s security middleware is enabled. The SecurityMiddleware and XFrameOptionsMiddleware protect against common web vulnerabilities like clickjacking and cross-site scripting (XSS).

    Add them to your MIDDLEWARE settings to add these extra layers of security.


If you’re just getting started or want a quicker setup, consider using the Django 1-Click Installation from the DigitalOcean Marketplace. It automatically configures Django, Gunicorn, Nginx, and PostgreSQL, making deployment faster and easier.

Let me know if you need further clarification or have more questions!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.