DEV Community

Cover image for Linux VPS Hardening Checklist for Laravel Servers
Muhammad Ali Mughal
Muhammad Ali Mughal

Posted on Edited on

Linux VPS Hardening Checklist for Laravel Servers

A fresh Ubuntu VPS with a Laravel app on it is reachable from the whole internet within minutes of booting, and automated scanners start knocking on SSH almost immediately. Most small-business servers I am asked to take over were never compromised through a clever exploit. They were exposed through defaults: password SSH, a database listening on a public interface, a Redis port open to the world, or an .env file Nginx would happily serve.

I run Loop Origin, where we set up and maintain production servers for business applications. I wrote this with help from AI tools and checked every command and config myself. This is the Linux VPS hardening checklist I actually run, in order, with the mistakes that lock people out.

What is the minimum Linux VPS hardening checklist?

Direct answer: Use SSH keys only and disable root and password login, put a default-deny firewall in front of everything except SSH, HTTP and HTTPS, enable automatic security updates, run the app as an unprivileged user, bind databases and Redis to localhost, block dotfiles in Nginx and keep tested off-server backups.

Order matters: do it without locking yourself out

The most common hardening failure is not an attack. It is an engineer disabling password login before their key works, or enabling a firewall before allowing SSH. My rule: keep one SSH session open the whole time, test every change from a second session, and only close the first when the second works.

1. Create a deploy user and use keys

adduser deploy
usermod -aG sudo deploy
mkdir -p /home/deploy/.ssh
cp ~/.ssh/authorized_keys /home/deploy/.ssh/   # assumes your key is already on root
chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh && chmod 600 /home/deploy/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

Open a new terminal and confirm ssh deploy@your-server works and sudo -v succeeds before going further.

2. Lock down sshd

On current Ubuntu releases, put overrides in a drop-in file rather than editing the main config:

# /etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers deploy
Enter fullscreen mode Exit fullscreen mode
sudo sshd -t && sudo systemctl reload ssh
Enter fullscreen mode Exit fullscreen mode

sshd -t validates the config; if it prints anything, fix it before reloading. Then test from a second session. Changing the SSH port reduces log noise but is not a security control on its own; keys are.

3. Default-deny firewall

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose
Enter fullscreen mode Exit fullscreen mode

If you can, restrict SSH to your office or VPN IPs (sudo ufw allow from 203.0.113.10 to any port 22) and use your provider's cloud firewall as a second layer, so a mistake on the box does not expose everything.

Docker gotcha: ports published with docker run -p 5432:5432 are written straight into iptables and can bypass ufw rules. If you run containers, publish to localhost only (-p 127.0.0.1:5432:5432) or put the rules in the provider firewall. I have found more than one "firewalled" server with a public database because of this.

4. Brute-force protection and automatic updates

sudo apt install fail2ban unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
Enter fullscreen mode Exit fullscreen mode
# /etc/fail2ban/jail.local
[sshd]
enabled  = true
maxretry = 5
findtime = 10m
bantime  = 1h
Enter fullscreen mode Exit fullscreen mode

With key-only SSH, fail2ban mostly cuts log noise, but it is also useful later for Nginx jails on login endpoints. Unattended-upgrades applies security updates; decide whether automatic reboots are acceptable for your app, and if not, schedule a monthly reboot window for kernel updates.

A laptop with a green-lit screen on an office desk
AI-generated image.

Hardening the Laravel layer

5. Users and file permissions

Run PHP-FPM as a dedicated user and give it write access only where Laravel needs it:

sudo chown -R deploy:www-data /var/www/app
sudo find /var/www/app -type f -exec chmod 644 {} \;
sudo find /var/www/app -type d -exec chmod 755 {} \;
sudo chmod -R ug+rwx /var/www/app/storage /var/www/app/bootstrap/cache
sudo chmod 640 /var/www/app/.env
Enter fullscreen mode Exit fullscreen mode

The web server should never be able to write to app/, routes/ or vendor/. If an attacker gets code execution through a vulnerable package, read-only code limits what they can persist.

6. Nginx: serve only the public folder

server {
    root /var/www/app/public;
    server_tokens off;

    location ~ /\.(?!well-known) { deny all; }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    }
}
Enter fullscreen mode Exit fullscreen mode

The root must point at public/, not the project folder; otherwise .env, storage/logs and composer.json may be downloadable. The dotfile rule is a second guard. Also set expose_php = Off in php.ini and APP_DEBUG=false in production, because a debug error page can reveal environment values.

7. Databases, Redis and queues

sudo ss -tulpn
Enter fullscreen mode Exit fullscreen mode

