Project

General

Profile

Actions

Setting up a session save path in php

I recently encountered an issue with Nextcloud where I kept experiencing unwanted logouts, leading to frustrating login loops. After some research and trial-and-error, I managed to find a solution that worked for me.

After a few clicks in the Nextclouds Web interface, especially in the Files page, I get the message:

"Problem loading the page, reloading in 5 seconds"
After 5 seconds I am logged out and have to login again. Having a look into the log-file there you can find the following entry:

"app":"no app in context","message":"Current user is not logged in"

Solution:
The solution involves configuring the php.ini file, which controls how PHP handles various settings on the server. Specifically, I focused on the session.save_path setting, which determines where PHP stores temporary session data for user logins.

Note I've installing the 'tmp' directory within the Nextcloud folder at '/var/www/html/nextcloud' can cause issues during updates as it may flag the directory. So install it in the /var/ that way you do not have to remove it and reinstall it later, when updating Nextcloud.
Create a Temporary Directory:
If you're setting up Nextcloud in the '/var/' directory, you can likely skip this step. However, if you're installing it in the Nextcloud directory, you'll need to create a new folder. To do this, I initially established a fresh directory called 'tmp' within my Nextcloud installation path, which in my case was '/var/www/html/nextcloud.' This 'tmp' directory serves as the location where PHP stores temporary session data.

Create the "tmp" Directory:

You can use the mkdir command to create the "tmp" directory in the /var directory. Open your terminal and run:

sudo mkdir /var/tmp-session

This will create the "tmp-session" directory within /var.

Set Permissions:

Set the correct permissions for the "tmp-session" directory using chmod 750:

sudo chmod 750 /var/tmp-session

This ensures that the directory is readable, writable, and executable by the owner (root) and the group.

sudo chown www-data:www-data /var/tmp-session

The command sudo chown www-data:www-data /var/tmp-session changes the ownership of the directory /var/tmp-session to the user and group www-data.

Configure php.ini:

Open the php.ini file for editing:

sudo nano /etc/php/<version>/apache2/php.ini

Replace <version> with the PHP version you're using. For example:

sudo nano /etc/php/8.1/apache2/php.ini

Find the session.save_path directive and update it to use the new "/var/tmp" directory:

session.save_path = "/var/tmp-session" 

Save your changes and exit the text editor.

Restart Apache Web Server:

Finally, restart the Apache web server to apply the changes to the PHP configuration:

sudo service apache2 restart

Why This Works:
The unwanted logouts and login loops were likely caused by issues with session management. By creating a custom temporary directory and configuring PHP's session.save_path to use it, I ensured that session data was stored reliably. This prevents Nextcloud from losing track of my authentication status, thus stopping the annoying logouts and loops.

Updated by Gareth Eaton 7 months ago · 6 revisions