Home Backend Development PHP Tutorial Linux服务器,PHP的10大安全配置实践

Linux服务器,PHP的10大安全配置实践

Jun 23, 2016 pm 01:41 PM

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中的模块。来看看下面这个被执行命令安装的模块。

1    # php ?m
Copy after login

你将会得到类似的结果:

你将会得到类似的结果:

[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

1    # rm /etc/php.d/sqlite3.ini
Copy after login

或者

1    # mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disableRestrict
Copy after login

2. 使PHP信息泄露最小化

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

HTTP示例:

1    HTTP/1.1 200 OK2    X-Powered-By: PHP/5.2.103    Content-type: text/html; charset=UTF-84    Vary: Accept-Encoding, Cookie5    X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;string-contains=wikiToken;6    string-contains=wikiLoggedOut;string-contains=wiki_session7    Last-Modified: Thu, 03 Nov 2011 22:32:55 GMT8    ...
Copy after login

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

1    expose_php=Off
Copy after login

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
Copy after login

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

1    # mv gd.{disable,ini}2    # /sbin/service httpd restart
Copy after login

4. 记录PHP错误信息

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

1    display_errors=Off
Copy after login

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

1    log_errors=On2    error_log=/var/log/httpd/php_scripts_error.log
Copy after login

5. 禁用远程执行代码

如果远程执行代码,允许PHP代码从远程检索数据功能,如FTP或Web通过PHP来执行构建功能。比如:file_get_contents()。

很多程序员使用这些功能,从远程通过FTP或是HTTP协议而获得数据。然而,此法在基于PHP应用程序中会造成一个很大的漏洞。由于大部分程序员 在传递用户提供的数据时没有做到适当的过滤功能,打开安全漏洞并且创建了代码时注入了漏洞。要解决此问题,需要禁用_url_fopen in /etc/php.d/security.ini,并设置以下命令:

1    allow_url_fopen=Off
Copy after login

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

1    allow_url_include=Off
Copy after login

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
Copy after login

7. 资源控制

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

1    # set in seconds2    max_execution_time = 303    max_input_time = 304    memory_limit = 40M
Copy after login

8. 限制PHP访问文件系统

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

1    ; Limits the PHP process from accessing files outside2    ; of specifically designated directories such as /var/www/html/3    open_basedir="/var/www/html/"4    ; ------------------------------------5    ; Multiple dirs example6    ; open_basedir="/home/httpd/vhost/cyberciti.biz/html/:/home/httpd/vhost/nixcraft.com/html/:/home/httpd/vhost/theos.in/html/"7    ; ------------------------------------
Copy after login

9.限制文件/目录访问

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

1    # chown -R apache:apache /var/www/
Copy after login

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

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

1    # chattr +i /etc/php.ini2    # chattr +i /etc/php.d/*3    # chattr +i /etc/my.ini4    # chattr +i /etc/httpd/conf/httpd.conf5    # chattr +i /etc/
Copy after login

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

1    # chattr +i /var/www/html/file1.php2    # chattr +i /var/www/html/
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP scripts to implement cross-server file transfer on Linux servers How to use PHP scripts to implement cross-server file transfer on Linux servers Oct 05, 2023 am 09:06 AM

Title: PHP script implementation of cross-server file transfer 1. Introduction In cross-server file transfer, we usually need to transfer files from one server to another. This article will introduce how to use PHP scripts to implement cross-server file transfer on Linux servers, and give specific code examples. 2. Preparation Before starting to write PHP scripts, we need to ensure that the following environment has been configured on the server: Install PHP: Install PHP on the Linux server and ensure that the PHP version meets the code requirements.

How to deploy a trustworthy web interface on a Linux server? How to deploy a trustworthy web interface on a Linux server? Sep 09, 2023 pm 03:27 PM

How to deploy a trustworthy web interface on a Linux server? Introduction: In today's era of information explosion, Web applications have become one of the main ways for people to obtain information and communicate. In order to ensure user privacy and information reliability, we need to deploy a trustworthy Web interface on the Linux server. This article will introduce how to deploy a web interface in a Linux environment and provide relevant code examples. 1. Install and configure the Linux server. First, we need to prepare a Li

How to optimize the performance and resource utilization of Linux servers How to optimize the performance and resource utilization of Linux servers Nov 07, 2023 pm 02:27 PM

How to optimize the performance and resource utilization of Linux servers requires specific code examples. Summary: Optimizing Linux server performance and resource utilization is the key to ensuring stable and efficient server operation. This article will introduce some methods to optimize Linux server performance and resource utilization, and provide specific code examples. Introduction: With the rapid development of the Internet, a large number of applications and services are deployed on Linux servers. In order to ensure the efficient and stable operation of the server, we need to optimize the performance and resource utilization of the server to achieve

Linux server failure and security: How to manage your system healthily Linux server failure and security: How to manage your system healthily Sep 10, 2023 pm 04:02 PM

With the development of Internet technology, more and more enterprises and individuals choose to use Linux servers to host and manage their applications and websites. However, as the number of servers increases, server failures and security issues become an urgent task. This article will explore the causes of Linux server failures and how to manage and protect the system healthily. First, let's take a look at some common reasons that can cause Linux servers to malfunction. Firstly, hardware failure is one of the most common reasons. For example, the server is overheating,

Linux Server Security: Use Commands to Check System Vulnerabilities Linux Server Security: Use Commands to Check System Vulnerabilities Sep 08, 2023 pm 03:39 PM

Linux Server Security: Using Commands to Check System Vulnerabilities Overview: In today’s digital environment, server security is crucial. Timely detection and repair of known vulnerabilities can effectively protect servers from potential attack threats. This article will introduce some commonly used commands that can be used to check system vulnerabilities on Linux servers and provide relevant code examples. By using these commands correctly, you will be able to enhance the security of your server. Check for system updates: Before you start checking for vulnerabilities, make sure your system has

Providing Stronger Web Interface Security: Key Practices for Linux Servers. Providing Stronger Web Interface Security: Key Practices for Linux Servers. Sep 08, 2023 pm 12:51 PM

Providing Stronger Web Interface Security: Key Practices for Linux Servers Web interface security has become increasingly important in today’s digital age. As more and more applications and services move to the cloud, server security protection is increasingly becoming a critical issue. As one of the most commonly used server operating systems, Linux's security protection is crucial. This article will introduce some key practices to help you provide stronger web interface security. Updating and maintaining operating systems and software Timely updates of operating systems and software are services

Linux server security in action: using command line tools for defense Linux server security in action: using command line tools for defense Sep 09, 2023 pm 12:51 PM

Linux server security practice: using command line tools for defense Introduction: As a Linux server administrator, we must always protect the security of the server. In daily work, using command line tools to defend servers is a simple and efficient method. This article will introduce some commonly used command line tools and give corresponding code examples to help administrators strengthen server security. 1. Firewall settings Firewall is an important tool to protect the server from malicious attacks. The commonly used firewall tool in Linux systems is i

How to automate operations on Linux servers via PHP scripts How to automate operations on Linux servers via PHP scripts Oct 05, 2023 am 10:09 AM

How to automate operations on a Linux server through PHP scripts. On a Linux server, you can use PHP scripts to implement various automated operations, such as database backup, scheduled tasks, file management, etc. Next, we will introduce how to use PHP scripts to implement these automated operations and provide specific code examples. Backing up the database Database backup is an important task of server management. The function of automatically backing up the database can be realized through PHP scripts. Here is an example of a simple PHP script to back up a database: &l

See all articles