By psmod2
Hello,
I’ve recently moved from Heroku to a droplet running Dokku which runs my node.js web app.
I’d like to ask what security suggestions I should follow to minimise any risk of getting hacked.
As its a web app, I’m a little unsure as it needs to be accessible to the world - but how to I secure.
So far:
Any help and advice would be appreciated.
Thanks!
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!
Accepted Answer
The first steps I normally perform are updating the packages and upgrading current packages to make sure everything that’s default is up to date.
sudo apt-get update \
&& sudo apt-get -y upgrade
Once that’s done, I’ll handle setting up a new user, add them to the sudo group, and test that the user is able to actually use sudo
to escalate and run commands that would otherwise require root
.
If all is well there, then I’ll setup firewall rules. Since you’re using Ubuntu, this is super easy. We can use ufw
and ultimately, we would setup a default policy to deny any connection that we don’t allow through with rules we’ll add afterwards.
So, first we should make sure ufw
is not enabled.
sudo ufw status
If it returns enabled
, run sudo ufw disable
, otherwise well go ahead and setup our default deny.
sudo ufw default deny
Now, to setup our rules, we need to figure out what ports we need open for public access. We need at least SSH, HTTP, and HTTPS.
sudo ufw allow 22/tcp \
&& sudo ufw allow 80/tcp \
&& sudo ufw allow 443/tcp
We’re using /tcp
to define the connection type. This means that we only want to allow connections on these ports using TCP (not UDP).
Beyond SSH, HTTP, and HTTPS, you would also need to add any rules to cope with connections on other public ports. So, for example, if you needed remote database access to MySQL, you’d have to open that port up as it’d be blocked with just the above rules in place.
Once all rules are set, we then need to enable the firewall.
sudo ufw enable
It’ll ask you if you really want to enable and that enabling may cause your current connection to drop. Type y
and hit enter. As long as SSH has been setup (as shown above), the connection won’t drop.
Beyond a Firewall, there’s also fail2ban
, which is useful. Setting it up requires a little more, though it’s definitely something to consider.
I would also make sure directory and file permissions are properly setup – that is, files should have a chmod
of 644
and directories 755
. Also, proper ownership of files and directories should be set as you don’t want public files being served by the root
user. I even tend to go a step further and only serve files and directories from a non-root, non-sudo user. Just a basic, unprivileged, non-shell user that can’t do anything other than own files and directories.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.