The traffic on one server is large. due to the needs of the program, the session expiration time is set to 3 hours, resulting in the accumulation of nearly 0.2 million session files under/tmp. As a result, the cpu usage of the kernel increases sharply. Because session read/write involves the random reading and writing of a large number of small files and is concentrated in a directory, iowait also increases sharply. First consider the ses "> <LINKhref =" http:
The traffic on one server is large. due to the needs of the program, the session expiration time is set to 3 hours, resulting in the accumulation of nearly 0.2 million session files under/tmp. As a result, the cpu usage of the kernel increases sharply. Because session read/write involves the random reading and writing of a large number of small files and is concentrated in a directory, iowait also increases sharply.
First, consider putting the session into the memory. The simplest way is to mount/tmp to the tmpfs file system, that is, in the memory.
For details, see using memory as a temporary folder in linux.
Step 2: store the session in an inaccessible Directory
Php itself supports multi-level hash of sessions
In php. ini, change; session. save_path =/tmp
Session. save_path = "2;/tmp/session"
Stores the session in the/tmp/session folder and uses 2 and hashes.
Save and exit. restart php after step 3.
Step 3: Create a session storage folder
Php does not automatically create these folders, but some scripts for creating folders are provided in the source file. The script below is also useful
I = "0 1 2 3 4 5 6 7 8 9 a B c d e f"
For acm in $ I;
Do
For x in $ I;
Do
Mkdir-p/tmp/session/$ acm/$ x;
Done;
Done
Chown-R nobody: nobody/tmp/session
Chmod-R 1777/tmp/session
Because/tmp is memory used, after the server is restarted, all files in it will be lost. Therefore, you need to add the above script to/etc/rc. local, and before starting php
Step 4: reclaim the session
The session will expire after it passes through session. gc_maxlifetime, but will not be deleted immediately. after a long time,/tmp space will be greatly occupied. The specific deletion algorithm is too lazy to study. The following command deletes expired sessions. the Expiration Time defined here is 3 hours.
Find/tmp/session-amin + 180-exec rm-rf {}\;
Put it in cron and execute it once every 10 minutes.