Recently got hacked. How should I prevent it from happening again? (Django / Ubuntu / Postgres / Gunicorn / Nginx)

Posted May 27, 2015 7.3k views

So our website (clothing e-commerce startup) was recently attacked by an SYN flood initiated by a Chinese IP. It took almost a full day getting everything back up, reloading the database, copying images etc. - Needless to say, not a very pleasant experience and lost a lot of precious time.

Tech stack: Django 1.6.7/ Postgres / Nginx / Gunicorn / Elasticsearch

I want to know what all I can do to prevent something like this from happening again. Have already done the following steps, following the DO tutorials (they are great btw) at

  1. Updated all packages and downloaded security updates
  2. Set up SSH keys
  3. Disabled root login
  4. Changed SSH port to a random large number
  5. Setup UFW with disabling all incoming and outgoing connections except for the ports on which services are running (For Nginx, Gunicorn, Elasticsearch, Postgres and Redis)

Anything else I can do to prevent such a hack from happening again? Also do I need to install fail2ban given that root login is disabled and UFW rules are setup?

1 comment

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.

Submit an Answer
3 answers

I am not tech expert here but no matter how strong steps we take, if some one wants to hack, he/she or they will hack.. Heck even country specific security and intelligence agencies with highest level protection possible gets hacked..

Anyway, try CDN service as a layer of extra protection and services like cloudflare allows you to block specific region, like China for ex.

Manual via server rules, and firewall, you can block, but professional service like CDN services has much better database of such blocked IP or faster update as IP gets handed over from one ISP to another !!

Regarding security, best is a good backup cycle, with professional website like yours, top up with modular backup also..

So when things goes wrong, restore ASAP..

With CDN, even if server IP gets change, impact is minimal since Domain was and will continue to resolve to that CDN provider and its CDN provider’s end your IP details may get modified..

I agree with hunky, but there are many tools that may help you out a bit. I would highly recommend OSSEC Great write up on DO here It is an intrusion detection system, but has a couple of ips features like iptables dropping. This should stop brute force attacks. Also is you limit your root login to Digital Ocean to ssh keys that would almost certainly reduce the risk of hacking. That said make sure file and folder permissions are all good and search the web for other things to do. Web applications are usually the weak point in a hacking attempt as their surface area is much greater. Hope this helps! And good luck!

by finid
This tutorial will show you how to install and configure OSSEC to monitor one DigitalOcean server running Ubuntu 14.04 LTS. We'll configure OSSEC so that if a file is modified, deleted, or added to the server, OSSEC will notify you by email - in real-time. That's in addition to other integrity-checking features that OSSEC offers.

MicroPyramid sponsored Django-mfa is a simple package to add extra layer of security to your django web application. It gives web app a randomly changing password as an extra protection. Follow documentation and raise issues if you want further support, features.

Read more about multifactor authentication:
Get the code in our git repository,
For detail documentation visit