Common security settings of the PHP configuration file PHP. ini

Source: Internet
Author: User

PHP itself has some problems with the old version, such as some serious bugs before php4.3.10 and php5.0.3, 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, PHPCodeWriting is on the one hand, and PHP configuration is even more critical.

We manually install PHP. The default configuration file of PHP is in/usr/local/apache2/CONF/PHP. INI, we mainly need to configure PHP. the content in ini makes it safer to execute PHP. The security settings in PHP are mainly used to prevent phpshell and SQL injection attacks. Let's take a look at them. Use any editing tool to open/etc/local/apache2/CONF/PHP. ini. If you install it in other ways, the configuration file may not be in this directory.

(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.chinaz.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.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.