Php-Redis安装测试笔记_php技巧
后端开发用到php操作redis,在此将安装测试过程中遇到的问题汇总记录下来,以便以后参考!(系统为ubuntu)
1.redis安装
下载地址:http://download.redis.io/releases/
解压安装:
tar -xvf redis-2.8.17.tar.gz
make
sudo make install
为方便使用,在/usr目录下创建redis目录,讲如下几个文件拷贝到/usr/redis/目录下:
/yourdir/redis-2.8.17/redis.conf
/yourdir/redis-2.8.17/src/redis-benchmark
/yourdir/redis-2.8.17/src/redis-server
/yourdir/redis-2.8.17/src/redis-cli
当然,你也可以通过软连接的方式达到方便使用的目的。此外,你也可以将redis-server加入开机启动,此处从略。
2.redis测试
1)先开启redis服务端程序
为方便测试,我们将redis.conf配置文件中的loglevel和logfile的值,修改后如下:
loglevel debug
logfile “/tmp/redis.log”
jay13@ubuntu:/usr/redis$ redis-server redis.conf
2)开启redi客户端,通过客户端向redis数据库中进行增删改查操作。整个操作过程中生成的日志可以到/tmp/redis.log中查看。
以最简单的key操作为例,实例如下:
jay13@ubuntu:/usr/redis$ redis-cli
127.0.0.1:6379> set jay13 jb51.net
OK
127.0.0.1:6379> set jay hello,world
OK
127.0.0.1:6379> get jay
"hello,world"
127.0.0.1:6379> get jay13
"jb51.net"
127.0.0.1:6379> del jay
(integer) 1
127.0.0.1:6379> get jay
(nil)
127.0.0.1:6379> set jay13 www.jb51.net
OK
127.0.0.1:6379> get jay13
"www.jb51.net"
3.安装phpredis扩展
在使用sudo apt-get install php5安装php时,默认是没有安装phpize的,我们安装phpredis时,需要用到phpize,因此,需要先安装phpize。
1)我们通过安装php开发者工具来获取phpize。执行如下命令即可:
sudo apt-get install php5-dev
2)获取phpredis源文件
最新的phpRedis地址:https://github.com/nicolasff/phpredis
按照GitHub上的说明进行如下安装时,
phpize
./configure --enable-redis-igbinary
make && make install
可能会出现如下出错说明:
checking for igbinary includes... configure: error: Cannot find igbinary.h
这个是因为我们没有igbinary扩展,这是phpredis依赖的一个东西。
好吧,怎么安装igbinary呢?
使用apt-get没有无法安装完成,我们通过下载安装文件进行安装。
wget http://pecl.php.net/get/igbinary-1.1.1.tgz
tar -xzvf igbinary-1.1.1.tgz
cd igbinary-1.1.1
phpize
./configure # No need for extra config params
make
make install
安装好igbinary后,可以用如下命令安装phpredis。
phpize
./configure –enable-redis-igbinary
make && make install
至此,安装完成。
我们修改php.ini配置文件,将刚才安装的两个扩展加入到php.ini文件中,加入的语句如下:
extension=igbinary.so
extension=redis.so
重启apache,Done!!!
4.测试php-redis
在网页根目录/var/www/中新建文件test.php,内容如下:
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->set('Jay13','www.jb51.net');
echo 'Jay13:'.$redis->get('Jay13');
echo '';
echo 'Jay12:'.$redis->get('Jay12');
?>
结果如下图:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
