If your server is sending out spam emails, the culprit is often the www-data user—the account that runs your web server. A compromised PHP script, a vulnerable plugin, or a misconfigured contact form can be abused to send thousands of emails. This guide shows you how to lock down www-data so it can no longer send mail, stopping the spam at the source. You’ll need root or sudo access to your server.
What You’ll Need
- Root or sudo access to your Linux server (Ubuntu, Debian, CentOS, etc.)
- Basic familiarity with the command line
- Ability to edit files in
/etc(e.g., usingnanoorvim)
Step-by-Step
- Identify the mail path. First, check which mail transfer agent (MTA) is installed. Run
which sendmailandwhich postfix. If you see/usr/sbin/sendmail, it might be Postfix or Sendmail. Also checkwhich eximandwhich mail. This tells you what binaries are being used to send mail. - Check current mail logs. Look at
/var/log/mail.log(or/var/log/maillogon CentOS) to see if there are bursts of outgoing mail. Usegrep 'status=sent' /var/log/mail.log | tail -n 20to see recent sent messages. Note the sender and recipient addresses—they’ll often be random or from your domain. - Find the source script. If you see emails from
www-data, search your web directory for PHP files that contain mail functions. Usegrep -r 'mail(' /var/www/html(adjust path to your web root). Look for suspicious files—recently modified, in upload directories, or with obfuscated code. - Remove or neutralize the malicious script. Delete the offending file if it’s clearly malware. If it’s a legitimate plugin or script that’s vulnerable, update it or remove it temporarily. Also check cron jobs (
crontab -efor root, andls /etc/cron.*) for anything running as www-data that might be sending mail. - Block the sendmail binary for www-data. The most direct fix is to remove execute permissions on the mail binary for the www-data user. Run:
chmod o-x /usr/sbin/sendmail
This removes execute permissions for “other” (which includes www-data). Test withsudo -u www-data /usr/sbin/sendmail -v test@example.com—it should fail with “Permission denied.” - Use AppArmor or SELinux (if available). On Ubuntu/Debian with AppArmor, you can create a profile to deny mail sending. For example, create
/etc/apparmor.d/usr.sbin.php-fpmwith rules that deny/usr/sbin/sendmail. On CentOS with SELinux, you can setsetsebool -P httpd_can_sendmail off(this is often already off, but verify). - Configure PHP to disable mail functions. In your
php.inifile (find it withphp -i | grep php.ini), adddisable_functions = mail, sendmail. Then restart PHP-FPM or Apache. This stops any PHP script from using the mail() function. - Restart services. After making changes, restart your web server and MTA:
sudo systemctl restart apache2(or nginx/php-fpm) andsudo systemctl restart postfix(if you’re using Postfix). - Monitor logs. Watch
/var/log/mail.logfor any new attempts. If you still see outgoing mail, the attacker may have found another way (e.g., using SMTP directly). In that case, you’ll need to block outbound SMTP connections from the web server (see Common Mistakes).
Common Mistakes
- Only removing the script. If you delete the malicious script but don’t lock down the mail binary, the attacker can re-upload it. Always combine script removal with permission changes.
- Blocking sendmail but not other MTAs. Some servers use Exim or Postfix’s sendmail wrapper. Make sure you block all mail-sending binaries:
/usr/sbin/sendmail,/usr/sbin/exim4,/usr/sbin/postfix(though postfix is usually not directly executable by www-data). - Forgetting about PHP’s mail() function. Even if you block the binary, PHP’s mail() function can still try to use it. Disable it in php.ini as described.
- Not checking for other users. Spam might be sent by another user (e.g., a compromised FTP account). Repeat the process for any user that runs web services.
- Overlooking SMTP direct connections. A script can connect to localhost:25 (or a remote SMTP server) directly. To block this, use firewall rules to prevent outbound connections on port 25 from the web server, except to your own mail server if needed.
Tips
- Use a firewall. On Ubuntu, use UFW:
sudo ufw deny out 25(this blocks all outbound SMTP, which may affect legitimate mail sending—adjust accordingly). On CentOS, use firewalld:sudo firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 0 -p tcp --dport 25 -j REJECT. - Run your web server as a non-privileged user. If you can, run PHP-FPM as a dedicated user (e.g.,
phpuser) instead of www-data. Then you can restrict that user’s permissions more tightly. - Use a mail relay. If your applications need to send email (e.g., password resets), set up a dedicated mail relay that requires authentication. Then block direct SMTP from the web server and only allow the relay.
- Regularly scan for malware. Use tools like ClamAV or Linux Malware Detect (LMD) to scan your web directory for malicious files.
- Keep software updated. The most common way www-data gets compromised is through vulnerable plugins or outdated CMS software. Regular updates reduce the risk.
Troubleshooting
- After blocking sendmail, your legitimate web forms stop sending email. This is expected. You’ll need to configure a valid mail relay or use an external SMTP service (like your hosting provider’s SMTP) with authentication in your application. Update your PHP scripts to use SMTP instead of the mail() function.
- You still see outgoing mail in logs. Check if the mail is being sent via a different binary (e.g.,
/usr/bin/mail). Block that too. Also check if the attacker is using a PHP script that connects to an external SMTP server—usenetstat -tulpnto see active connections from the web server. - AppArmor or SELinux denies everything. If you enable a strict profile, you might break legitimate services. Test with
aa-status(AppArmor) orgetenforce(SELinux). Adjust the profile to allow necessary operations while denying sendmail. - You can’t find the malicious script. Use
lsof -p $(pgrep -u www-data | head -1) | grep mailto see which files the www-data process has open. Also check/tmpand/dev/shmfor suspicious files. - After restarting services, the site breaks. If you disabled too many functions, PHP might error out. Check
/var/log/php_errors.logand re-enable any functions that are essential (e.g.,mailmight be needed for a contact form—instead, use SMTP).
Frequently Asked Questions
- Will blocking sendmail break my website? It will break any script that relies on the PHP mail() function or direct sendmail. However, most modern applications use SMTP with authentication, which is not affected. If you need mail, configure an SMTP relay.
- How do I know if my server is sending spam? Check mail logs for a high volume of sent messages, or look for bounce-back emails in your inbox. You can also use
pflogsumm(for Postfix) to summarize mail traffic. - Can I just change the www-data user’s shell to nologin? That would prevent login, but www-data still runs web processes. It won’t stop mail sending. You need to restrict the mail binaries specifically.
- What if I use Nginx instead of Apache? The process is the same—www-data is often used for Nginx too. The steps apply regardless of the web server.
- Is it safe to delete the malicious script? Yes, if you’ve verified it’s malicious. But first, back it up for analysis. After deleting, scan the entire web directory for other infected files.
- How do I prevent this from happening again? Keep your CMS and plugins updated, use strong passwords, disable file uploads in directories that don’t need them, and regularly scan for malware. Also, consider using a Web Application Firewall (WAF).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Mouse Moving on Its Own? Causes & Fixes
- Slack Channels for Construction Projects: Setup Guide
- Surface Won’t Turn On: Quick Fixes1.8 h