Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Why Hackers Target Web Servers & How to Protect Yours

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Hackers target web servers because they are high-value, always-on systems that often hold sensitive data, credentials, and computing power. A compromised server can be used to steal customer information, launch attacks on other systems, send spam, or mine cryptocurrency. Protecting your server means closing common entry points, keeping software updated, and monitoring for signs of intrusion. This guide walks you through the most effective steps you can take today.

What You'll Need

  • Administrative access to your web server (SSH or control panel login)
  • A list of software you run (web server, CMS, plugins, etc.)
  • Ability to install software (or a hosting provider that allows it)
  • A firewall tool (like UFW on Linux, or your hosting provider's firewall)
  • Access to your DNS settings (for enabling HTTPS)

Step-by-Step

  1. Update everything immediately. Log into your server and run the system update command for your OS (e.g., sudo apt update && sudo apt upgrade on Ubuntu/Debian, or sudo yum update on CentOS/RHEL). Then update your web server software, PHP, database, and any CMS or scripts you run. Unpatched software is the #1 reason servers get hacked.
  2. Change default credentials. If you have any default usernames (like admin or root) or default passwords, change them to strong, unique ones. Use a password manager to generate and store them. Also change the default SSH port from 22 to something else (like 2222) in your SSH config file, but only if you know how to update your firewall rules accordingly.
  3. Enable SSH key authentication. Generate an SSH key pair on your local machine (ssh-keygen), then copy the public key to the server (ssh-copy-id user@server). Disable password authentication in /etc/ssh/sshd_config by setting PasswordAuthentication no. This blocks brute-force password attacks.
  4. Set up a firewall. Use a firewall to allow only necessary ports: 80 (HTTP), 443 (HTTPS), and your SSH port. On Ubuntu, sudo ufw allow 80/tcp, sudo ufw allow 443/tcp, sudo ufw allow 2222/tcp, then sudo ufw enable. If you use a cloud provider, also configure their security group settings.
  5. Install and configure a web application firewall (WAF). A WAF filters malicious traffic before it reaches your server. Options include ModSecurity (open-source) or a cloud WAF like Cloudflare. If you use Cloudflare, enable the “Under Attack” mode during a surge, and set security level to “High” normally.
  6. Enable HTTPS with a free SSL certificate. Use Let's Encrypt via Certbot: install certbot, then run sudo certbot --nginx (or --apache) to automatically issue and configure the certificate. This encrypts traffic and prevents man-in-the-middle attacks.
  7. Harden your web server configuration. Disable directory listing (set Options -Indexes in Apache or autoindex off in Nginx). Hide server version headers (e.g., ServerTokens Prod in Apache). Limit request size to prevent buffer overflows (e.g., client_max_body_size 1m in Nginx).
  8. Secure your database. Change the default database admin password, create separate users with minimal privileges for each application, and never use the root user for web apps. If using MySQL, run mysql_secure_installation.
  9. Keep backups offsite. Set up automated backups of your server files and database to a separate location (like an external bucket or another server). Test restoring from backups at least once a month. This is your safety net if a hack does occur.
  10. Monitor for intrusions. Install a security scanner like chkrootkit or rkhunter and run them weekly. Check your server logs regularly (/var/log/auth.log for SSH, /var/log/nginx/access.log for web traffic). Look for failed login attempts, unusual outbound connections, or unexpected files.

Common Mistakes

  • Ignoring updates because “it might break something.” Outdated software is the easiest way in.
  • Using a weak password for the root/admin user.
  • Opening unnecessary ports (like 3306 for MySQL) to the internet.
  • Running a server without a firewall, relying only on the hosting provider's default.
  • Not backing up, or having backups that are not tested.
  • Assuming that “small” sites are not targeted—automated scanners hit every IP.

Tips

  • Use a separate user for day-to-day tasks instead of root, and elevate with sudo only when needed.
  • Set up automatic security updates for your OS and web software if your provider allows it.
  • Regularly audit which users have access to your server and revoke any that are unused.
  • If you run a CMS like WordPress, disable XML-RPC if you don't need it—it's a common attack vector.
  • Consider a managed hosting provider that handles security for you, if you prefer not to manage it yourself.

Troubleshooting

If something goes wrong or you suspect your server has been compromised, follow these steps in order:

  1. Check if your site is actually down or just slow. Use a service like DownForEveryoneOrJustMe to see if it's a local issue.
  2. Restart your web server and database. Often a simple restart clears a temporary issue.
  3. Review your firewall rules. If you just changed a rule, you may have locked yourself out. Use your hosting provider's console (like KVM) to access the server if SSH is blocked.
  4. If you suspect a hack: Immediately take the server offline (stop the web server), change all passwords, and restore from a known-good backup. Then audit how the breach happened before putting it back online.
  5. If you cannot restore from backup, you may need to reinstall the OS and rebuild from scratch—this is often safer than trying to clean a compromised system.

Frequently Asked Questions

Why would a hacker target my small website?

Automated bots scan the internet for any server with known vulnerabilities, regardless of size. Your server's resources can be used for cryptomining, sending spam, or as a launchpad for attacks on larger networks.

How do I know if my server has been hacked?

Signs include unexpected server load, strange files in your web directory, unknown processes running, or your site being flagged by browsers. Check your logs for unusual activity and run a rootkit scanner.

Is a web application firewall enough?

No, a WAF is one layer. You still need updates, strong passwords, and monitoring. Defense in depth is the goal.

What should I do if my server is already compromised?

Take it offline immediately, change all credentials, restore from a clean backup, and patch the vulnerability that was exploited. If you don't know the cause, consider a full reinstall.

Can I protect my server without using the command line?

Yes, many hosting providers offer control panels (like cPanel or Plesk) that let you manage updates, firewalls, and SSL certificates graphically. However, some steps like SSH key authentication may still require command-line access.

How often should I back up?

At least daily for files and databases, and test your restore process monthly. The more critical your data, the more frequent the backups.