简体   繁体   English

php-fpm 和 nginx 会话问题

[英]php-fpm and nginx session problems

I've been having this problem for the past week or so.在过去一周左右的时间里,我一直遇到这个问题。 I've been working on a PHP project that relies HEAVILY on Sessions.我一直在研究一个严重依赖会话的 PHP 项目。 For some reason we've been having troubles with the sessions saving the past few days.出于某种原因,我们在保存过去几天的会话时遇到了麻烦。 Any idea why?知道为什么吗?

Here's the error:这是错误:

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 version: nginx版本:

nginx version: nginx/1.0.11

PHP-FPM config: 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: 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;
                }
        }
}

I found that my php.ini was attempting to save sessions to /var/lib/php/session rather than /tmp我发现我的 php.ini 试图将会话保存到 /var/lib/php/session 而不是 /tmp

So check your ini file and see where they're being saved to (or set it to somewhere else);因此,请检查您的 ini 文件并查看它们的保存位置(或将其设置为其他位置); then make sure that directory is writeable by the appropriate processes然后确保该目录可由适当的进程写入

Just change the ownership of /var/lib/php/session/ to nginx from apache instead of giving a world read.只需将 /var/lib/php/session/ 的所有权从 apache 更改为 nginx,而不是提供全局读取。

$sudo chown -R nginx:nginx /var/lib/php/session/ $sudo chown -R nginx:nginx /var/lib/php/session/

TL;DR: Add nginx user to apache group TL;DR:将nginx用户添加到apache

RHEL has decided that /var/lib/php/session is owned by the php package. RHEL 已决定/var/lib/php/sessionphp包所有。 That package has decided that it will always recreate the /var/lib/php/session directory when installed and will always return the directory to being owned by root with group set to apache with full permissions for each and no permissions for anything else.该软件包已决定在安装时始终重新创建/var/lib/php/session目录,并且将始终将该目录归还给root ,组设置为apache ,每个人都拥有完全权限,其他任何人都没有权限。 Therefore, while many suggested solutions here suggest changing the permissions of /var/lib/php/session , that will cause problems in the future.因此,虽然这里许多建议的解决方案建议更改/var/lib/php/session的权限,但这会在将来引起问题。

https://bugzilla.redhat.com/show_bug.cgi?id=1146552 https://bugzilla.redhat.com/show_bug.cgi?id=1146552

The RHEL suggested way of fixing this issue is to create your own session directory wherever you'd like to store it and set the permissions as necessary. RHEL 建议的解决此问题的方法是在您想要存储它的任何位置创建自己的会话目录,并根据需要设置权限。 Future php updates won't affect that new location and everything should stay working.未来的 php 更新不会影响那个新位置,一切都应该保持工作。

An alternative that has worked quite well for me is to simply add nginx to the apache group.对我来说效果很好的另一种方法是简单地将nginx添加到apache组。

this error occured due to the user which run php process may not have permission to write on /tmp directory发生此错误是由于运行 php 进程的用户可能无权在 /tmp 目录上写入

to make it writeable by all user use this commend使其可被所有用户写入使用此推荐

chmod 777 /tmp

another reason which causes the same issue is read only file system导致相同问题的另一个原因是只读文件系统

if /dev/sda1 is mounted on /tmp and due to heavy write your file system may become read only...如果 /dev/sda1 安装在 /tmp 上并且由于大量写入,您的文件系统可能会变为只读...

to make it rewritable again use this command要使其再次可重写,请使用此命令

mount -t ext3 -o rw,remount /dev/sda1 /tmp

Chris Rutledge is right, php some times is saving sesions on /var/lib/php/session/ directory check your php.ini file or create the directory with 777 rights Chris Rutledge 是对的,php 有时会在 /var/lib/php/session/ 目录上保存会话 检查您的 php.ini 文件或创建具有 777 权限的目录

mkdir /var/lib/php/session
chmod -R 777 /var/lib/php/session

Seems I found something interesting on the Linux.似乎我在 Linux 上发现了一些有趣的东西。 In the chroot php-cgi make same errors when some PHP software try to read/write session.在 chroot php-cgi 中,当某些 PHP 软件尝试读/写会话时,会出现相同的错误。 I thought this could be permission issue, but after set 777 and set owner of the webserver to the "/tmp" and set it in the After many hours it found that "urandom" device in the "/dev" needed to work it.我认为这可能是权限问题,但是在设置 777 并将网络服务器的所有者设置为“/tmp”并将其设置在“/dev”中的“urandom”设备需要工作很多小时之后。 Just make sure that it found or copy/make it and change permissions temporary (just for check and then change to safely):只需确保它找到或复制/制作它并临时更改权限(仅用于检查然后更改为安全):

chmod 777 /dev/urandom

Strange to me that it wasn't required in some PHP5.x version but in some PHP7.x need to be there.对我来说奇怪的是,在某些 PHP5.x 版本中不需要它,但在某些 PHP7.x 中需要存在。

I just went through an upgrade of PHP on CentOS.我刚刚在 CentOS 上升级了 PHP。 I had to change /etc/php-fpm.d/www.conf and update the php_value[session.save_path] variable and set it to /tmp我不得不更改 /etc/php-fpm.d/www.conf 并更新 php_value[session.save_path] 变量并将其设置为 /tmp

php_value[session.save_path]    = /tmp

This works fine.这工作正常。 I don't think this will be a security hazard.我认为这不会构成安全隐患。

You might get this error when you'r using NGINX and the server gives permission to apache instead of nginx.当您使用 NGINX 并且服务器授予 apache 而不是 nginx 权限时,您可能会收到此错误。

My fix is:我的解决方法是:

chown -R nginx:nginx /var/lib/php/

With chown you are changhing the owner of that specific folder and -R means its recursive.使用 chown 您正在更改该特定文件夹的所有者,而 -R 表示其递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM