Centos7 install php-redis extension and simple use

Source: Internet
Author: User
Tags install redis

Centos7 install php-redis extension and simple use
Preface:

In this blog, I will show you how to install the PHP-redis extension on Centos 7 and some simple and practical methods, and how to install redis on Centos, refer to the installation and deployment of Redis on Centos7 in my previous blog.

To operate redis in php, you must install the php-redis extension. Just like MySQL, If php wants to operate the mysql database, you must install the mysql extension, mysql extension is installed by default in php installation.

Step 1: Download and decompress

Download: https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz
It is a good habit to put the source code package under a specific directory. Now I put the source code package in/usr/local/src.

[lsgozj@localhost ~]# cd /usr/local/src[lsgozj@localhost src]# wget https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz

Decompress the package after the download is completed:

[Lsgozj @ localhost src] # tar-zxvf 2.2.4.tar.gz // enter the decompressed directory [lsgozj @ localhost src] # cd phpredis-2.2.4
Step 2: Compile and install

1. In this step, we will use the phpize generated during php installation to generate the configure configuration file.

[Lsgozj @ localhost phpredis-2.2.4] #/usr/local/php/bin/phpize (or/usr/bin/phpize) // The specific usage depends on the directory where your phpize file is located. You should use whereis phpize to view the path.

After executing the previous step, we have the configure configuration file.

2. Configuration

[Lsgozj @ localhost phpredis-2.2.4] #./configure -- with-php-config =/usr/local/php/bin/php-config # Configuration

The directory where php-config and phpize are located is the same. For example, if I use/usr/bin/phpize above, I will use it in this step. /configure-with-php-config =/usr/bin/php-config (in fact, if your php is the default installation path, directly. /configure is enough, for personal purposes)

3. Compile and install

[Lsgozj @ localhost phpredis-2.2.4] # make # compile [lsgozj @ localhost phpredis-2.2.4] # make install # install
Step 3: Configure php support

Configure the php configuration file php. ini (where you can use whereis php. ini to view details). My configuration file php. ini is under/etc /.

[Lsgozj @ localhost phpredis-2.2.4] # vim/etc/php. ini // edit php. ini: Add extension = "redis. so"

Save and exit

Step 4: restart the apache server to make the configuration take effect.
[lsgozj@localhost phpredis-2.2.4]# systemctl restart httpd.service

After the restart, check whether the php-redis extension is installed.

[Lsgozj @ localhost phpredis-2.2.4] # echo "<? Php echo phpinfo ()?> ">/Home/www/index. php (if the web directory is not changed here, it is in/var/www/html /)

Enter 127.0.0.1 in the address bar of the browser to view the php extension. If the extension is shown in a piece, the installation is successful:

Step 5: simple use of redis in php

In fact, redis is no different from mysql. It is the same as a database (redis is a non-relational database, mysql is a relational database). Therefore, it is similar in use, and it is also used for command execution and php operations, you can use the command line format of apsaradb for redis on your own.

<? Php $ redis = new redis (); // instantiate the redis class $ redis-> connect ('2017. 127. 0.0.1 ', 6379); // redis connection. 127.0.0.1 is a local server, because the PHP file and the connected redis database are on the same host, and 6379 is the default port of redis, you can omit $ redis-> set ('name', 'lsgogroup'); // set the cache value $ redis-> get ('name '); // obtain the cache value $ reids-> setex ('name', 3600, 'lsgogroup '); // set the cache validity period to 1 hour $ redis-> del ('name'); // manually delete the cache?>

The redis command is much more than what we mentioned above. It is only simple to use here.

Statement: Since I didn't start this blog very much at the beginning, I have referenced several blogs on the Internet and MOOC. I will not give the original address here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.