Obviously, Php+mysql+apache is a very popular web technology, this combination is powerful, scalable, or free. However, the default settings for PHP are not as appropriate for Web sites that are already online. The following changes the security policy of PHP by modifying the default configuration file!
0x01: Disable remote URL file handling feature
File processing functions like fopen, which accept the Rul parameters of the file (for example: fopen (' http://www.yoursite.com ', ' R ').), this feature can easily access remote resources, however, this is a very important security threat, Disabling this feature to restrict the file function is a good choice, as in the php.ini file:
Copy Code code as follows:
0x02: Disable registering global variables
PHP in the previous version of 4.2.0, with global variables as input, this function is called register_globals, in Web applications it causes a lot of security problems, because it allows attackers in some cases can easily manipulate global variables, Fortunately, this feature is disabled by default in 4.2.0, and it is very dangerous, regardless of the circumstances to disable this feature. If some scripts require this functionality, there is a potential security threat to the script. Modify Pnp.ini To disable this feature:
Copy Code code as follows:
0X03: Restrict PHP Read and write operations
In many web development processes, PHP scripts need to read and write to the local file system, such as/var/www/htdocs/files, in order to enhance security, you can modify the local file read and Write permissions:
Copy Code code as follows:
Open_basedir =/var/www/htdocs/files
0x04:posing Limit
Restricting PHP execution time, memory usage, post, and upload data is the best policy and can be configured as follows:
Copy Code code as follows:
Max_execution_time = 30; Max Script Execution Time
Max_input_time = 60; Max time spent parsing input
Memory_limit = 16M; Max memory used by one script
Upload_max_filesize = 2M; Max Upload File size
Post_max_size = 8M; Max Post Size
0X05: Disable error messages and enable logging
In the default setting, PHP prints the error message to the browser, which is the most reasonable configuration during the development of the application, but it can also disclose some security information to the user, such as the installation path and user name. In a Web site that has been developed, it is a good idea to disable the error message and then output the error message to the log file.
Copy Code code as follows:
Display_errors = Off
Log_errors = On
0x06: Hide php files
If there are no hidden php files, we can get a version of the server PHP in a variety of ways, such as using: http://www.example.com/script.php?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000
Obviously, we don't want users to have direct access to the PHP version of your Web server, and fortunately, there's a switch in php.ini that disables this feature:
Copy Code code as follows:
0X07: Safe Mode configuration
In the default case, PHP can be configured as Safe mode, in this mode, Apache prohibit access to files, environment variables and binaries, in safe mode, the biggest problem is that only the owner of the file to access this PHP file, if a lot of developers to develop this program, This setting is impractical, when you need to access a PHP file, you need to modify the owner of the file, another problem is that other programs do not access these PHP files, the following configuration can modify the file's permissions for the user group rather than individual users.
Copy Code code as follows:
Safe_mode = Off
Safe_mode_gid = On
By enabling Safe_mode_gid, you can use this group of Apache to access PHP files. Safe mode is also very effective at blocking the execution of binaries, but developers want to be able to run some binaries under certain circumstances. In these special situations, you can put the binaries into a directory, such as (/var/www/binaries), and you can set the following:
Copy Code code as follows:
Safe_mode_exec_dir =/var/www/binaries
Finally, with the following settings, you can access the server's environment variables and provide a prefix with "_" so that you can access only the environment variables with the specified prefix:
Copy Code code as follows:
Safe_mode_allowed_env_vars = Php_
0x08: Restricting public users ' access to files with a specific suffix name
For security reasons, many files with a specific suffix name cannot be accessed by public users, for example, the. inc suffix file, which contains some sensitive information, such as MySQL connection information, if not properly configured, then each user can access this profile, in order to enhance the security of the site, you need to be in. The. htaccess file is configured as follows:
Copy Code code as follows:
<filesmatch>
Order Allow,deny
Deny from all
</filesmatch>
0X09: summary
The default configuration for PHP is developer-oriented, and it is recommended that you reconfigure PHP if the site is for a wide range of users.