1

Suppose I have a Laravel 11 application running at www.website.com, which is hosted at a linux machine. The application has a poor response time because it has to fetch data from three databases before it can return a response to the user. As such, I thought it would be a good idea to host a SQLite database at www.website.com to cache the results of read-intensive operations so the application can serve the results to the user immediately (I've considered Redis and Memched, but I decided to work with SQLite since I was already familiar with it). The concept worked on my local machine, so I pushed it over to the dev server for further testing before pushing it to the production and started configuring it.

The configuration process had the following steps:

  1. I accidentally deleted the database.sqlite file at the database directory before, so I replaced with a fresh database.sqlite and ran chmod 776 on it. This file is stored in the database directory in the default Laravel directory configuration. I'm perfectly aware that permissions this open is a bad idea, but since this is a dev server where things are usually broken during testing its fine. I was planning to change its permissions before pushing it to production.
  2. I also gave the database directory chmod 776 permissions.
  3. The .env was configured accordingly and I set the sqlite's filepath to /var/www/sites/website.com/database/database.sqlite.
  4. I ran migrations using php artisan migrate. No issues there. The migration was Laravel's default cache table.

However, trying to open the website failed and threw a 500 Internal Server Error. After checking the logs, it turns out that the app can't find the location of the database/database.sqlite. What made me confused was that the migrations in step 4 worked just fine without issue, so the app could find the location of the database. Furthermore, after checking the config at config/database.php, I found no issues.

Here is the relevant .env file and config/database.php sections of the code respectively. They might be useful in debugging.

.env

DB_CONNECTION=sqlite_cache
DB_DATABASE=/var/www/sites/website.com/database/database.sqlite
CACHE_STORE=database
DB_CACHE_CONNECTION=sqlite_cache
DB_CACHE_TABLE=cache
DB_CACHE_LOCK_CONNECTION=sqlite_cache
DB_CACHE_LOCK_TABLE=cache_locks

config/database.php

'sqlite_cache' => [
'driver' => 'sqlite',
'url' => env('DB_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'busy_timeout' => null,
'journal_mode' => null,
'synchronous' => null,
],

Right now, what caused the php artisan migrate command to work but not the application? How can I get the application to detect the existence of the database?

What I tried: The steps described above. What I expected to happen: The application runs just fine, since the php artisan migrate commands ran just fine. What actually happened: The application couldn't locate the location of the sqlite database, even though the php artisan migrate and its related commands worked just fine.

1
  • you might have stale connections somewhere. restart everything involved in the setup and see what happens. if you can enable logging/debugging then do that to get as much detail written to logfiles as possible. Commented Dec 12, 2024 at 23:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.