Php stores a large number of sessions in the memory, with hash and expiration recycle.

Source: Internet
Author: User
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.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.