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:
- I accidentally deleted the
database.sqlitefile at thedatabasedirectory before, so I replaced with a freshdatabase.sqliteand ranchmod 776on it. This file is stored in thedatabasedirectory 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. - I also gave the
databasedirectorychmod 776permissions. - The
.envwas configured accordingly and I set the sqlite's filepath to/var/www/sites/website.com/database/database.sqlite. - 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.