PHP security configuration is a very powerful server-side scripting language, but the powerful functions are always accompanied by significant risks. the old version of PHP has some problems, for example, php4.3.10 and php5.0.3 have some serious bugs, so we recommend that you use the new version. In addition, the vigorous SQL Injection is also widely used in PHP, so to ensure security, PHP code writing is one aspect, and PHP configuration is even more critical. In this section, you will learn how to modify the PHP. ini file to prevent some potential PHP risk factors.
1. php. ini file selection
Set a valid PHP configuration file, php. ini. The package contains two ini files: php. ini-dist and php. ini-recommended. We recommend that you use php. ini-recommended because this file optimizes the performance and security of the default settings. Carefully read the instructions in this document and study the ini settings chapter to manually set each project. If you want to achieve the best security effect, you 'd better use this file, although PHP works well under the default ini file. Copy the selected ini file to the directory that PHP can find and change it to php. ini. By default, PHP searches for php. ini in the Windows Directory:
In Windows 9x/ME/XP, copy the selected ini file to % WINDIR %, usually c: \ windows.
In Windows NT/2000, copy the selected ini file to % WINDIR % or % SYSTEMROOT %. generally, c: \ winnt or c: \ winnt40 corresponds to the server version.
If NTFS is used in Windows NT, 2000, or XP, make sure that the user name running webserver has the permission to read php. ini (for example, to make it readable to Everyone ).
Php. ini-dist is generally used for program development.
Php. ini-recommended is used for online services.
2. modify the php. ini file
The security mode of php is a very important embedded security mechanism that can control some functions in php, such as system () and control permissions for many File operation functions, some key files, such as/etc/passwd, are not allowed, but the default php. ini does not enable the security mode. we can open it.
1. change "safe_mode = off" to "safe_mode = on"
Shell_exec () and ''functions that can execute system commands are prohibited. other execution functions include exec (), system (), passthru (), and popen () only programs in the directory specified by safe_mode_exec_dir can be executed. If you want to execute some commands or programs, find the following:
Safe_mode_exec_dir =
Specifies the path of the program to be executed, for example:
Safe_mode_exec_dir = c: \ wwwroot
Then, copy the program to the c: \ wwwroot Directory. in this way, the restricted function can execute the program in the directory.
2. change "display_errors = on" to "display_errors = off"
Generally, php prompts an error when it is not connected to the database or in other cases. the error message contains the current path information of the php script or the queried SQL statement, this type of information is insecure when it is provided to hackers. Therefore, we recommend that you disable error messages on servers.
Display_errors = Off
Log_errors = On
You must also set the directory where the error logs are stored to find the following line.
; Error_log = filename
Remove the comment and change filename to the specified file.
Error_log = D:/usr/php_error.log
3. change "disable_functions =" to "disable_functions = phpinfo, system, exec, passthru, shell_exec, popen, is_dir ".
We do not want to execute php functions that can execute commands, including system (), or phpinfo () functions that can view php information, so we can disable them:
4. search: change magic_quotes_gpc to On if it is Off.
Enable magic_quotes_gpc to prevent SQL injection.
5. search: register_globals
Variables submitted in PHP, including those submitted using POST or GET, will be automatically registered as global variables for direct access, which is very insecure for the server, therefore, if we cannot register it as a global variable, we disable the register global variable option:
6. search: Add/www // * after open_basedir. note: www is the file stored in the website program */
This option can disable file operations outside the specified directory, and effectively eliminate attacks by calling functions such as include () to local files or remote files.
7. set expose_php to off, so that php will not disclose information in the http file header.
To prevent hackers from obtaining information about the php version on the server, we can disable this information in the http header.
8. set "allow_url_fopen" to "off" to disable the remote file function.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service