當一個目錄下有很多檔案時,伺服器的處理效能會變低,php預設的session僅僅存放在/tmp目錄下,未進行分級,當有一定的訪問量時,就存在效能問題了。
首先,修改 php.ini的 session.save_path 選項修改如下:
session.save_path = “2;/tmp/session” (去掉前面分號)
表示把session存放在 “/tmp/session” 目錄下,並且分成 2 級子目錄
一般情況下2級目錄就夠了,就能夠處理相當大的訪問量了
———————–
其他注釋
session.hash_function = 0
; 產生SID的散列演算法。SHA-1的安全性更高一些
; 0: MD5 (128 bits)
; 1: SHA-1 (160 bits)
; 建議使用SHA-1。
session.hash_bits_per_character = 4
; 指定在SID字串中的每個字元內儲存多少bit,
; 這些位元是hash函數的運算結果。
; 4: 0-9, a-f
; 5: 0-9, a-v
; 6: 0-9, a-z, A-Z, “-“, “,”
; 建議值為 5
————————–
php源碼檔案中ext/session/mod_files.sh檔案,可以輔助組建目錄,就不用自己在寫指令碼了
#! /bin/sh
if test "$2" = ""; then
echo "usage: $0 basedir depth"
exit 1
fi
if test "$2" = "0"; then
exit 0
fi
hash_chars="0 1 2 3 4 5 6 7 8 9 a b c d e f"
if test "$3" -a "$3" -ge "5"; then
hash_chars="$hash_chars g h i j k l m n o p q r s t u v"
if test "$3" -eq "6"; then
hash_chars="$hash_chars w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z - ,"
fi
fi
for i in $hash_chars; do
newpath="$1/$i"
mkdir $newpath || exit 1
sh $0 $newpath `expr $2 - 1` $3
done
設定為可執行之後,運行以下命令來建立雜湊目錄:
#cd /root/soft_install/php-5.3.5/ext/session
#./mod_files.sh /tmp/session 2 5
三個參數依次表示,存放路徑, 幾級目錄,每個目錄產生多少個目錄(參考session.hash_bits_per_character)
另外需要注意修改 /tmp/session的許可權,保證運行php的帳號有許可權讀寫
php中設定多級目錄session的問題
在 php.ini 中找到 session.save_path 將值設定為 session.save_path = '3;/tmp/session'; 即可開啟三級目錄儲存session。但是php不會自動組建目錄結構,這時可以藉助源碼包 ext/session 目錄下的 mod_files.sh 來組建目錄
$ bash mod_files.sh /tmp/session 3
產生完成後發現仍然不能產生session,糾結了半天,開啟mod_files.bat才發現玄機,原來後面還需要帶一個參數,對應於 php.ini 中的 session.hash_bits_per_character ,這個值預設是4,development和production版本的預設配置裡是5,於是用下面的命令從新組建目錄
$ bash mod_files.sh /tmp/session 3 5
終於可以登入了。