PHP被普遍用于各类Web开发。而当服务器端脚本配置错误时会出现各类问题。现今,大部分Web服务器是基于Linux环境下运行(好比:Ubuntu,Debian等)。本文例举了十大PHP最佳安全实践方式,可以让您轻松、安全配置PHP。php
PHP安全性设置提示:html
DocumentRoot: /var/www/
Default Web server: Apache
Default PHP configuration file: /etc/php.ini
Default PHP extensions config directory: /etc/php.d/
Our sample php security config file: /etc/php.d/security.ini (you need to create this file using a text editor)
Operating systems: Ubuntu (the instructions should work with any other Linux distributions such as RHEL / CentOS / Fedora or other Unix like operating systems such as OpenBSD/FreeBSD/HP-UX).
1. 减小PHP内置模块mysql
为了加强性能和安全性,强烈建议,减小PHP中的模块。来看看下面这个被执行命令安装的模块。程序员
1 # php –m
你将会获得相似的结果:sql
你将会获得相似的结果:shell
[PHP Modules]
apc
bcmath
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
imap
json
libxml
mbstring
memcache
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
suhosin
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
Suhosin
删除一个模块,并执行此命令。例如:删除模块sqlite3apache
1 # rm /etc/php.d/sqlite3.ini
或者json
1 # mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disableRestrict
2. 使PHP信息泄露最小化安全
在默认PHP时在HTTP抬头处会生成一条线介于每一个响应中,(好比X-Powered-By: PHP/5.2.10)。而这个在系统信息中为攻击者建立了一个很是有价值的信息。服务器
HTTP示例:
1 HTTP/1.1 200 OK 2 X-Powered-By: PHP/5.2.10 3 Content-type: text/html; charset=UTF-8 4 Vary: Accept-Encoding, Cookie 5 X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;string-contains=wikiToken; 6 string-contains=wikiLoggedOut;string-contains=wiki_session 7 Last-Modified: Thu, 03 Nov 2011 22:32:55 GMT 8 ...
所以,咱们强烈建议,禁止PHP信息泄露,想要要禁止它,咱们要编辑/etc/php.d/secutity.ini,并设置如下指令:
1 expose_php=Off
3. 使PHP加载模块最小化
在默认状况下,RHEL加载的全部模块能够在/etc/php.d/目录中找到。要禁用或启用一个特定的模块,只须要在配置文件/etc /php.d/目录中中注释下模块名称。而为了优化PHP性能和安全性,当你的应用程序须要时,咱们强烈建议建议启用扩展功能。举个例子:当禁用GD扩展 时,键入如下命令:
1 # cd /etc/php.d/ 2 # mv gd.{ini,disable} 3 # /etc/init.d/apache2 restart
为了扩展PGP GD模块,而后键入如下命令:
1 # mv gd.{disable,ini} 2 # /sbin/service httpd restart
4. 记录PHP错误信息
为了提升系统和Web应用程序的安全,PHP错误信息不能被暴露出。要作到这一点,须要编辑/etc/php.d/security.ini 文件,并设置如下指令:
1 display_errors=Off
为了便于开发者Bug修复,全部PHP的错误信息都应该记录在日志中。
1 log_errors=On 2 error_log=/var/log/httpd/php_scripts_error.log
5. 禁用远程执行代码
若是远程执行代码,容许PHP代码从远程检索数据功能,如FTP或Web经过PHP来执行构建功能。好比:file_get_contents()。
不少程序员使用这些功能,从远程经过FTP或是HTTP协议而得到数据。然而,此法在基于PHP应用程序中会形成一个很大的漏洞。因为大部分程序员 在传递用户提供的数据时没有作到适当的过滤功能,打开安全漏洞而且建立了代码时注入了漏洞。要解决此问题,须要禁用_url_fopen in /etc/php.d/security.ini,并设置如下命令:
1 allow_url_fopen=Off
除了这个,我还建议禁用_url_include以提升系统的安全性。
1 allow_url_include=Off
6. 禁用PHP中的危险函数
PHP中有不少危险的内置功能,若是使用不当,它可能使你的系统崩溃。你能够建立一个PHP内置功能列表经过编辑/etc/php.d/security.ini来禁用它。
1 disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
7. 资源控制
为了提升系统的稳定性,强烈建议设置每一个脚本解析请求数据所花费的时间和脚本可能消耗的最大内存量。正确的配置这些参数能够防止PHP任何脚本消耗太多的资源或是内存,从而避免系统不安全或下降安全系数。
1 # set in seconds 2 max_execution_time = 30 3 max_input_time = 30 4 memory_limit = 40M
8. 限制PHP访问文件系统
该open_basedir指令指定的目录是容许PHP访问使用fopen()等功能。若是任何脚本试图访问超出open_basdir定义的路径文件,PHP将拒绝打开。值得注意的是,你不能使用一个符号连接做为一种变通方法。
1 ; Limits the PHP process from accessing files outside 2 ; of specifically designated directories such as /var/www/html/ 3 open_basedir="/var/www/html/" 4 ; ------------------------------------ 5 ; Multiple dirs example 6 ; open_basedir="/home/httpd/vhost/cyberciti.biz/html/:/home/httpd/vhost/nixcraft.com/html/:/home/httpd/vhost/theos.in/html/" 7 ; ------------------------------------
9.限制文件/目录访问
进行适当的安全设置:确保Apache做为非root用户运行,好比www-data或www。对于文件和目录在基于/var/www/下一样属于非root用户。想要更改全部者,执行如下命令:
1 # chown -R apache:apache /var/www/
10.编译保护Apache,PHP和MySQL的配置文件
使用charrt命令编译保护配置文件
1 # chattr +i /etc/php.ini 2 # chattr +i /etc/php.d/* 3 # chattr +i /etc/my.ini 4 # chattr +i /etc/httpd/conf/httpd.conf 5 # chattr +i /etc/
使用charrt命令能够编译保护PHP文件或者是文件中的/var/www/html的目录:
1 # chattr +i /var/www/html/file1.php 2 # chattr +i /var/www/html/