Cyberpanel'de Php Session Dosyalarının Otomatik Olarak Silinmesi

CyberPanel stores session files by default in

session.save_path = "/var/lib/php/session"

If you have manually set session.save_path, there is a warning as shown below.

; NOTE: If you are using the subdirectory option for storing session files
;       (see session.save_path above), then garbage collection does *not*
;       happen automatically.  You will need to do your own garbage
;       collection through a shell script, cron entry, or some other method.
;       For example, the following script would is the equivalent of
;       setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
;          find /path/to/sessions -cmin +24 -type f | xargs rm

In short, it says that if you have determined the session path yourself, the automatic session cleaning system will not work. You need to add a cron job for this. Since CyberPanel adds session files to a different directory when installed, it does not perform automatic session cleaning in its current state. (I don’t know if they will solve this with an update. I will indicate the situation in this topic then. However, even if they solve it with an update, I don’t see any harm in keeping the cron command. Other panels use a session timeout of around 1 hour. We set it to 120 minutes.)

The part cmin +120 indicates sessions older than how many minutes will be deleted.
The part 0 0 * * * indicates it will run once a day, which is sufficient. You can modify the code to run once every 12 hours, etc., depending on the situation.
For a CyberPanel server using CentOS 7, we provided the necessary code to edit crontab with the nano editor.

Let’s provide it here as well.

Step 1: We log into SSH. Then:

EDITOR=nano crontab -e

We opened the cron file with nano. We add the following code to the bottom line.
Step 2: We add our command to the crontab.

0 0 * * * find /var/lib/php/session -cmin +120 -type f | xargs rm > /dev/null

Step 3: We restart the crontab service.

service crond restart

Step 4: We test if it is working.

run-parts /etc/cron.daily

Run the command. After a while

cd /var/lib/php/session

switch to the folder. The folder size will have decreased significantly.

dir

You can list the contents with this. The situation will also be apparent from the server disk usage. Recently, on an e-commerce site, I cleaned approximately 30 GB of session files using the method above. If your site usage is heavy, using this is inevitable. I recommend using it on all CyberPanel servers. After all, even WordPress admin logins, etc., are written to the session and take up unnecessary space.