Securing a VPS: Best Practices for Enhanced Security
A Virtual Private Server (VPS) is a powerful solution for hosting websites and applications. However, securing it is essential to protect your data, users, and reputation. Follow these guidelines to harden your VPS security effectively:
The root account has unlimited privileges, making it a prime target for attackers. Disabling root login reduces the risk of unauthorized access.
Steps:
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
sudo systemctl restart sshd
Restricting open ports reduces the server's attack surface. Allow only essential ports like SSH (22), HTTP (80), HTTPS (443), and DNS (53).
Steps:
ufw:
sudo ufw allow 22 # SSH
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw allow 53 # DNS
sudo ufw enable
sudo ufw status
Sensitive files like /etc/passwd, wp-config.php, and others should have limited access.
Steps:
wp-config.php:
sudo chmod 600 /var/www/html/wp-config.php
sudo chown www-data:www-data /var/www/html/wp-config.php
ls -l /etc/passwd
Unnecessary modules like autoindexing expose directory listings, which can reveal sensitive data.
Steps (for Apache):
autoindex:
sudo a2dismod autoindex
sudo systemctl restart apache2
Securing PHP prevents exposure of sensitive information and reduces vulnerabilities.
Steps:
Open the PHP configuration file:
sudo nano /etc/php/7.x/apache2/php.ini
(Replace 7.x with your PHP version.)
Make these changes:
expose_php = Off
disable_functions = exec,passthru,shell_exec,system
Restart the server:
sudo systemctl restart apache2
SSL encrypts communication between the server and clients, enhancing security.
Steps (using Let’s Encrypt):
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
sudo certbot renew --dry-run
Incorrect permissions can allow unauthorized users to modify critical files.
Recommended WordPress Permissions:
sudo find /var/www/html/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/ -type f -exec chmod 644 {} \;
wp-config.php AccessThis file contains sensitive credentials and should not be accessed publicly.
Steps:
.htaccess:
<Files wp-config.php>
Order Allow,Deny
Deny from all
</Files>
.htaccess file in the root WordPress directory.Fail2Ban monitors logs and bans IPs attempting brute-force attacks.
Steps:
sudo apt install fail2ban
sudo nano /etc/fail2ban/jail.local
Add:
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
sudo systemctl restart fail2ban
ClamAV helps detect and remove malware on your server.
Steps:
sudo apt install clamav clamav-daemon
sudo freshclam
sudo clamscan -r /var/www/html
To secure your WordPress installation further:
By following these steps, your VPS will be fortified against common threats. Regular maintenance, updates, and monitoring are critical for sustained security. Don’t just set it and forget it—stay vigilant to keep your VPS and WordPress site secure.
Your email address will not be published. Required fields are marked *
Comments