php安全配置记录和常见错误梳理(总结)

2019-05-01 20:36:15王冬梅

------------------------Nginx+Php中限制站点目录防止跨站的配置方案记录(使用open_basedir)-------------------

方法1)在Nginx配置文件中加入:

fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/proc/"; 

通常nginx的站点配置文件里用了include fastcgi.conf;,这样的,把这行加在fastcgi.conf里就OK了。

如果某个站点需要单独设置额外的目录,把上面的代码写在include fastcgi.conf;这行下面就OK了,会把fastcgi.conf中的设置覆盖掉。

这种方式的设置需要重启nginx后生效。

方法2)在php.ini中加入

[HOST=www.wangshibo.com] 
open_basedir=/home/www/www.wangshibo.com:/tmp/:/proc/
[PATH=/home/www/www.wangshibo.com] 
open_basedir=/home/www/www.wangshibo.com:/tmp/:/proc/ 

这种方式的设置需要重启php-fpm后生效。

方法3)在网站根目录下创建.user.ini文件,并在该文件中写入下面信息:

open_basedir=/home/www/www.wangshibo.com:/tmp/:/proc/ 

这种方式不需要重启nginx或php-fpm服务。安全起见应当取消掉.user.ini文件的写权限。

php.ini中建议禁止的函数如下:

disable_functions = pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority, eval, popen, passthru, exec, system, shell_exec, proc_open, proc_get_status, chroot, chgrp, chown, ini_alter, ini_restore, dl, pfsockopen, openlog, syslog, readlink, symlink, popepassthru, stream_socket_server, fsocket, chdir

----------------------------------------------php启动后,9000端口没有起来?--------------------------------------------

问题描述:

php服务安装后,启动php-fpm,启动的时候没有报错。然后ps -ef|grep php没有发现进程起来,lsof -i:9000发现端口也没有起来。

查看日志,发现系统所允许打开的文件数超过了预定设置。

[root@i-v5lmgh7y etc]# /usr/local/php/sbin/php-fpm 
[root@i-v5lmgh7y etc]# ps -ef|grep php 
[root@i-v5lmgh7y etc]#lsof -i:9000 
[root@i-v5lmgh7y etc]# 
 
查看错误日志发现问题: 
[root@i-v5lmgh7y log]# tail -f php-fpm.log 
[15-Nov-2015 23:53:15] NOTICE: fpm is running, pid 18277 
[15-Nov-2015 23:53:15] ERROR: failed to prepare the stderr pipe: Too many open files (24) 
[15-Nov-2015 23:53:16] NOTICE: exiting, bye-bye! 
[15-Nov-2015 23:53:59] NOTICE: fpm is running, pid 18855 
[15-Nov-2015 23:53:59] ERROR: failed to prepare the stderr pipe: Too many open files (24) 
[15-Nov-2015 23:54:00] NOTICE: exiting, bye-bye! 
 
发现是系统允许打开的文件数超了预定的设置。需要调大这个值: 
[root@i-v5lmgh7y etc]# ulimit -n 
1024 
[root@i-v5lmgh7y etc]# ulimit -n 65535  //临时解决办法 
[root@i-v5lmgh7y etc]# ulimit -n 
65535 
 
 
永久解决办法: 
在/etc/security/limits.conf文件底部添加下面四行内容: 
[root@i-v5lmgh7y etc]# cat /etc/security/limits.conf 
......... 
# End of file 
* soft nproc unlimited 
* hard nproc unlimited 
* soft nofile 65535 
* hard nofile 65535 
 
然后再次启动php-fpm程序,9000端口就能正常启动了 
[root@i-v5lmgh7y etc]# /usr/local/php/sbin/php-fpm 
[root@i-v5lmgh7y etc]# ps -ef|grep php 
root 21055 1 0 00:12 ? 00:00:00 php-fpm: master process 
(/usr/local/php/etc/php-fpm.conf) 
nobody 21056 21055 0 00:12 ? 00:00:00 php-fpm: pool www 
nobody 21057 21055 0 00:12 ? 00:00:00 php-fpm: pool www								 
			 
相关文章 大家在看