剛才在看一個有關php的talk slide時,發現一個簡單優化php於Apache存取效率的方法。
也許有朋友已經很早就知道了~ 不過熊小真的沒想過這有分別…(果然是要多多看多多學)
於apache setting file內 (e.g. /etc/httpd/conf/httpd.conf)
找
<Directory /var/www>
DirectoryIndex index.htm index.html
</Directory>
改成
<Directory /var/www>
DirectoryIndex index.php index.htm index.html
</Directory>
重點是index.php必須加到最前 (熊小之前是加到htm與html之間), 如果是cgi, pl其實都一樣,加到最前。令apache最先存取。
原理
strace your web server process
% /usr/sbin/apache2 -X &
[1] 16367
(hit page a few times to warm up caches)
% strace -p 16367 -o sys1.txt
Process 16367 attached - interrupt to quit
(hit page once)
Process 16367 detached
% grep stat sys1.txt | grep -v fstat | wc -l
153
Common Mistake
stat64("/var/www/index.html", 0xbfd279ac) = -1 ENOENT (No such file or directory)
stat64("/var/www/index.cgi", 0xbfd27afc) = -1 ENOENT (No such file or directory)
stat64("/var/www/index.pl", 0xbfd27afc) = -1 ENOENT (No such file or directory)
stat64("/var/www/index.php", {st_mode=S_IFREG|0664, st_size=7198, ...}) = 0