Session php fpm和nginx会话问题
过去一周左右我一直有这个问题。我一直在从事一个严重依赖会话的PHP项目。由于某种原因,我们在过去几天的会议中遇到了麻烦。知道为什么吗 以下是错误:Session php fpm和nginx会话问题,session,nginx,php,Session,Nginx,Php,过去一周左右我一直有这个问题。我一直在从事一个严重依赖会话的PHP项目。由于某种原因,我们在过去几天的会议中遇到了麻烦。知道为什么吗 以下是错误: Warning: Unknown: open(/tmp/sess_mmd0ru5pl2h2h9bummcu1uu620, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files).
Warning: Unknown: open(/tmp/sess_mmd0ru5pl2h2h9bummcu1uu620, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
Warning: session_start(): open(/tmp/sess_mmd0ru5pl2h2h9bummcu1uu620, O_RDWR) failed: Permission denied (13)
nginx版本:
nginx version: nginx/1.0.11
PHP-FPM配置:
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
; All relative paths in this configuration file are relative to PHP's install
; prefix.
; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
include=/etc/php-fpm.d/*.conf
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
; Pid file
; Default Value: none
pid = /var/run/php-fpm/php-fpm.pid
; Error log file
; Default Value: /var/log/php-fpm.log
error_log = /var/log/php-fpm/error.log
; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
;log_level = notice
; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
;emergency_restart_threshold = 0
; Interval of time used by emergency_restart_interval to determine when
; a graceful restart will be initiated. This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;emergency_restart_interval = 0
; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0
; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
;daemonize = yes
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
; See /etc/php-fpm.d/*.conf
nginx.conf:
#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki - http://wiki.nginx.org/Main
# * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################
#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
# http://wiki.nginx.org/NginxHttpMainModule
#
#----------------------------------------------------------------------
user nginx nginx;
worker_processes 5;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
#----------------------------------------------------------------------
# Events Module
#
# http://wiki.nginx.org/NginxHttpEventsModule
#
#----------------------------------------------------------------------
events {
worker_connections 4096;
}
#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.nginx.org/NginxHttpCoreModule
#
#----------------------------------------------------------------------
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
index index.php index.html index.htm;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name stats.smilingdevil.com;
error_page 404 /404.php;
root /var/www;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
set $page_to_view "/index.php";
try_files $uri $uri/ @rewrites;
root /var/www/;
index index.php;
}
location @rewrites {
if ($uri ~* ^/([a-z0-9]+)$) {
set $page_to_view "/$1.php";
rewrite ^/([a-z]+)$ /$1.php last;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}
}
}
发生此错误的原因是运行php进程的用户可能没有在/tmp目录上写入的权限 要使其可由所有用户写入,请使用此命令
chmod 777 /tmp
导致相同问题的另一个原因是只读文件系统
如果/dev/sda1安装在/tmp上,并且由于写操作繁重,您的文件系统可能会变成只读
要使其再次可重写,请使用以下命令
mount -t ext3 -o rw,remount /dev/sda1 /tmp
我发现我的php.ini试图将会话保存到/var/lib/php/session而不是/tmp 因此,检查您的ini文件并查看它们保存到的位置(或将其设置到其他位置);然后确保目录可通过适当的进程写入Chris Rutledge是正确的, php有时会将sessions保存在/var/lib/php/session/directory上 检查php.ini文件或 创建具有777权限的目录
mkdir /var/lib/php/session
chmod -R 777 /var/lib/php/session
似乎我在Linux上发现了一些有趣的东西。在chroot中,当一些php软件尝试读/写会话时,php cgi也会犯同样的错误。我认为这可能是权限问题,但在设置777并将Web服务器的所有者设置为“/tmp”并在中设置它之后,它发现“/dev”中的“uradom”设备需要使用它。只需确保找到或复制/使其成为临时权限并更改权限(仅用于检查,然后更改为安全权限):
奇怪的是,在某些PHP5.x版本中不需要它,但在某些PHP7.x版本中需要它。只需将/var/lib/php/session/的所有权从apache更改为nginx,而不必进行全局读取 $sudo chown-R nginx:nginx/var/lib/php/session/TL;DR:将
nginx
用户添加到apache
组
RHEL已决定/var/lib/php/session
归php
包所有。该软件包决定,在安装时,它将始终重新创建/var/lib/php/session
目录,并始终将目录归root
所有,组设置为apache
,对每个目录都具有完全权限,对任何其他目录都没有权限。因此,尽管这里有许多建议的解决方案建议更改/var/lib/php/session
的权限,但这将在将来导致问题
RHEL建议的解决此问题的方法是在您希望存储会话目录的任何位置创建您自己的会话目录,并根据需要设置权限。未来的php更新不会影响新位置,一切都应该保持正常
另一种对我来说效果很好的方法是简单地将
nginx
添加到apache
组。我刚刚在CentOS上升级了PHP。我必须更改/etc/php-fpm.d/www.conf并更新php_值[session.save_path]变量并将其设置为/tmp
php_value[session.save_path] = /tmp
这个很好用。
我认为这不会造成安全隐患。当您使用NGINX时,服务器将权限授予apache而不是NGINX时,可能会出现此错误 我的解决办法是:
chown -R nginx:nginx /var/lib/php/
对于chown,您是该特定文件夹的所有者,-R表示其递归。请检查/tmp目录模式,它应该是777。还要检查selinux audit.log(如果已安装),我认为它。。。大多数用户在单独的卷上安装/tmp驱动器。。。我说“如果”。。在向stackoverflow发布任何问题之前,请先检查您的程序代码…@SmilingDevil:投票否决那些尽力提供帮助的人有点苛刻,你不觉得吗?我觉得这个答案有点太松散了,Joe在下面给出的答案是,在不打开整个tmp文件夹的情况下解决了这个问题。
/tmp
应该由所有用户读写。(正确的八进制权限是1777,而不是0777-区别在于“粘性”位,这使得用户A无法删除用户B的文件。)为什么有些安装使用/var/lib/php5/而有些安装使用/tmp。。。等等?是的,除了to/var/lib/php/5.5/session之外,我也遇到了同样的问题@CMCDRANGKAI我认为它的参数是您在编译它时提供的。注意,当您通过yum(CentOS)升级NginX时,它默认将/var/lib/php的所有权更改为“apache”--错误的所有权!这以前给我带来过麻烦。这不是正在干扰会话路径的nginx
或apache
。正是php
假设它与apache
一起使用才导致了问题。许多人决定“下注”,只使用/tmp
进行会话数据。这是相对不安全的。请参阅我的答案了解更多详细信息。对于那些想知道的人,php.ini参数是session.save\u path
。这对我很有用;升级CentOS 7服务器导致此文件夹的权限出错;购物车没有存放物品,用户无法登录,管理员网站也无法访问(虽然登录信息正在验证)。感谢您的评论,它也解决了我的会话问题。但是,在该会话文件夹上设置777 chmod是否没有安全问题?感谢您,我不确定此解决方案的安全性,而且我也这么做了,但每次服务器重新启动时,它似乎都会恢复chown nginx.nginx fpm
chown nginx.nginx fpm/sessionchmod 777 session
我的Ubuntu16.04/PHP7.1安装在/tmp中保留会话文件时出现问题,在重新启动时被擦除,所以我将会话文件夹更改为/var/lib/php/sessions
,这很有效。谢谢。我不明白。似乎PHP会话文件需要随机生成器设备/dev/uradom来生成不同的文件名,所以您的web服务器应该访问/dev/uradom(在Linux系统的服务器上)。可以吗?“chmod 777/dev/uradom”在这种情况下会有所帮助(仅供检查和使用)
chown -R nginx:nginx /var/lib/php/