您的位置:首页 > 理论基础 > 计算机网络

php性能优化之设置php session多级目录(原文http://www.bewyn.com/archives/44)

2016-12-14 22:59 190 查看
我们知道在php里面,session默认保存的路径都是在/tmp/session或/var/lib/php5,这样就造成了这个目录里面的文件数太多,引发了PHP性能问题,所以我们可以设置多级目录,当然也可以把session放在mysql或者redis,这里我们只说分级目录,后续会讲mysql及redis方案。
首先,修改 php.ini的 session.save_path 选项修改如下:
session.save_path = “2;/tmp/session“  //这里设置2级就可以
session.hash_function = 1 //默认是为0(md5),这里设置为1(sha1)
session.hash_bits_per_character = 5 //指定在SID字符串中的每个字符内保存多少bit  4: 0-9, a-f ;5: 0-9, a-v; 6: 0-9, a-z, A-Z, “-“, “,”
找到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 /home
#./mod_files.sh /var/lib/php5 2 5 //参数表示 存放路径, 几级目录,每个目录生成多少个目录。
其中一点,需要注意其存储目录(/tmp/session或/var/lib/php5),要有相应的执行权限,可以给777。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 性能