Linux安全centos php.ini 配置

| |
[ 2019/05/11 09:09 | by admin ]
PHP被广泛用于各种Web开发。而当服务器端脚本配置错误时会出现各种问题。现今,大部分Web服务器是基于Linux环境下运行(比如:Ubuntu,Debian等)。本文例举了十大PHP最佳安全实践方式,能够让您轻松、安全配置PHP。


PHP安全性设置提示:
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内置模块

为了增强性能和安全性,强烈建议,减少PHP中的模块。来看看下面这个被执行命令安装的模块。



# php –m

你将会得到类似的结果:

[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

删除一个模块,并执行此命令。例如:删除模块sqlite3


# rm /etc/php.d/sqlite3.ini

或者


# mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disableRestrict

2. 使PHP信息泄露最小化

在默认PHP时在HTTP抬头处会生成一条线介于每个响应中,(比如X-Powered-By: PHP/5.2.10)。而这个在系统信息中为攻击者创建了一个非常有价值的信息。

HTTP示例:


HTTP/1.1 200 OK

X-Powered-By: PHP/5.2.10

Content-type: text/html; charset=UTF-8

Vary: Accept-Encoding, Cookie

X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;string-contains=wikiToken;

string-contains=wikiLoggedOut;string-contains=wiki_session

Last-Modified: Thu, 03 Nov 2011 22:32:55 GMT

...
因此,我们强烈建议,禁止PHP信息泄露,想要要禁止它,我们要编辑/etc/php.d/secutity.ini,并设置以下指令:


expose_php=Off

3. 使PHP加载模块最小化

在默认情况下,RHEL加载的所有模块可以在/etc/php.d/目录中找到。要禁用或启用一个特定的模块,只需要在配置文件/etc /php.d/目录中中注释下模块名称。而为了优化PHP性能和安全性,当你的应用程序需要时,我们强烈建议建议启用扩展功能。举个例子:当禁用GD扩展 时,键入以下命令:


# cd /etc/php.d/

# mv gd.{ini,disable}

# /etc/init.d/apache2 restart

为了扩展PGP GD模块,然后键入以下命令:


# mv gd.{disable,ini}

# /sbin/service httpd restart

4. 记录PHP错误信息

为了提高系统和Web应用程序的安全,PHP错误信息不能被暴露出。要做到这一点,需要编辑/etc/php.d/security.ini 文件,并设置以下指令:


display_errors=Off

为了便于开发者Bug修复,所有PHP的错误信息都应该记录在日志中。


log_errors=On

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,并设置以下命令:


allow_url_fopen=Off

除了这个,我还建议禁用_url_include以提高系统的安全性。


allow_url_include=Off

6. 禁用PHP中的危险函数

PHP中有很多危险的内置功能,如果使用不当,它可能使你的系统崩溃。你可以创建一个PHP内置功能列表通过编辑/etc/php.d/security.ini来禁用它,去掉;为启用禁止以下查询参数。


disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

7. 资源控制

为了提高系统的稳定性,强烈建议设置每个脚本解析请求数据所花费的时间和脚本可能消耗的最大内存量。正确的配置这些参数可以防止PHP任何脚本消耗太多的资源或是内存,从而避免系统不安全或降低安全系数。


# set in seconds

max_execution_time = 30

max_input_time = 30

memory_limit = 64M

8. 限制PHP访问文件系统

该open_basedir指令指定的目录是允许PHP访问使用fopen()等功能。如果任何脚本试图访问超出open_basdir定义的路径文件,PHP将拒绝打开。值得注意的是,你不能使用一个符号链接作为一种变通方法。


; Limits the PHP process from accessing files outside

; of specifically designated directories such as /var/www/html/

open_basedir="/var/www/html/"

; Multiple dirs example

; open_basedir="/home/httpd/vhost/cyberciti.biz/html/:/home/httpd/vhost/nixcraft.com/html/:/home/httpd/vhost/theos.in/html/"


9.限制文件/目录访问

进行适当的安全设置:确保Apache作为非root用户运行,比如www-data或www。对于文件和目录在基于/var/www/下同样属于非root用户。想要更改所有者,执行以下命令:


# chown -R apache:apache /var/www/

10.编译保护Apache,PHP和MySQL的配置文件

使用charrt命令编译保护配置文件


# chattr +i /etc/php.ini

# chattr +i /etc/php.d/*

# chattr +i /etc/my.ini

# chattr +i /etc/httpd/conf/httpd.conf

# chattr +i /etc/

使用charrt命令可以编译保护PHP文件或者是文件中的/var/www/html的目录:


# chattr +i /var/www/html/file1.php

# chattr +i /var/www/html/
PHP笔记 | 评论(0) | 引用(0) | 阅读(5469)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写