Many factors are playing role if a web server is affected by virus. I have mentioned some as below:
1. If you allow your users/visitors to upload any files, folders from your web site.
2. If you use a third party script which is vulnerable and also if you use outdated third party script in your web site.
3. The system from where you manage your web site is infected with virus/spyware/Trojan/malware.
4. If the password of your FTP account is compromised.
5. If your web site has XSS vulnerability.
so to protect your system and server use any good anti virus with firewalls so that it will stop bad data.