Configure phpopen_basedir on the shared host to improve security and prevent files from being accessed across sites. For more information about the security configuration in nginx/php-fpm/apache mod_php, see my previous article:
Correctly setting nginx/php-fpm/apache to improve website security and prevent Trojans
Http://zhangxugg-163-com.iteye.com/blog/1171572
I. basic knowledge preparation
Before starting any configuration, you must understand these concepts. if these concepts are not fully understood, you will not be able to understand the significance of subsequent configuration. experienced system administrators can skip this step.
1. what is the website file owner's account and the file permission?
2. what account does nginx, php-fpm, and apache use for running?
3. which directories and files on the website are writable? Which of the following statements can be written to nginx? Php-fpm? Or apache?
4. how can I disable php code execution?
The most basic permissions for Linux are: the owner of the file, which has any permissions on all the files to which the file belongs (root permissions are not restricted). the read/write execution permissions are marked as 4 2 1 respectively.
What I see most is that some system administrators allow php-fpm/apache to use the website file owner to facilitate the php program to generate files anywhere, however, such configuration may cause serious security problems.
Reply to the above questions:
1. in the linux Directory, run the ls-l command to view the users/groups of the current directory owner, column 3rd and column 4, respectively. this is the list of files in the protected Directory of the yii Framework.
We can see that the file owner and group are ftp, and the runtime directory permission is for anyone who has read and write permissions.
2. websites often need writable directories to generate files (cache, static files, attachment uploads, etc.) during runtime. Therefore, this writable directory is for php programs, in nginx + php-fpm mode, nginx is only responsible for forwarding requests to the php-fpm process. Therefore, the final file generated is generated by php-fpm. However, Apache mod_php is different. as a module of apache, mod_php inherits its permissions from apache. Therefore, it is not wrong to describe apache's writable directory.
Run the ps aux | egrep 'nginx | php-fpm | apache 'command to view the nginx/php-fpm/apache running account.
3. communicate with developers to understand the writable directory and its significance
4. if php code execution is prohibited? The trojan file may be uploaded to the attachment directory due to a program code vulnerability. do you think you can just remove the execution permission of this directory in linux?
Chmod a-x-R uploadfile
However, apache cannot read the files in this directory, and the attachment files cannot be accessed. On the other hand, this method is also incorrect. Even removing the execution permission of the attachment file itself (the normal file itself does not have the execution permission) does not work. you must first understand the php operating mechanism.
Common php running methods include:
A. run in nginx + php-fpm mode
B. run it in apache mod_php mode.
C. use php under the command line Running
D. add the following in the first line of the php script :#! /Usr/local/php/bin/php and add the execution permission to the file to run it as a shell script. This type of file is not common.
In these methods, only the last method requires the execution permission. in other methods, the php interpreter can run only when the php interpreter has the permission to read the php script. For more information about how to disable php script execution, see the author's article.
II. safa_mode
The security mode of php is controversial because it involves system configuration and uses the configuration idea of windows, which is not suitable in the Linux environment. Simply put, after safe_mode is enabled, the php process itself can only open its own script file. That is to say, in a multi-site environment, we must create a user for each site, and let the php process run with the corresponding account permissions, so that it can have the highest permissions on its own site files, however, you do not have any permissions on files of other users. It sounds good, but in fact this configuration is quite tedious and error-prone:
1. if there are many sites, you need to create a large number of accounts and grant the site directories to these users separately.
2. php-fpm requires creating a large number of process pools and specifying different account identities, resulting in a great waste of resources.
3. apachce cannot be configured with multiple running accounts, but only one can be specified.
4. it is not easy to specify a shared directory (such as/tmp,/dev/shm.
It can be seen that due to the tedious configuration of safe_mode, the new version of php will cancel the support of safe_mode. The PHP Manual has already described it clearly:
5. if the site is to be migrated to another machine, the user account also needs to be fully migrated, which is also a tedious workload.
Security mode is deprecated from PHP 5.3.0 and removed from PHP 5.4.0.
Therefore, the method that relies on the security mode will not be supported in the new php version. when I write this article, php 7.0.2 has been released.
III. why open_basedir?
Open_basedir is used to limit the prefix of directories that php processes can open. generally, in addition to reading files on this site, php programs often use the/tmp/dev/shm directory.
If the site is located in/data/wwwroot/site.cn and the/tmp,/dev/shm,/proc directory is required during runtime, you can set this in php. ini.
Open_basedir =/data/wwwroot/site.cn:/tmp:/dev/shm:/proc
If you use functions such as include, require, fopen, and gzopen in php to load other directory files, an error is returned:
Require_once (): open_basedir restriction in effect. File (file. php) is not within the allowed path (s): (PATH)
Warning: require_once (file. php): failed to open stream: Operation not permitted in
Fatal error: require_once (): Failed opening required...
This parameter can only be configured in php 5.2.3 and cannot be configured at runtime. Php 5.2.3 can be set anywhere.
Obviously, it is difficult to configure multiple php. ini methods for a host with a large number of sites. There are two methods:
1. in the php-fpm.conf profile, specify a different qualified directory for each process pool
[Www]
Php_admin_value [open_basedir] =/var/www/www.example.com:/usr/share/php5:/tmp:/usr/share/phpmyadmin:/etc/phpmyadmin
Php_admin_value indicates that this value cannot be modified at runtime once it is set. However, this method is not recommended, because every site needs to establish a process Pool. if there are 100 sites, 100 process pools need to be created, and these php processes, they cannot be used with each other. they can only be exclusive to the site, resulting in a great waste of resources.
2. when nginx communicates with php-fpm through the fastcgi protocol, you can specify some parameters to modify the configuration parameters of php before the request starts. this is the most convenient and effective method and mandatory recommendation.
Fastcgi_param PHP_ADMIN_VALUE "open_basedir = $ document_root/:/tmp/:/proc/:/dev/shm ";
This is a function unique to php-fpm, indicating that some parameters can be specified from the fastcgi client.
Since the $ document_root variable is introduced, we can directly configure it in batches.
In this way, all sites can share the same process pool and solve the problem of restrict access to directories separately. We can add this command to the fastcgi_params configuration file of nginx, so that all sites will take effect immediately and solve the problem.
You can use the following php code to test
Echo ini_get ('open _ basedir ');
Ini_set ('open _ basedir', '/etc /');
Echo ini_get ('open _ basedir ');
It can be seen that the restriction has been set as expected and cannot be modified at runtime. We can use nginx to specify various php configurations as needed.