本文将介绍如何在 RHEL / CentOS 8 上安装 phpredis。
目录
安装Redis
首先,我们需要在我们的服务器上安装Redis。为此,请先阅读这篇文章:在 RHEL / CentOS 8 上安装和配置 Redis。
安装phpredis
在安装phpredis之前,我们需要安装一些依赖:
# yum-utils
sudo dnf -y install yum-utils
# epel repo
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
# remi repo
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
现在我们可以安装 phpredis:
sudo dnf -y install php-pecl-redis5
重新启动网络服务器:
# apache
sudo systemctl restart httpd
# nginx
sudo systemctl restart nginx
# php-fpm if installed
sudo systemctl restart php-fpm
开放端口和配置 SELinux
默认情况下,Redis 在端口 6379上运行。我们需要从防火墙允许这个端口:
# firewalld
sudo firewall-cmd --add-port=6379/tcp --permanent
sudo firewall-cmd --reload
# ufw
sudo ufw allow 6379/tcp
如果您使用的是其他防火墙,则从防火墙打开 6379 端口。
如果启用了 SELinux,我们必须运行此命令才能在 PHP 文件中运行 Redis:
setsebool -P httpd_can_network_connect on
存储和获取数据
创建一个名为的文件 cache.php
并粘贴以下代码:
<?php
// connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection successful<br/><br/>";
// caching
$cache_name = "test_redis_cache";
$expire_time = 10;
if ($redis->exists($cache_name)) {
echo 'Load from cache:<br>';
$data = $redis->get($cache_name);
}
else {
echo 'Cache missed!<br>';
$data = Date("d M, Y - h:i:s");
$redis->set($cache_name, $data);
$redis->expire($cache_name, $expire_time); // will disappear in 10 seconds.
}
echo $data;
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/install-phpredis-on-centos8-rhel8/