将php的session存放到redis

通常我们将 session 存储到 file 或者 memcached

现今异地结构一体化,通常许多人会将 session 存储在 redis 里面

准备:

PHP 8.2.16

Pecl_Redis 6.0.1

安装拓展:

1
2
3
4
5
6
7
8
wget https://pecl.php.net/package/redis/6.0.1
mv redis-6.0.1.tgz php-redis-6.0.1.tgz
tar -zxvf php-redis-6.0.1.tgz
mv redis-6.0.1 php-redis-6.0.1
cd php-redis-6.0.1
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

修改 php.ini 文件:

`vim /etc/php.ini`

追加 redis 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
extension = "/usr/local/php/lib/extensions/no-debug-non-zts-20220829/redis.so"

session.save_handler = redis # 默认的设置是file,这里改成redis

session.save_path = "tcp://192.168.2.11:6379?auth=passwd" # 这里填redis的连接配置参数
;session.save_path = "tcp://192.168.2.11:6379" # 如果redis不带密码,则使用这种配置

session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID # 这个名字可以自己改
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5

存储到 redis 里面的 keys 的名称是类似 PHPREDIS_SESSIONxxxxxxxxx ,我们只需要连接到 redis,并执行 keys PHPREDIS_SESSION* 即可列出全部的 sessionkeys.若存在则为成功。

keys PHPREDIS_SESSION* "PHPREDIS_SESSION:77fsmg64qqebooaotlue6050g7"

测试 php 脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
// 启动会话
session_start();

// 配置 Redis 连接
$redis = new Redis();
$redis->connect('your_remote_redis_host', 6379); // 替换为你的远程 Redis 服务器地址和端口
$redis->auth('your_redis_password'); // 替换为你的 Redis 密码

// 配置 Redis 作为会话存储
ini_set('session.save_handler', 'redis');
// 替换为你的远程 Redis 服务器地址和端口
ini_set('session.save_path', 'tcp://your_remote_redis_host:6379');

// 尝试存储和获取会话数据
$_SESSION['test'] = 'Hello Redis';
echo $_SESSION['test'];

// 关闭 Redis 连接
$redis->close();

PHP局部开启Composer

正文

指定 PHP 版本下安装

curl -sS https://getcomposer.org/installer | /usr/local/php-你在使用的php版本号/bin/php

验证是否安装成功

/usr/local/php-你在使用的php版本号/bin/php composer.phar

进入项目根目录执行

/usr/local/php-你在使用的php版本号/bin/php composer.phar install

即开启并完成项目 composer

其他

Q:Your lock file does not contain a compatible set of packages.

原因在于 PHP 版本不对,不符合 composer.json 需要的版本,可以设置忽略版本匹配。

composer install --ignore-platform-reqs

composer update --ignore-platform-reqs

此外,也有可能是 composer 镜像源的问题

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

CentOS7中编译PHP及开启JIT

实施过程

1、安装相关依赖

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel

2、下载并解压

建议查看官网release 版本

wget https://www.php.net/distributions/php-8.0.30.tar.gz

tar -zxvf php-8.0.30.tar.gz

3、预编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
./configure \
--with-config-file-path=/usr/local/php80/etc \
--with-config-file-scan-dir=/usr/local/php80/etc/php.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--with-freetype-dir=/usr/local/freetype \
--enable-gd \
--enable-gd-jis-conv \
--with-gettext \
--with-iconv=/usr/local/libiconv \
--with-libxml-dir=/usr \
--with-kerberos \
--with-libdir=/usr/lib64 \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--enable-pdo \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--with-password-argon2 \
--with-sodium=/usr/local \
--disable-rpath \
--disable-fileinfo \
--disable-debug \
--enable-intl \
--enable-exif \
--enable-ftp \
--enable-cli \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip

如果看到以下提示信息,说明 configure 成功。

4、安装

sudo make && make install

到安装目录查看版本

/usr/local/php80/bin/php -v

5、编辑配置

复制 php.ini

cp php.ini-production /usr/local/php80/etc/php.ini

复制 php-fpm

cp /usr/local/php80/etc/php-fpm.conf.default /usr/local/php80/etc/php-fpm.conf

cp /usr/local/php80/etc/php-fpm.d/www.conf.default /usr/local/php80/etc/php-fpm.d/www.conf

修改 php-fpm 配置,在 php-fpm.conf 中开启 pid = run/php-fpm.pid 配置项

6、配置启动 php-fpm

复制 php-fpm 的启动脚本
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

授权执行权限
chmod +x /etc/init.d/php-fpm

启动 php-fpm
/etc/init.d/php-fpm start 或者service php-fpm start

7、开启 openche 和 JIT

1
2
3
4
5
6
7
8
9
10
11
12
13
zend_extension=/usr/local/php80/lib/php/extensions/no-debug-non-zts-20231015/opcache.so

[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000

***

opcache.jit=1205
opcache.jit_buffer_size=64M

记录问题

Q: 编译时候出现错误,configure: error: Please reinstall the iconv library.

没有安装 iconv 库,iconv –version

iconv (GNU libiconv 1.17)

Copyright (C) 2000-2017 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Written by Bruno Haible.

下载并编译

1
2
3
4
5
6
7
8
9
10
11
wget https://ftp.gnu.org/gnu/libiconv/libiconv-1.17.tar.gz
tar -zxvf libiconv-1.17.tar.gz
cd ./libiconv-1.17
./configure --prefix=/usr/local/libiconv --enable-static --enable-shared
#--prefix 参数表示安装目录,默认为 /usr/local;--enable-static 和 --enable-shared 参数表示编译时同时生成静态和动态库文件。
make
sudo make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/libiconv.conf
echo "/usr/local/libiconv/lib" > /etc/ld.so.conf.d/libiconv.conf
sudo ldconfig -v
sudo ldconfig

若还是出现问题,建议先--without-iconv再后面 make 的时候

make ZEND_EXTRA_LIBS='-liconv'

Q: 编译时候出现错误,configure: error: Package requirements (libsodium >= 1.0.8) were not met:

libsodium 版本低了直接上yum install libsodium libsodium-devel解决

不行的话可以继续手动编译

1
2
3
4
5
6
wget https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gz
tar -zxvf libsodium-1.0.18.tar.gz
cd libsodium-1.0.18
./configure CC="gcc -m64" --prefix=/usr/local/libsodium --libdir=/usr/lib64
sudo make
sudo make install

Q: 提示 OpenSSL 找不到 / libssl.so.1.1: cannot open shared object file: No such file or directory:

请果断升级 openssl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -xvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config shared --openssldir=/usr/local/openssl --prefix=/usr/local/openssl
make && make install
echo "/usr/local/lib64/" >> /etc/ld.so.conf
ldconfig
openssl version
# 如果不行继续敲入
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/lib/openssl /usr/lib/openssl.old
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
sudo ldconfig -v
sudo ldconfig
openssl version
# OpenSSL 1.1.1w 11 Sep 2023