Failed to start session. session_start() - failed: Permission denied - Yii 3 error solution

I am just playing around (getting started) with yii-demo

/path/to/yii-demo$ ./vendor/bin/yii serve

When I visit the browser http://localhost:8080/

I am facing

Yiisoft\Session\SessionException
Failed to start session.
↵
Caused by: Yiisoft\ErrorHandler\ErrorException
session_start(): open(/var/lib/php/sessions/sess_pu15kbejse8125i7sil6utcg3j3lqv6h9an935c6v43ivkrn, O_RDWR) failed: Permission denied (13)
in /path/to/yii-demo/vendor/yiisoft/session/src/Session.php at line 85

vendor/yiisoft/session/src/Session.php -> line 88

What is the solution?

Solution:

Try

chmod -R 666 /var/lib/php/sessions

If that did not work, change ownership of directory

Found that the dir /var/lib/php/sessions should be owned by the user. Here user means operating system user. Generally in Linux CLI you see sparrow@pearl:/path/to/project: here sparrow is the user. So

sudo chown -R sparrow:sparrow /var/lib/php/sessions

This is not concrete solution but in most cases it will work.

Additional note

I asked: Out of curiosity and to confirm and polish my knowledge about sessions, session in PHP in just the key=>value database stored in plain file in the same operating system PHP is running, am i right?

Samdark:

Kind of.

It’s not the same file all the time.

It creates/modifies multiple files.

each session ID corresponds to a separate file

that is not to have too much locking and concurrency issues