Total Pageviews

Friday, 15 April 2016

PHP 安全与优化杂记

安全
禁用一些系统相关的函数
php.ini
disable_functions = system,exec,shell_exec,passthru,proc_open,proc_close, proc_get_status,checkdnsrr,getmxrr,getservbyname,getservbyport, syslog,popen,show_source,highlight_file,dl,socket_listen,socket_create,socket_bind,socket_accept, socket_connect, stream_socket_server, stream_socket_accept,stream_socket_client,ftp_connect, ftp_login,ftp_pasv,ftp_get,sys_getloadavg,disk_total_space, disk_free_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
关闭服务器响应中的 PHP 相关信息
php.ini
expose_php = Off
禁止 fopen 打开 URL
php.ini
allow_url_fopen = Off
限制内存耗用
php.ini
memory_limit = 128M
禁用内存溢出报告
php.ini
report_memleaks = Off
性能
优化 opcache
php.ini
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
增大上传文件大小限制
php.ini
upload_max_filesize = 8M
使用 igbinary + redis 存储 session
安装 igbinary, perl-redis
$ emerge igbinary pecl-redis
php.ini
session.save_handler = redis
session.save_path = "/path/to/redis.sock"
session.serialize_handler = igbinary

opcache 忽略注释
php.ini
opcache.save_comments=0
opcache.load_comments=0

本地 MySQL 使用 socket 连接
php.ini
pdo_mysql.default_socket=/path/to/mysqld.sock

mysql.default_socket=/path/to/mysqld.sock