Project

General

Profile

Setting up a session save path in php » History » Version 6

Gareth Eaton, 10/12/2023 03:06 PM

1 1 Gareth Eaton
h1. Setting up a session save path in php
2
3
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.
4
5
6
After a few clicks in the Nextclouds Web interface, especially in the Files page, I get the message:
7
8
*"Problem loading the page, reloading in 5 seconds"*
9
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:
10
11
*"app":"no app in context","message":"Current user is not logged in"*
12
13 6 Gareth Eaton
Solution:
14
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.
15 1 Gareth Eaton
16 5 Gareth Eaton
| *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. |
17 1 Gareth Eaton
18
19
20 2 Gareth Eaton
| Create a Temporary Directory:
21
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. |
22
23
24 1 Gareth Eaton
25
26
Create the "tmp" Directory:
27
28
You can use the mkdir command to create the "tmp" directory in the /var directory. Open your terminal and run:
29
30
31
<pre>
32 3 Gareth Eaton
sudo mkdir /var/tmp-session
33 1 Gareth Eaton
</pre>
34
35 3 Gareth Eaton
This will create the "tmp-session" directory within /var.
36 1 Gareth Eaton
37
Set Permissions:
38
39 3 Gareth Eaton
Set the correct permissions for the "tmp-session" directory using chmod 750:
40 1 Gareth Eaton
41
42
<pre>
43 3 Gareth Eaton
sudo chmod 750 /var/tmp-session
44 1 Gareth Eaton
</pre>
45
46
This ensures that the directory is readable, writable, and executable by the owner (root) and the group.
47
48 3 Gareth Eaton
<pre>
49
sudo chown www-data:www-data /var/tmp-session
50
</pre>
51
52
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. 
53
54
55 1 Gareth Eaton
Configure php.ini:
56
57
Open the php.ini file for editing:
58
59
60
<pre>
61
sudo nano /etc/php/<version>/apache2/php.ini
62
</pre>
63
64
Replace <version> with the PHP version you're using. For example:
65
66
<pre>
67
sudo nano /etc/php/8.1/apache2/php.ini
68
</pre>
69
70
Find the session.save_path directive and update it to use the new "/var/tmp" directory:
71
72
73
<pre>
74 3 Gareth Eaton
session.save_path = "/var/tmp-session"
75 1 Gareth Eaton
</pre>
76
77
Save your changes and exit the text editor.
78
79
Restart Apache Web Server:
80
81
Finally, restart the Apache web server to apply the changes to the PHP configuration:
82
83
84
<pre>
85
sudo service apache2 restart
86
</pre>
87
88
89
Why This Works:
90
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.