This shows every listening port. On a single-server setup, MySQL or PostgreSQL, Redis and Horizon dashboards should listen on 127.0.0.1 only. For Redis, keep bind 127.0.0.1 ::1 and protected-mode yes, and set a password if anything else can reach it. When you split into separate database and app servers, use a private network and firewall rules that allow only the app server's private IP.

Queue workers run under Supervisor as the deploy or web user, never root. I covered worker setup in running Laravel queues in production.

8. Secrets, TLS and backups

  • Keep secrets out of Git and out of shell history; rotate any key that was ever committed. My notes on clean codebase handover cover secret inventories.
  • Use Let's Encrypt with automatic renewal and check the renewal timer after the first install.
  • Back up databases and uploads off the server, encrypt them, and test a restore. The server you are hardening is also the server that can be lost. See SaaS backup and disaster recovery for RPO and restore drills.

Which controls matter most?

Control Threat it stops Effort What it can break
SSH keys only, no root login Password brute force, credential stuffing Low Lockout if keys are not tested first
Default-deny firewall Exposed databases, admin panels, debug ports Low Services that rely on open ports; Docker bypass
Unattended security updates Known vulnerabilities in system packages Low Rare package conflicts; reboots need a window
Least-privilege file permissions Persistence after code execution Medium Uploads and cache writes if storage is not writable
Nginx root on public/ + dotfile block .env and log disclosure Low Nothing, if the app follows Laravel layout
Localhost-only DB and Redis Remote database and cache access Low Remote admin tools; use an SSH tunnel instead
Off-server, tested backups Ransomware, provider failure, bad deploys Medium Nothing; costs storage

DIY, control panel or managed retainer?

Option Who does the hardening Good for Watch out for
DIY on a bare VPS Your team Teams with Linux experience and time for patching Drift: steps done once and never re-checked
Server management panel Panel defaults plus your team Small teams running many similar sites Panel defaults vary; still check ports and backups
Managed retainer An external engineer, on a schedule Businesses without in-house ops Make sure you keep root access and documentation

Whichever you choose, the checklist is the same. The difference is who re-runs it after every OS upgrade and new service.

The Linux VPS hardening checklist

  • [ ] Deploy user with sudo and SSH keys, tested from a second session
  • [ ] PermitRootLogin no and PasswordAuthentication no, validated with sshd -t
  • [ ] ufw default deny; only SSH, 80 and 443 open; cloud firewall as a second layer
  • [ ] Docker ports published to 127.0.0.1 or filtered upstream
  • [ ] fail2ban for sshd; unattended-upgrades enabled with a reboot plan
  • [ ] Code owned by deploy user, only storage/ and bootstrap/cache writable
  • [ ] .env at 640, APP_DEBUG=false, expose_php = Off
  • [ ] Nginx root on public/, dotfiles denied, server_tokens off
  • [ ] ss -tulpn shows databases and Redis on localhost only
  • [ ] TLS auto-renewal verified; encrypted off-server backups with a tested restore

FAQ

Is changing the SSH port enough?

No. It reduces automated log noise, but scanners find non-standard ports quickly. Key-only authentication, no root login and a firewall that limits SSH to known IPs are the real controls.

Do I still need fail2ban if passwords are disabled?

It is less critical, because brute force cannot succeed against keys. It still reduces log noise and is useful for protecting app login endpoints through Nginx jails, so I usually keep it.

Should unattended-upgrades reboot the server automatically?

It depends on your uptime needs. For a single server running a business app, I prefer automatic security updates without automatic reboots, plus a scheduled monthly reboot window so kernel patches actually take effect.

Why is my database public even though ufw blocks the port?

Usually Docker. Published container ports are added to iptables directly and can bypass ufw. Bind the port to 127.0.0.1 in your compose file or block it in the provider firewall, then confirm with an external port check.

What file permissions does Laravel need?

The web server user needs write access to storage/ and bootstrap/cache only. Everything else should be readable but not writable by PHP-FPM, and .env should not be world-readable.

How often should I re-check a hardened server?

After every OS upgrade, new service or new team member, and at least quarterly. Run ss -tulpn, review firewall rules and users, and restore a backup to confirm it still works.

The short version

Most VPS incidents start with defaults, not zero-days. Work in order, keep a second session open, use keys and a default-deny firewall, keep databases private, serve only Laravel's public/ folder and test your backups. Then write the checklist into your runbook so the next upgrade does not quietly undo it.


Follow & Connect

Watch on YouTube: Resume Boost AI · PixelValut · Daily AI News By Ali

Top comments (0)