阅读量:2
在Ubuntu上配置PHP缓存常用方法包括使用OPcache、Redis、Memcached等,以下是具体步骤:
1. OPcache(内置扩展,推荐)
- 安装:
sudo apt update sudo apt install php-opcache # 大多数Ubuntu默认已安装 - 配置:
编辑PHP配置文件(如/etc/php/8.x/apache2/php.ini或/etc/php/8.x/fpm/php.ini):[opcache] zend_extension=opcache.so opcache.enable=1 opcache.memory_consumption=128 # 缓存大小(MB) opcache.max_accelerated_files=4000 # 缓存文件数 opcache.revalidate_freq=60 # 检查脚本更新频率(秒) - 重启服务:
sudo systemctl restart apache2 # 或 nginx/php-fpm
2. Redis(外部缓存后端)
- 安装:
sudo apt update sudo apt install redis-server php-redis sudo systemctl start redis-server sudo systemctl enable redis-server - 配置:
编辑PHP配置文件,添加:[redis] extension=redis.so - 使用:
在PHP代码中通过Redis类连接缓存:$redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->set('key', 'value', 3600); // 缓存1小时
3. Memcached(分布式缓存)
- 安装:
sudo apt update sudo apt install memcached php-memcached sudo systemctl start memcached sudo systemctl enable memcached - 配置:
编辑PHP配置文件,添加:[memcached] extension=memcached.so - 使用:
在PHP代码中通过Memcached类操作缓存:$memcached = new Memcached(); $memcached->addServer('127.0.0.1', 11211); $memcached->set('key', 'value', 3600);
验证配置
- OPcache:在PHP脚本中执行
phpinfo(),搜索“opcache”确认启用。 - Redis/Memcached:通过
redis-cli或telnet 127.0.0.1 11211测试连接。
根据需求选择缓存方案,OPcache适合字节码缓存,Redis/Memcached适合数据缓存。
以上就是关于“PHP缓存配置Ubuntu怎么弄”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm