Skip to main content

How to Deploy a Laravel Application on a VPS: A Technical Guide for CTOs

Leo Liebert
NR Studio
6 min read

Deploying a Laravel application to a Virtual Private Server (VPS) moves you beyond the limitations of managed platforms like Forge or Heroku, granting you full control over the stack, security, and resource allocation. For technical founders and CTOs, understanding the underlying infrastructure is critical for maintaining high-performance production environments that scale with your user base.

This guide covers the manual configuration of a production-ready LEMP stack (Linux, Nginx, MySQL, PHP-FPM) optimized for Laravel. We will move past basic tutorials by addressing process management, security hardening, and zero-downtime deployment strategies. Whether you are migrating from a shared host or scaling a growing SaaS, these steps ensure your application remains stable and efficient under load.

Prerequisites and Server Preparation

Before installing your code, your VPS must be hardened. Start with a fresh Ubuntu LTS instance. Never run your application as the root user. Create a dedicated system user with sudo privileges and configure your SSH keys to disable password-based authentication entirely.

  • Update System: Run sudo apt update && sudo apt upgrade -y to patch vulnerabilities.
  • Firewall Configuration: Use ufw to block all incoming traffic by default, allowing only SSH (port 22), HTTP (80), and HTTPS (443).
  • Swap Memory: Laravel applications, especially those using Laravel Mix or Vite for asset compilation, can consume significant memory. If your VPS has limited RAM, create a 2GB swap file to prevent the OOM (Out of Memory) killer from terminating your PHP processes.

Configuring the LEMP Stack

The performance of your Laravel application is directly tied to the efficiency of your web server and PHP processor. We recommend Nginx for its event-driven architecture, which handles concurrent connections more gracefully than Apache.

sudo apt install nginx php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip

Once installed, configure the Nginx server block to point to your application’s public/ directory. Ensure your PHP-FPM pool is tuned for your specific hardware. For high-traffic applications, adjust pm.max_children and pm.max_requests in your www.conf file to balance memory usage against request throughput.

Database Deployment and Security

MySQL or MariaDB is the backbone of your application. During deployment, do not use the root database user for your application. Create a specific user with privileges limited to your application’s database schema:

CREATE DATABASE app_db; CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'secure_password'; GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';

For production, ensure binary logging is enabled for backups and that your database is tuned for InnoDB buffer pool size (typically 70-80% of total system RAM if the database is the primary resource on the server).

Automating Deployment with Git and Deploy Scripts

Manually uploading files via FTP is unacceptable for professional software development. Use a Git-based deployment workflow. Initialize a bare repository on your server or use a CI/CD pipeline (GitHub Actions or GitLab CI) to push updates.

Your deployment script should follow this sequence:

  1. git pull origin main
  2. composer install --no-dev --optimize-autoloader
  3. php artisan migrate --force
  4. php artisan config:cache
  5. php artisan route:cache
  6. php artisan view:cache

Always use the --force flag in production to bypass confirmation prompts, and ensure APP_DEBUG is set to false in your .env file.

Process Management with Supervisor

Laravel relies on background workers for tasks like queue processing and scheduled jobs. If these processes crash, your application will lose functionality. Supervisor is the standard tool for managing these long-running processes.

Create a configuration file in /etc/supervisor/conf.d/laravel-worker.conf:

[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 --max-time=3600 autostart=true autorestart=true user=www-data numprocs=8 redirect_stderr=true

Using numprocs allows you to scale your queue throughput horizontally on a single machine by running multiple worker instances simultaneously.

Performance and Security Tradeoffs

Deploying on a VPS introduces a critical tradeoff: Control vs. Maintenance. While you save on platform fees, you are responsible for security patches, OS updates, and server monitoring. If you lack the internal capacity to manage Linux servers, the operational cost of downtime often outweighs the monthly savings compared to managed services.

For performance, consider the impact of caching. If your application reads from the database frequently, implement Redis as your cache and session driver. This reduces disk I/O and significantly lowers latency, though it requires additional RAM on your VPS.

Monitoring and Maintenance

Once deployed, your work is not finished. You must implement log rotation (typically via logrotate) to prevent your disk from filling up with laravel.log files. Monitor your server health using tools like Netdata or Prometheus. If you detect high CPU usage, use htop to identify which processes are consuming resources, and check your database slow query logs to optimize your Eloquent queries.

Factors That Affect Development Cost

  • Server hardware (CPU/RAM requirements)
  • Automated monitoring and backup solutions
  • Engineer time for initial server hardening
  • Security audit requirements

VPS costs are generally predictable monthly fees based on resource allocation, though manual maintenance increases internal labor costs compared to managed platforms.

Frequently Asked Questions

Should I use the root user for my Laravel application?

No, never run your application as the root user. Always create a dedicated system user to minimize the security impact if your application is compromised.

Is a VPS better than shared hosting for Laravel?

Yes, a VPS provides isolated resources, root access to the server environment, and the ability to install necessary extensions like Redis or Supervisor, which are essential for Laravel performance.

How do I handle downtime during a deployment?

You can minimize downtime by using symlink-based deployments where you build your application in a new directory and swap the web server’s root path to the new directory once the build is complete.

Deploying a Laravel application on a VPS provides the performance and architectural freedom required for high-growth businesses. By automating your deployment pipeline and properly managing your background workers with Supervisor, you create a robust foundation that is significantly more performant than standard shared hosting.

If your team needs assistance architecting a scalable infrastructure or optimizing your current deployment workflow, our engineers at NR Studio are here to help. We specialize in building and maintaining high-performance Laravel applications tailored for complex business requirements.

NR Studio builds custom web apps, mobile apps, SaaS platforms, and internal tools for growing businesses. If you’re working through a technical decision, feel free to reach out — no commitment required.

References & Further Reading

NR Studio Engineering Team
3 min read · Last updated recently

Leave a Comment

Your email address will not be published. Required fields are marked *