PHP会话共享(redis):
按照yum安装路径演示:
vim /etc/php.ini
#以下三行参数都死修改后的配置
session.save_handler = redis #指定保存到redis
session.save_path = "tcp://127.0.0.1:6379?auth=redhat" #指定redis地址与端口,有密码就指定?auth=密码
session.auto_start = 1 #启动session
-------------------------------------------------------------------------------------------------------
#注释php-fpm.d/www.conf里面的两条内容,否则session内容会一直写入/var/lib/php/session目录中
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
重启PHP:
systemctl restart php-fpm.service
检查redis存在cookie对应的session:
redis-cli -a redhat
#'查看所有key
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:658f75cbcc9f9df92f067790f2f5ca9e"
session已经在web服务器间进行共享了
PHP网站优化(fastcgi):
http区块配置:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
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;
tcp_nopush on; # 激活 TCP_CORK socket 选择
tcp_nodelay on; # 数据在传输的过程中不进缓存
fastcgi_connect_timeout 240; # Nginx服务器和后端FastCGI服务器连接的超时时间
fastcgi_send_timeout 240; # Nginx允许FastCGI服务器返回数据的超时时间,即在规定时间内后端服务器必须传完所有的数据,否则Nginx将断开这个连
接
fastcgi_read_timeout 240; # Nginx从FastCGI服务器读取响应信息的超时时间,表示连接建立成功后,Nginx等待后端服务器的响应时间
fastcgi_buffer_size 64k; # Nginx FastCGI 的缓冲区大小,用来读取从FastCGI服务器端收到的第一部分响应信息的缓冲区大小
fastcgi_buffers 4 64k; # 设定用来读取从FastCGI服务器端收到的响应信息的缓冲区大小和缓冲区数量
fastcgi_busy_buffers_size 128k; # 用于设置系统很忙时可以使用的 proxy_buffers 大小
fastcgi_temp_file_write_size 128k; # FastCGI 临时文件的大小
#fastcti_temp_path /data/ngx_fcgi_tmp; # FastCGI 临时文件的存放路径
fastcgi_cache_path /data/nginx_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; # 缓存目录
client_max_body_size 8m;
include extra/*.conf;
}
server区块配置:
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#include fastcgi.conf;
fastcgi_cache ngx_fcgi_cache; # 缓存FastCGI生成的内容,比如PHP生成的动态内容
fastcgi_cache_valid 200 302 1h; # 指定http状态码的缓存时间,这里表示将200和302缓存1小时
fastcgi_cache_valid 301 1d; # 指定http状态码的缓存时间,这里表示将301缓存1天
fastcgi_cache_valid any 1m; # 指定http状态码的缓存时间,这里表示将其他状态码缓存1分钟
fastcgi_cache_min_uses 1; # 设置请求几次之后响应被缓存,1表示一次即被缓存
fastcgi_cache_use_stale error timeout invalid_header http_500; # 定义在哪些情况下使用过期缓存
fastcgi_cache_key http://$host$request_uri; # 定义 fastcgi_cache 的 key
}
PHP参数说明:(yum安装环境)
PHP文件目录说明:
/etc/php-fpm.conf # --- php-fpm进程的配置文件
/etc/php-fpm.d/ # --- php-fpm进程加载配置文件的目录
nginx配置参数说明:
server {
listen 80;
server_name blog.52olda.cn;
location / {
root html/blog;
index index.html index.php;
}
location ~ \.php$ {
root html/blog; #--- PHP程序访问的站点数据信息
fastcgi_index index.php; #--- 指定动态请求的默认首页文件 index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #将静态服务获取到的请求信息进行整合后抛送请求给PHP程序
fastcgi_pass 127.0.0.1:9000; #--- 9000代表就是php服务程序
include fastcgi_params; #--- 加载fastcgi配置文件,可以识别变量信息
}
}
PHP配置文件:
vim /etc/php-fpm.d/www.conf
user = nginx #启动php程序的工作进程用户
group = nginx #启动php程序的工作进程用户组
listen = 127.0.0.1:9000 #第22行行,修改监听地址与端口
listen.allowed_clients = 127.0.0.1 #第48行,允许哪些客户端(web)可以访问
######开启慢日志
slowlog = /var/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 10s
######设置php的session目录(所属用户和用户组都是nginx)
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
PHP.INI详解说明:
vim /etc/php.ini
######发送数据最大的大小
post_max_size = 1024M
#####内存缓存最大数据大小
memory_limit = 1024M
#####允许客户端最大上传数据大小
upload_max_filesize = 1024M
######避免PHP信息暴露在http头中
expose_php = Off
######避免暴露php调用mysql的错误信息
display_errors = Off
######在关闭display_errors后开启PHP错误日志(路径在php-fpm.conf中配置)
log_errors = On
######设置PHP的扩展库路径
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20141001/"
######设置PHP的opcache和mysql动态库
zend_extension=opcache.so
extension=mysqli.so
extension=pdo_mysql.so
######设置PHP的时区
date.timezone = PRC
######开启opcache
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
######设置PHP脚本允许访问的目录(需要根据实际情况配置)
;open_basedir = /usr/share/nginx/html;
配置php-fpm.conf:
vim /etc/php-fpm.conf
######设置错误日志的路径
error_log = /var/log/php-fpm/error.log
######引入www.conf文件中的配置
include=/usr/local/php7/etc/php-fpm.d/*.conf