• Fri, Mar 2026

Deploying Laravel to Production: Best Practices for Beginners to Experts

Deploying Laravel to Production: Best Practices for Beginners to Experts

This guide teaches you how to deploy Laravel applications to production servers. From preparing your environment and configuring Nginx or Apache, to database migrations, caching, performance optimization, CI/CD pipelines, and security practices—this article covers everything step by step.It’s suitable for both beginners and advanced developers who want to ship stable, secure & scalable app.

Introduction

Building an application with Laravel is only half the journey—the real challenge begins when you’re ready to deploy it to a production environment. Deployment is more than just copying files from your local machine to a server. It involves configuring the environment for performance, ensuring security, setting up web servers, automating builds, and monitoring health.

In this guide, we’ll explore Laravel deployment from start to finish. Whether you’re deploying to a shared hosting provider, a VPS, Docker, or a cloud platform like AWS, DigitalOcean, or Linode, you’ll find best practices to help you achieve a smooth launch.

Step 1: Preparing Your Laravel Application

1.1 Check Application Dependencies

Make sure your composer.json and package.json files are up to date:


composer install --optimize-autoloader --no-dev
npm install --production
npm run build
    

1.2 Set Environment Variables

Never commit your .env file. Instead, configure it on the server.


APP_ENV=production
APP_DEBUG=false
APP_KEY=base64:YOURKEYHERE
APP_URL=https://yourdomain.com
    

1.3 Generate Application Key


php artisan key:generate
    

Step 2: Choosing the Right Hosting

Laravel can run on multiple environments. Let’s compare common hosting options:

Hosting TypeProsCons
Shared HostingCheap, beginner-friendlyLimited control, poor scalability
VPS (DigitalOcean, Linode, Vultr)Full control, affordable, scalableRequires server management skills
Managed Laravel Hosting (Laravel Forge, Ploi)Easy deployment, automated setupsMonthly cost, less flexibility
Cloud Platforms (AWS, GCP, Azure)Highly scalable, advanced toolsComplex setup, higher learning curve

Step 3: Setting Up Your Server

3.1 Update Packages


sudo apt update && sudo apt upgrade -y
    

3.2 Install PHP, Composer, and MySQL


sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql unzip curl git -y
sudo apt install composer mysql-server -y
    

3.3 Configure Database


CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
    

Step 4: Configuring Web Server

4.1 Nginx Configuration

sudo nano /etc/nginx/sites-available/laravel

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/laravel/public;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    index index.php;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
                                            

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
    

4.2 Apache Configuration

sudo a2enmod rewrite
<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/laravel/public
    <Directory /var/www/laravel/public>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 5: Deploying the Code

5.1 Clone the Repository


cd /var/www/
git clone https://github.com/your-repo/laravel-app.git laravel
cd laravel
    

5.2 Set Permissions


sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage
sudo chmod -R 775 /var/www/laravel/bootstrap/cache
    

5.3 Run Migrations

php artisan migrate --force

Step 6: Optimizing Laravel for Production

Run these artisan commands for optimization:


php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
    

Step 7: Setting Up Supervisor for Queues

For background jobs, use Supervisor:


sudo apt install supervisor -y
                    

sudo nano /etc/supervisor/conf.d/laravel-worker.conf
                    

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=3
redirect_stderr=true
stdout_logfile=/var/www/laravel/storage/logs/worker.log
                    

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
    

Step 8: Enabling HTTPS with SSL

Use Let’s Encrypt for free SSL:


sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
    

Step 5: Deploying the Code

5.1 Clone the Repository


cd /var/www/
git clone https://github.com/your-repo/laravel-app.git laravel
cd laravel
    

5.2 Set Permissions


sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage
sudo chmod -R 775 /var/www/laravel/bootstrap/cache
    

5.3 Run Migrations

php artisan migrate --force

Step 9: Setting Up CI/CD Pipelines

Use GitHub Actions or GitLab CI/CD for automated deployments.

# .github/workflows/deploy.yml
name: Deploy Laravel
on:
  push:
    branches: [ "main" ]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Deploy via SSH
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USER }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            cd /var/www/laravel
            git pull origin main
            composer install --optimize-autoloader --no-dev
            php artisan migrate --force
            php artisan config:cache

Step 10: Monitoring and Logging

10.1 Laravel Logs


tail -f storage/logs/laravel.log
    

10.2 Error Monitoring

Use services like Sentry, Bugsnag, or Laravel Telescope for real-time error tracking.

Best Practices for Production Deployment

  • Never expose .env to the public.
  • Use strong database passwords and disable remote root login.
  • Regularly back up your database and app files.
  • Use caching (Redis, Memcached) for performance.
  • Use queues for heavy tasks like emails or reports.
  • Always test deployments on staging before production.

Conclusion

Deploying a Laravel application to production can feel daunting, but with the right process, it becomes smooth and repeatable. From server setup, database configuration, and web server tuning, to security hardening and CI/CD automation, each step ensures your application is stable, secure, and ready for real users.

Following these best practices will help you avoid downtime, improve performance, and provide a professional experience for your end users. Whether you’re deploying your first Laravel app or managing multiple large-scale projects, mastering deployment will elevate your skills as a full-stack developer.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy