Tuesday, July 29, 2025Deploy Laravel application with Nginx
Keven Client A. Cataluña @LinkedIn
Introduction

This guide demonstrates how to deploy a Laravel application with Nginx by configuring the server, resolving common permission issues, running database migrations, and ensuring the application is accessible.

Steps1. Clone the Laravel project into the web root directory
/var/www/html
    git clone git@github_development:kevenclient/laravel.git
  
2. Create a new Nginx site configuration
/etc/nginx/sites-available/laravel
    server {
  root /var/www/html/laravel/public;

  # Add index.php to the list if you are using PHP
  index index.php index.html index.htm index.nginx-debian.html;

  server_name laravel.get-go.dev;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  # PHP-FPM Configuration Nginx
  location ~ .php$ {
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_hide_header X-Powered-By;
  }
}
  
3. Create a symlink to enable the new site configuration
/etc/nginx/sites-enabled
    sudo ln -s /etc/nginx/sites-available/laravel
  
4. Test the Nginx configuration
    sudo nginx -t
  
      nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  
5. Reload Nginx to apply the changes
    sudo systemctl reload nginx
  
6. Ensure that the views and logs directories are writable (To avoid permission errors Failed to open stream: Permission denied) Error
    file_put_contents(/var/www/html/laravel/storage/framework/views/823ba0f21fb92d4957f115f907d5ac44.php): Failed to open stream: Permission denied
  
    UnexpectedValueException
The stream or file "/var/www/html/laravel/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: The stream or file "/var/www/html/laravel/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
  
Solution
    chmod -R 777 /var/www/html/laravel/storage/framework/views
  
    chmod -R 777 /var/www/html/laravel/storage/logs
  
7. Run the migration and create database file (To avoid database file does not exist error Database file at path [/var/www/html/laravel/database/database.sqlite] does not exist) Error
    Illuminate\Database\QueryException
Database file at path [/var/www/html/laravel/database/database.sqlite] does not exist. Ensure this is an absolute path to the database. (Connection: sqlite, SQL: select * from "sessions" where "id" = vZdPKuxK1WKVrO0j4arsEG9spHURAB1VYCP1xRUX limit 1)
  
Solution
    php artisan migrate
  
      WARN  The SQLite database configured for this application does not exist: database/database.sqlite.

  ┌ Would you like to create it? ────────────────────────────────┐
  │ Yes                                                          │
  └──────────────────────────────────────────────────────────────┘

   INFO  Preparing database.

  Creating migration table ............................................................................... 7.98ms DONE

   INFO  Running migrations.

  0001_01_01_000000_create_users_table .................................................................. 21.38ms DONE
  0001_01_01_000001_create_cache_table ................................................................... 6.83ms DONE
  0001_01_01_000002_create_jobs_table ................................................................... 20.84ms DONE
  
8. Ensure that the SQLite database file is writable (To avoid permission error attempt to write a readonly database) Error
    Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 8 attempt to write a readonly database (Connection: sqlite, SQL: update "sessions" set "payload" = YTozOntzOjY6Il90b2tlbiI7czo0MDoibG5kQUZLcG03ZTRySEpyWndhMnJvZUZMVmlueE1DdU82M2FLWU9sWCI7czo5OiJfcHJldmlvdXMiO2E6MTp7czozOiJ1cmwiO3M6MjY6Imh0dHBzOi8vbGFyYXZlbC5nZXQtZ28uZGV2Ijt9czo2OiJfZmxhc2giO2E6Mjp7czozOiJvbGQiO2E6MDp7fXM6MzoibmV3IjthOjA6e319fQ==, "last_activity" = 1731239511, "user_id" = ?, "ip_address" = 49.145.102.186, "user_agent" = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36, "id" = vhOVWv02xJnMFVal1HtBN5q0TSsXnSYlX18hOtEX where "id" = vhOVWv02xJnMFVal1HtBN5q0TSsXnSYlX18hOtEX)
  
Solution
    chmod -R 777 /var/www/html/laravel/database/database.sqlite
  
9. Verify the deployment by navigating to https://laravel.get-go.dev and you should see the default Laravel landing page