From: http://www.phpchina.com/archives/view-42868-1.html
PHP is increasingly used! Security issues are even more important! This section describes how to configure PHP. ini for security.
Security Configuration 1 (1) Enable the PHP security mode The security mode of PHP is a very important embedded security mechanism that can control some PHP functions, such as system (), At the same time, many file operation functions are subject to permission control, and files of some key files are not allowed, such as/etc/passwd, However, the default PHP. ini mode does not enable the security mode. Open it: Safe_mode = on (2) user group security When safe_mode is enabled and safe_mode_gid is disabled, the PHP script can access the file and Group users can also access files. Recommended settings: Safe_mode_gid = off If you do not set it, we may not be able to operate the files under the website directory of our server. For example, we need During file operations. (3) execution in Security ModeProgramMain directory If security mode is enabled, but you want to execute some programs, you can specify the main directory of the program to be executed: Safe_mode_exec_dir = D:/usr/bin Generally, you do not need to execute any program. Therefore, we recommend that you do not execute the system program Directory, which can point to a directory, Then copy the program to be executed, for example: Safe_mode_exec_dir = D:/tmp/CMD However, I recommend that you do not execute any program, so you can point to our webpage directory: Safe_mode_exec_dir = D:/usr/WWW (4) file inclusion in Security Mode If you want to include some public files in safe mode, modify the following options: Safe_mode_include_dir = D:/usr/www/include/ In fact, the files contained in the PHP script are all written in the program itself, which can be set as needed. (5) control directories accessible by PHP scripts You can use the open_basedir option to control the PHP script to access only the specified directory, so as to avoid PHP script access. Files that should not be accessed limit the harm of phpshell to a certain extent. We can generally set to only access the website directory: Open_basedir = D:/usr/WWW (6) Disable dangerous functions If the security mode is enabled, the function is not required, but we should consider it for security. For example, We do not want to execute a PHP function that includes system () and so on that can execute commands, or can view PHP information. Phpinfo () and other functions, we can disable them: Disable_functions = system, passthru, exec, shell_exec, popen, phpinfo If you want to disable operations on any files and directories, you can disable many file operations. Disable_functions = chdir, chroot, Dir, getcwd, opendir, readdir, scandir, fopen, unlink, delete, copy, mkdir, rmdir, rename, file, file_get_contents, fputs, fwrite, chgrp, chmod, chown The above only lists some file processing functions that are not commonly used. You can also combine the preceding command functions with this function, You can resist most phpshells. (7) Disable PHP version information leakage in the HTTP Header To prevent hackers from obtaining information about the PHP version on the server, we can disable this information in the HTTP header: Expose_php = off For example, when hackers Telnet www.12345.com 80, they will not be able to see the PHP information. (8) Disable registration of global variables Variables submitted in PHP, including those submitted using post or get, are automatically registered as global variables and can be directly accessed, This is very insecure for the server, so we can disable the register global variable option if we cannot register it as a global variable: Register_globals = off Of course, if this is set, a reasonable way should be used to obtain the corresponding variable, such as getting the variable VAR submitted by get, You need to use $ _ Get ['var'] to obtain it. This PHP programmer should pay attention to it. (9) Enable magic_quotes_gpc to prevent SQL injection. SQL injection is a very dangerous problem. In small cases, the website background is intruded, while in heavy cases, the entire server is compromised, So be careful. PHP. INI has a setting: Magic_quotes_gpc = off This is disabled by default. If it is enabled, it will automatically convert the SQL query submitted by the user, For example, convert 'to \' to prevent SQL injection. Therefore, we recommend the following settings: Magic_quotes_gpc = on (10) error message control In general, PHP prompts an error when it is not connected to the database or in other cases. The common error message will contain the PHP script when The preceding path information or the queried SQL statement information is not safe after the information is provided to the hacker. Therefore, it is recommended that the server disable the error prompt: Display_errors = off If you want to display the error information, you must set the Display error level. For example, only the warning information is displayed: Error_reporting = e_warning & e_error Of course, we recommend that you disable the error message. (11) Error Log We recommend that you record the error information after you disable display_errors to find out the reason for running the server: Log_errors = on At the same time, you must set the directory where error logs are stored. It is recommended that the logs of the root Apache exist together: Error_log = D:/usr/local/apache2/logs/php_error.log Note: You must grant write permissions to Apache users and groups. MySQL downgrading operation Create a user, such as mysqlstart Net user mysqlstart ***** Microsoft/Add Net localgroup users mysqlstart/del Does not belong to any group If MySQL is installed in D: \ mysql, grant full control of MySQL start. Then, set the MySQL service attribute in the system service. In the logon attribute, select this user mysqlstart and enter the password to confirm. Restart the MySQL service and MySQL runs under low permissions. If you build Apache on the WINDOS platform, note that Apache runs with the system permission by default, This is terrible, and it makes people feel uncomfortable. Let's drop Apache permissions. Net user Apache **** Microsoft/Add Net localgroup users Apache/del OK. We have created a user Apche that does not belong to any group. Open the computer manager, select a service, click the apache service attribute, Select log on, select this account, and fill in the account and password created above, Restart the apache service. OK. Apache runs under low permissions. In fact, we can also set the permissions for each folder so that Apache users can only execute what we want it to do, and create a single read/write user for each directory. This is also a popular configuration method for many VM providers. However, this method is used to prevent minor usage. Security Configuration 2 This section describes the basic configuration process of IIS + MySQL + PHP and the basic permission settings for Windows. In this part, we need to discuss the security configuration of PHP and the security configuration of the web directory. Of course, the IIS abnormal security configuration must also be included. Let me talk a few things. Our final goal is that the web site only runs PHP, does not support ASP, does not support Asp.net, so that the specific directory or sub-site cannot execute PHP scripts, sample directory, we set it to unable to run php, so that even if your website is logged on to the background by a "hacker", you can upload files. But in the end, he cannot execute webshell. Even if webshell is obtained, it cannot read directories or files and execute commands. In other words, a powerful webshell does not have any value to use in the hands of hackers, making them crazy and dead. Haha! In fact, it is not very difficult to do this. follow my steps. CompletedArticleYou can configure such abnormal servers independently. I. abnormal configuration of the php. ini file Why do we put PHP. ini at the beginning? Because our web site is Php, many default options are insecure. This leaves a lot of opportunities for hackers to take advantage of. Therefore, the first step is to make the php. ini settings abnormal, so as to prevent attacks by General script hackers. First, let's take a look at some basic concepts of PHP. ini. Blank characters and rows starting with semicolons are ignored. Set the command format as follows: Directive = value command name (Directive) is case sensitive! Therefore, "foo = bar" is different from "foo = bar ". The value can be: 1. A string defined by quotation marks (for example, "foo ") 2. A number (integer or floating point number, for example, 0, 1, 34,-1, 33.55) 3. a PHP constant (for example, e_all, m_pi) 4. An ini constant (On, off, none) 5. An expression (for example, e_all &~ E_notice) Another option is to set a Boolean value. If 1 is on, it is enabled. If 0 is off, It is disabled. PHP. INI is divided into many parts, such as module, PHP global configuration, database configuration, and so on. 1 is a basic example of PHP. ini. After learning about the basic concepts, we can start our abnormal configuration journey.